[loj3408]lancllords

Posted PYWBKTDA

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[loj3408]lancllords相关的知识,希望对你有一定的参考价值。

考虑归并排序,问题即如何合并两个序列\\(A,B\\)

  • 不妨假设\\(|A|>|B|\\),将\\(A\\)按下标奇偶性划分为\\(A_0\\)\\(A_1\\)

  • \\(A_0\\)\\(B\\)归并,得到序列\\(C\\)

  • 对于\\(A_1\\)中的元素,仅需与(\\(C\\)中)\\(A_0\\)中相邻两数间的\\(B\\)中元素比较

    比较次数为\\(|B|\\),用莫队实现,操作次数为\\(O(l\\sqrt|B|)\\)(其中\\(l\\)为区间长度)

综上,复杂度即\\(T(n)=T(\\fracn2)+O(l\\sqrtn)\\),由主定理得\\(T(l)=O(l\\sqrtl)\\)

在此基础上,原问题复杂度即\\(T\'(n)=2T\'(\\fracn2)+O(n\\sqrtn)\\),由主定理得\\(T\'(n)=O(n\\sqrtn)\\)

综上,总比较次数为\\(O(n\\log n)\\),总移动次数为\\(O(n\\sqrtn)\\)

#include<bits/stdc++.h>
#include "lancllords.h"
using namespace std;
typedef vector<int>vi;
const int N=150005;
int p,q,K;vi ans;
unordered_map<int,bool>mat[N];
struct Node
	int x,y;
	bool operator < (const Node &n)const
		return (x/K!=n.x/K ? x/K<n.x/K : y<n.y);
	
;vector<Node>Q;
bool cmp(int x,int y)
	if (mat[x].find(y)!=mat[x].end())return mat[x][y];
	while (p<x)p++,inc_p();
	while (p>x)p--,dec_p();
	while (q<y)q++,inc_q();
	while (q>y)q--,dec_q();
	return mat[x][y]=cmp();

vi merge(int l,vi vx,vi vy)
	int sx=vx.size(),sy=vy.size();
	if (sx<sy)swap(vx,vy),swap(sx,sy);
	if (!sy)return vx;
	int k=0;vi v;
	for(int i=1;i<sx;i+=2)v.push_back(vx[i]);
	vy=merge(l,v,vy),sy=vy.size();
	Q.clear();
	for(int i=0;i<sx;i+=2)
		while ((k<sy)&&((i==sx-1)||(vy[k]!=vx[i+1])))Q.push_back(Nodevx[i],vy[k++]);
		if (k<sy)k++;
	
	if (!Q.empty())
		K=max((int)(l/sqrt(Q.size())),1);
		sort(Q.begin(),Q.end());
		for(Node i:Q)cmp(i.x,i.y);
	
	k=0,v.clear();
	for(int i=0;i<sx;i+=2)
		bool flag=0;
		while ((k<sy)&&((i==sx-1)||(vy[k]!=vx[i+1])))
			if ((!flag)&&(cmp(vx[i],vy[k])))flag=1,v.push_back(vx[i]);
			v.push_back(vy[k++]);
		
		if (!flag)v.push_back(vx[i]);
		if (k<sy)v.push_back(vy[k++]);
	
	while (k<sy)v.push_back(vy[k++]);
	return v;

vi solve(int l,int r)
	if (l==r)return vil;
	int mid=(l+r>>1);
	return merge(r-l+1,solve(l,mid),solve(mid+1,r));

vi answer(int n)
	vi v=solve(0,n-1);
	ans.resize(n);
	for(int i=0;i<n;i++)ans[v[i]]=i;
	return ans;
 

loj 1248

Given a dice with n sides, you have to find the expected number of times you have to throw that dice to see all its faces at least once. Assume that the dice is fair, that means when you throw the dice, the probability of occurring any face is equal.

For example, for a fair two sided coin, the result is 3. Because when you first throw the coin, you will definitely see a new face. If you throw the coin again, the chance of getting the opposite side is 0.5, and the chance of getting the same side is 0.5. So, the result is

1 + (1 + 0.5 * (1 + 0.5 * ...))

= 2 + 0.5 + 0.52 + 0.53 + ...

= 2 + 1 = 3

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with a line containing an integer n (1 ≤ n ≤ 105).

Output

For each case, print the case number and the expected number of times you have to throw the dice to see all its faces at least once. Errors less than 10-6 will be ignored.

Sample Input

5

1

2

3

6

100

Sample Output

Case 1: 1

故设dp[i]为在已经扔出了i个不同面的情况下扔出n个不同面的期望次数,dp[n]=0,答案为dp[0]
dp[i]=dp[i]in+dp[i+1]nin+1dp[i]=dp[i]∗in+dp[i+1]∗n−in+1

移项得:dp[i]=dp[i+1]+nni

至今直接仍然不明白为什么是n/n-i,后来还是觉得要看状态转移方程,其中1和后面的一个式子是好理解的,第二个式子则是表明保持原装态不变,这才是需要理解的。

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
#define N 100010

int main()

    int T,n,iCase=1;
    double dp[N];
    scanf("%d",&T);
    while(T--)
    
        scanf("%d",&n);
        dp[n]=0;
        for(int i=n-1;i>=0;i--) dp[i]=dp[i+1]+n*1.0/(n-i);
        printf("Case %d: %.7f\n",iCase++,dp[0]);
    
    return 0;

 

以上是关于[loj3408]lancllords的主要内容,如果未能解决你的问题,请参考以下文章

BZOJ 3408 Usaco Heat Wave

loj558&loj2011

loj10195. 「一本通 6.1 练习 2」转圈游戏 (loj2608)

loj10139. 「一本通 4.5 练习 1」树上操作(loj2125. 「HAOI2015」树上操作 )

[Loj] 数列分块

59: loj #10215