暑假集训CSP提高模拟 16

\[暑假集训CSP提高模拟 \lim_{x\rightarrow\infty}\frac{8f_{x}}{f_{x+1}}\times(\sqrt{5}+1),\ \forall f_{x}=f_{x-1}+f_{x-2} \]

如果你实在不会算 \(\forall f_{x}=f_{x-1}+f_{x-2}\) 的情况,那你可以把它特化成斐波那契数列. 如果你连斐波那契数列特化的上式都算不出来,那么你应该看 这一篇 的第 7.7 章

A.九次九日九重色

今天先去研究了一下 \(n\log n\) 的最长上升子序列和最长公共子序列怎么写:

根据贪心思路,我们可以将最长上升子序列问题的求解优化至 \(n\log n\)

考虑到对于每次转移,我们都可以记录下当前序列最末尾的数字,可以想到的是,当前序列末尾数字越小,则可能被填入子序列的数字就越多,因此末尾更小的序列可能就是决策更优的,因此我们每次都保留这样的序列向后拓展.

也就是,我们使用数组 \(f_{i}\) 表示当枚举到第 \(i\) 位时,全部最长上升子序列中,最小的末尾元素值,则可以发现:

  • 当新的元素值更大的时候,符合上升条件,直接填到末尾
  • 当新的元素值更小的时候,虽然不符合上升条件,但是符合更新条件,可能会导致更优的决策,因此我们向前找到第一个能放当前值的 \(i\) 进行更新.

代码如下:

#include<bits/stdc++.h>
using namespace std;
int n;
int a[100001];
int f[100001];
int main(){
	ios::sync_with_stdio(false);
	cin>>n;
	for(int i=1;i<=n;++i){
		cin>>a[i];
	}
	memset(f,0x3f,sizeof f);
	f[1]=a[1];
	int len=1;
	for(int i=2;i<=n;++i){
		int l=0,r=len;
		if(a[i]>f[len]){
			f[++len]=a[i];
		}
		else{
			f[lower_bound(f+1,f+len+1,a[i])-f]=a[i];
		}
	}
	cout<<len<<endl;
}

字序列是一个字符串中有序的一段,即序列中的每个数在原序列内都从左到右排列,公共子序列即为几个字符串都含有的子序列.

\[dp[i][j]=\begin{cases}dp[i-1][j-1]+1\qquad (x[i]==y[j]) \\max \begin{cases} dp[i][j-1]\\dp[i-1][j] \end{cases} \qquad(x[i] \neq y[j]) \end{cases} \]

类似最长公共子串,但不一样的是,假如 \(x[i] \neq y[j]\),不要急着清零,因为最长公共子序列并不需要严格相邻. 此时应该跳过不相等的内容,那么如何跳过呢? 我们采用了继承相邻状态的方法.

假如序列中的每个数都仅仅出现过一次,那么我们可以考虑对上述 \(n^{2}\) 算法进行优化.

A:3 2 1 4 5
B:1 2 3 4 5

我们不妨给它们重新标个号:把 \(3\) 标成 \(a\),把 \(2\) 标成 \(b\),把 \(1\) 标成 \(c\),以此类推,目的是将 \(A\) 变为一个递增的序列,于是变成:

A: a b c d e
B: c b a d e

这样标号之后,最长公共子序列长度显然不会改变. 但是出现了一个性质:
两个序列的子序列,一定是 \(A\) 的子序列. 而A本身就是单调递增的,因此这个子序列是单调递增的

换句话说,只要这个子序列在 \(B\) 中单调递增,它就是 \(A\) 的子序列

哪个最长呢?当然是 \(B\) 的最长上升子序列最长
因此,我们只需要在 \(n\log n\) 的时间复杂度内求出 \(B\) 的最长上升子序列即可

因此这个题也可以转化成最长公共子序列问题,只不过在这里我们的 “公共” 是整除

#include<bits/stdc++.h>
using namespace std;
int n;
int p[200001],q[200001];
int a[200001];
int f[200001];
struct pai{
	int a,b;
	bool operator <(const pai &A)const{
		if(a==A.a) return b>A.b;
		return a<A.a;
	}
};
vector<pai>v;
map<int,int>mp;
int posp[200001],posq[200001];
int c[200001];
int lowbit(int x){return x&-x;}
int ask(int x){
	int ans=0;
	if(x<=0) return 0;
	while(x){
		ans=max(ans,c[x]);
		x-=lowbit(x);
	}
	return ans;
}
void update(int x,int val){
	if(x<=0) return;
	while(x<=n){
		c[x]=max(c[x],val);
		x+=lowbit(x);
	}
}
vector<int>pos[200001];
int main(){
	ios::sync_with_stdio(false);
	cin>>n;
	for(int i=1;i<=n;++i){
		cin>>p[i];
		posp[p[i]]=i;
	}
	for(int i=1;i<=n;++i){
		cin>>q[i];
		posq[q[i]]=i;
	}
	for(int i=1;i<=n;++i){
		for(int j=i;j<=n;j+=i){
			if(posq[j]) pos[i].push_back(posq[j]);
		}
//		for(int i:pos[i]) cout<<i<<" ";cout<<endl;
		sort(pos[i].begin(),pos[i].end());
		reverse(pos[i].begin(),pos[i].end());
	}
	int ans=0;
	for(int i=1;i<=n;i++){
		for(auto v:pos[p[i]]){
			int j=v;
			f[j]=ask(j);
			f[j]=max(ask(j-1)+1,f[j]);
			ans=max(ans,f[j]);
			update(j,f[j]);
		}
	}
	cout<<ans<<endl;
//	sort(v.begin(),v.end());
//	for(int i=0;i<=v.size()-1;++i){
//		cout<<v[i].a<<" "<<v[i].b<<endl;
//		a[i+1]=v[i].b;
//	}
//	memset(f,0x7f,sizeof f);
//	f[1]=a[1];
//	int len=1;
//	for(int i=2;i<=n;++i){
//		int l=0,r=len;
//		if(a[i]>f[len]){
//			f[++len]=a[i];
//		}
//		else{
//			f[lower_bound(f+1,f+len+1,a[i])-f]=a[i];
//		}
//	}
//	cout<<len<<endl;
}

B.天色天歌天籁音

写的普通莫队,赛时我在大黄旁边看他写分块,数组开了快一个 G,给我急死了

这是用莫队求区间众数的基本套路:先开一个桶,再开一个桶的桶,用来记录什么时候该转移了. 可以注意到莫队每次进行转移都只会至多使一个桶的值变化 \(1\),因此若当前数量的桶的数量为零,则说明需要给众数减一,否则,若新加入的桶的数量是更大的,则更新最优答案.

还是说一下这个题为什么是区间众数吧,注意到我们每找出一个上升子序列,都会贡献 \(1\) 的答案. 因此实际上这个题的目标是让我们将原数列分成若干单调递增的子序列,最小化切分总数量. 因此可以考虑每次都贪心地从小到大能拿就拿. 最后剩下的一定是出现次数最多的,即只有众数会对答案有影响.

#include<bits/stdc++.h>
using namespace std;
int n,m;
int len,locate[250001];
struct ques{
	int id,l,r;
	bool operator <(const ques &A)const{
		if(locate[l]!=locate[A.l]) return locate[l]<locate[A.l];
		if(locate[l]&1) return r<A.r;
		return r>A.r;
	}
}q[250001];
int a[250001],cnt[250001],totcnt[250001],ans;
map<int,int>mp;int mpcnt=0;
int nowl,nowr;
int anss[250001];
inline void prew(){
	len=sqrt(m);
	if(!m) len=sqrt(n);
	for(int i=1;i<=n;++i){
		locate[i]=i/len+(i%len!=0);
	}
}
inline void reduce(int pos){
	totcnt[cnt[a[pos]]]--;
	if(totcnt[cnt[a[pos]]]==0 and ans==cnt[a[pos]]) ans--;
	cnt[a[pos]]--;
//	cout<<"reduce "<<pos<<": "<<ans<<endl;
}
inline void add(int pos){
	cnt[a[pos]]++;
	if(totcnt[cnt[a[pos]]]==0 and ans<cnt[a[pos]]) ans=cnt[a[pos]];
	totcnt[cnt[a[pos]]]++;
//	cout<<"add "<<pos<<": "<<ans<<endl;
}
int main(){
//	freopen("大样例/T2.in","r",stdin);
//	freopen("out.out","w",stdout);
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;++i){
		scanf("%d",&a[i]);
		if(!mp.count(a[i])){
			mp[a[i]]=++mpcnt;
			a[i]=mpcnt;
		}
		else a[i]=mp[a[i]];
	}
	for(int i=1;i<=m;++i){
		scanf("%d %d",&q[i].l,&q[i].r);
		q[i].id=i;
	}
	prew();
	sort(q+1,q+m+1);
	nowl=q[1].l;nowr=q[1].l;
	ans=1;cnt[a[q[1].l]]++;totcnt[cnt[a[q[1].l]]]++;
	for(int i=1;i<=m;++i){
		while(nowl<q[i].l) reduce(nowl++);
		while(nowl>q[i].l) add(--nowl);
		while(nowr<q[i].r) add(++nowr);
		while(nowr>q[i].r) reduce(nowr--);
		anss[q[i].id]=ans;
	}
	for(int i=1;i<=m;++i){
		printf("%d\n",-anss[i]);
	}
}
posted @ 2024-08-08 21:25  HaneDaniko  阅读(82)  评论(14编辑  收藏  举报