跳到正文

个人记录

水题加训

libohan0905约 21 分钟
原文
文章信息与版本

文章信息

发布
2026年10月1日
收录
2026年10月1日
更新
2026年11月9日
篇幅
约 21 分钟

版本

  1. v1当前

Vasya And Array

给你若干个限制:

  • 指定区间非降序
  • 指定区间不是非降序

报告可行性并构造,数据 10001000(仁慈)

题解

数据小就暴力呗,先把所有区间按左端点排序。非降序区间就将整段标记,编号就是最左端点的编号。可行性就直接扫一遍区间有没有零或者相邻的值不同。

构造也不难,如果这个点编号为零或者相邻不同就增加,否则不变即可。

代码
C++
// Problem: C. Vasya And Array
// Contest: Codeforces - Educational Codeforces Round 67 (Rated for Div. 2)
// URL: https://codeforces.com/problemset/problem/1187/C
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
const int N=5e5+5;
int n,m,o[N],ans[N];
struct node{
	int op,l,r;
}a[N];
bool cmp(node x,node y){
	if(x.op^y.op) return x.op>y.op;
	return x.l<y.l;
}
signed main(){
	ios::sync_with_stdio(0);cin.tie(0);
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		cin>>a[i].op>>a[i].l>>a[i].r;
	}
	sort(a+1,a+m+1,cmp);
	bool flag=1;int t=0;
	for(int i=1;i<=m;i++){
		if(a[i].op){
			int now=o[a[i].l];
			if(!now) now=++t;
			for(int k=a[i].l;k<=a[i].r;k++) o[k]=now;
		}
		else{
			bool f=0;
			for(int k=a[i].l;k<=a[i].r;k++)
				if(!o[k]) {f=1;break;}
			for(int k=a[i].l+1;k<=a[i].r;k++){
				if(o[k]^o[k-1]) {f=1;break;}
			}
			flag=f;
			if(!flag) break;
		}
	}
	ans[n]=1;
	for(int i=n-1;i>=1;i--){
		ans[i]=ans[i+1];
		if(o[i]!=o[i+1]||o[i]==0) ++ans[i];
	}
	if(flag) {
		cout<<"YES"<<endl;
		for(int i=1;i<=n;i++) cout<<ans[i]<<" ";
	}
	else cout<<"NO"<<endl;
	return 0;
}
 

Swap Letters

两个只有 ab 的串,每次操作交换第一个串的元素和第二个串的元素,最少操作使两串相同或报告不可能并输出方案。2×1052\times10^5。

题解

唐题。

已经相同的不用管。两个 ab 或者两个 ba 可以操作两次搞定,显然最优。如果还剩一个 ab 和 ba 再操作三次,否则剩单个 ab 或 ba 说明数量都不一样显然不可能。

代码
C++
// Problem: C. Swap Letters
// Contest: Codeforces - Codeforces Round 585 (Div. 2)
// URL: https://codeforces.com/problemset/problem/1215/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
const int N=5e5+5;
int n,m,a[N];
string s,t;
vector<pair<int,int>>v;
signed main(){
	// ios::sync_with_stdio(0);cin.tie(0);
	cin>>n;
	cin>>s>>t;
	int l=0,r=0;
	for(int i=0;i<n;i++){
		if(s[i]=='a'&&t[i]=='b'){
			if(l){
				v.push_back({l,i+1});
				l=0;
			}
			else l=i+1;
		}
		if(s[i]=='b'&&t[i]=='a'){
			if(r){
				v.push_back({r,i+1});
				r=0;
			}
			else r=i+1;
		}
	}
	if(l&&r){
		v.push_back({l,l});
		v.push_back({l,r});
		l=r=0;
	}
	if(l||r){
		cout<<"-1"<<endl;
		return 0;
	}
	printf("%d\n",v.size());
	for(int i=0;i<v.size();i++) printf("%d %d\n",v[i].first,v[i].second);
	return 0;
}
 

Make Good

给你一个序列,增加最多三个数使其序列的和为异或和的两倍。

题解

唯一做过的。

异或这里拆不了位那构造零,然后发现第一个数是序列和,第二个数是现在序列的和做完了。

代码
C++
// LUOGU_RID: 134329663
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+(ch^48);ch=getchar();}
	return x*f;
}
const int N=2e5+5;
int n,a[N];
int main(){
	int T=read();
	while(T--){
		n=read();
		ll b,s=0,x=0;
		for(int i=1;i<=n;i++) b=read(),s+=b,x^=b;
		printf("2\n%lld %lld\n",x,s+x);
	}
	return 0;
}

Subset with Zero Sum

不会。

Omkar and Duck

构造 n×n,n≤25,0≤ai,j≤1016n\times n,n\le25,0\le a_{i,j}\le 10^{16} 的方阵,给你从左上到右下的路径和,求解路径。

题解

还行,在床上想出来了。

容易发现 22n≤10162^{2n} \le 10^{16},那就每个点乘上 2i+j2^{i+j} 的系数,可以确定在每一步的数据。

由于知道当前步后只有右下两个选择,每一斜行 01 染色即可用倒推回走法。

代码
C++
// Problem: E. Omkar and Duck
// Contest: Codeforces - Codeforces Global Round 10
// URL: https://codeforces.com/problemset/problem/1392/E
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
const int N=5e5+5;
int n,m,a[N];
ll mp[33][33];
signed main(){
	ios::sync_with_stdio(0);cin.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			mp[i][j]=(i&1)?0:1ll<<(i+j-2);
			cout<<mp[i][j]<<" ";
		}
		cout<<endl;
		cout.flush();
	}
	cin>>m;
	while(m--){
		ll o,t=1;cin>>o;
		int x=1,y=1;
		cout<<x<<" "<<y<<endl;
		cout.flush();
		for(;t<n+n-1;t++){
			if(x==n) ++y;
			else if(y==n) ++x;
			else{
				if(o&(1ll<<t)){
					if(mp[x][y+1]) ++y;
					else ++x;
				}
				else{
					if(mp[x][y+1]) ++x;
					else ++y;
				}
			}
			cout<<x<<" "<<y<<endl;
			cout.flush();
		}
	}
	return 0;
}
 

Clear the Multiset

给你一个可重集合的元素,每次操作可以:

  • 给区间的元素数量减一(这个区间每个数至少有一)
  • 删光一种元素

求最少的操作使集合清空。

50005000。

题解

比较扎实的 dp 题。

考虑 fi,jf_{i,j} 为前 ii 个数第 i−1i-1 个数进行 jj 次操作一。

可以转移 fi,0f_{i,0} 为 i−1i-1 中最小的数字加一,其他转移看前一个的 jj 和他的大小,这里可以分类讨论并且预处理。

代码
C++
// Problem: E. Clear the Multiset
// Contest: Codeforces - Educational Codeforces Round 94 (Rated for Div. 2)
// URL: https://codeforces.com/problemset/problem/1400/E
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
const int inf=1e9;
const int N=5005;
const int L=5000;
int n,m,a[N],f[2][N],g[2][N],h[2][N];
signed main(){
	ios::sync_with_stdio(0);cin.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i],a[i]=min(a[i],L);
	memset(f,0x3f3f3f3f,sizeof(f));
	memset(g,0x3f3f3f3f,sizeof(g));
	memset(h,0x3f3f3f3f,sizeof(h));
	f[1][0]=(a[1]>0);
	for(int i=1;i<=a[1];i++) f[1][i]=a[1]?i:0;
	for(int i=1;i<a[1];i++) f[1][i]++;
	g[1][0]=f[1][0];
	for(int j=1;j<=a[1];j++) {
		g[1][j]=min(g[1][j-1],f[1][j]-j);
	}
	h[1][a[1]]=f[1][a[1]];
	for(int j=a[1]-1;j>=0;j--){
		h[1][j]=min(h[1][j+1],f[1][j]);
	}
	int c=0;
	for(int i=2;i<=n;i++,c^=1){
		for(int j=0;j<=a[i];j++) f[c][j]=inf;
		for(int j=0;j<=a[i-1];j++) f[c][0]=min(f[c][0],f[c^1][j]+(a[i]>0));
		for(int j=1;j<=a[i];j++){
			int t=min(j-1,a[i-1]);
			f[c][j]=g[c^1][t]+j;
			// for(int k=0;k<=j-1&&k<=a[i-1];k++){
				// f[i][j]=min(f[i][j],f[i-1][k]+j-k);
			// }
			if(j<=a[i-1]){
				f[c][j]=min(f[c][j],h[c^1][j]);
			}
			// for(int k=j;k<=a[i-1];k++){
				// f[i][j]=min(f[i][j],f[i-1][k]);
			// }
			
			if(j<a[i]) f[c][j]+=(a[i]>0);
		}
		
		g[c][0]=f[c][0];
		for(int j=1;j<=a[i];j++) {
			g[c][j]=min(g[c][j-1],f[c][j]-j);
		}
		h[c][a[i]]=f[c][a[i]];
		for(int j=a[i]-1;j>=0;j--){
			h[c][j]=min(h[c][j+1],f[c][j]);
		}
		// for(int j=0;j<=a[i];j++) printf("[%d,%d]:%d\n",i,j,f[c][j]);
	}
	// for(int i=1;i<=n;i++){
		// for(int j=0;j<=a[i];j++) printf("[%d,%d]:%d\n",i,j,f[i][j]);
	// }
	int ans=L;
	for(int i=0;i<=a[n];i++) ans=min(ans,f[c^1][i]);
	cout<<ans<<endl;
	return 0;
}
 

Water Level

饮水机里的水正好是 kk 升。每天要使用 xx 升水。每天开始时,可以加入恰好 yy 升水,但在任何时候,冷藏箱中的水量都必须在 [l,r][l,r] 的范围内。

是否能在 tt 天内将水位保持在必要的水平。

k,y,l,r,t≤1018,x≤106k,y,l,r,t\le10^{18},x\le10^6

题解

首先如果 x≥yx\geq y 第一天如果能倒就倒,后面每天肯定可以倒,然后除法算一下就好。

然后 x<yx<y 的情况,我们考虑  mod x\bmod x 的意义下,每次倒水都会改变,所以我们尽量等快没水了再倒,然后如果回到访问过的说明无限了可以 YES。还有减 xx 的时候判一下会不会炸。

代码
C++
// Problem: E. Water Level
// Contest: Codeforces - Codeforces Round 689 (Div. 2, based on Zed Code Competition)
// URL: https://codeforces.com/problemset/problem/1461/E
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
const int N=2e6+5;
int n,m,vis[N];
ll k,l,r,t,x,y;
signed main(){
	ios::sync_with_stdio(0);cin.tie(0);
	cin>>k>>l>>r>>t>>x>>y;
	ll o=l/x;
	l-=o*x,r-=o*x,k-=o*x;
	// printf("%lld[%lld %lld] %lld %lld %lld\n",k,l,r,t,x,y);
	bool flag=1;o=k;
	if(x>=y){
		if(o+y<=r) o+=y;
		o-=x;if(o<l) flag=0;
		--t;
		if(x!=y&&t>(o-l)/(x-y)) flag=0;
	}
	else while(flag&&t>0){
		if(o+y<=r) o+=y;
		ll now=o%x;
		ll c=o/x;
		if(now<l){
			--c;now+=x;
		}
		if(!c){flag=0;break;}
		// printf("%lld now:%lld c:%lld\n",o,now,c);
		t-=c;
		if(t<=0) break;
		if(vis[now]) break;
		vis[now]=1;
		o=now;
	}
	if(flag) cout<<"Yes"<<endl;
	else cout<<"No"<<endl;
	return 0;
}
 

Minimax

假设 f(t)f(t) 等于字符串 tt 在所有位置上的前缀函数(最长前后缀相同且不为原串的长度)的最大值。

给你一个字符串 ss。任意重新排列字符,得到字符串 tt。在最小化 f(t)f(t) 的所有选项中,请输出字符串 tt 在字典序最小的选项。

题解

出生题目。

若有字母只出现一次那就以此开头做完了。

否则的话,字母数多可以这样:tutttttttttttttttttvuuuuuuuuvvvv 字母数少可以这样: wxxxxxxwwwwwwwwww 剩下的就是两种颜色互相切换,比较繁琐细节多。

代码
C++
// Problem: E. Minimax
// Contest: Codeforces - Codeforces Round 733 (Div. 1 + Div. 2, based on VK Cup 2021 - Elimination (Engine))
// URL: https://codeforces.com/problemset/problem/1530/E
// Memory Limit: 512 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
const int N=5e5+5;
int n,m,id,idx,a[N],op;
string s;
signed main(){
	// ios::sync_with_stdio(0);cin.tie(0);
	int T=1;
	cin>>T;
	while(T--){
		cin>>s;
		n=s.length();
		for(int i='a';i<='z';i++) a[i]=0;
		int col=0;
		for(int i=0;i<n;i++) {
			if(!a[s[i]]) ++col;
			++a[s[i]];
		}
		id=idx=0;
		for(int i='a';i<='z';i++) if(a[i]==1){
			idx=i;break;
		}
		if(idx){
			char x=idx;cout<<x;--a[idx];
			for(int i='a';i<='z';i++){
				while(a[i]--){
					char y=i;cout<<y;
				}
			}
			cout<<endl;
			continue;
		}
		for(int i='a';i<='z';i++) if(a[i]){
			id=i;break;
		}
		if(col==2||n-a[id]>=a[id]-2){
			for(int i=id+1;i<='z';i++) if(a[i]){
				idx=i;break;
			}
			char x=id,y=idx;
			cout<<x;--a[id];
			if(col==2&&a[id]>a[idx]+1){
				while(a[idx]--) cout<<y;
				while(a[id]--) cout<<x;
				cout<<endl;
				continue;
			}
			while(a[id]||a[idx]){
				if(a[id]) cout<<x,--a[id];
				if(!a[idx]) {
					for(int i=idx+1;i<='z';i++) if(a[i]&&i!=id){
						y=i;idx=i;break;
					} 
				}
				if(a[idx]) cout<<y,--a[idx];
			}
			for(int i='a';i<='z';i++){
				while(a[i]--){
					char z=i;cout<<z;
				}
			}
			cout<<endl;
			continue;
		}
		char x=id,y=id;cout<<x;--a[id];
		for(int i='a';i<='z';i++) if(a[i]&&i!=id) {
			y=idx=i;break;
		}
		if(idx) cout<<y,--a[idx];int lst=idx;
		for(int i='a';i<='z';i++) while(a[i]){
			if(lst==id&&i==idx){
				int t='a';for(;t<='z';t++) if(a[t]&&t!=i){break;}
				char z=t;cout<<z;--a[t];lst=t;
			}
			else{
				char z=i;cout<<z;--a[i];lst=i;
			}
		}
		cout<<endl;
	}
	return 0;
}
原文