水题加训
libohan0905 · · 个人记录
Vasya And Array
给你若干个限制:
- 指定区间非降序
- 指定区间不是非降序
报告可行性并构造,数据
| 构造也不难,如果这个点编号为零或者相邻不同就增加,否则不变即可。 |
|---|
::::success[代码]
// 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 的串,每次操作交换第一个串的元素和第二个串的元素,最少操作使两串相同或报告不可能并输出方案。
::::info[题解] 唐题。
| 已经相同的不用管。两个 ab 或者两个 ba 可以操作两次搞定,显然最优。如果还剩一个 ab 和 ba 再操作三次,否则剩单个 ab 或 ba 说明数量都不一样显然不可能。 |
|---|
::::success[代码]
// 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
给你一个序列,增加最多三个数使其序列的和为异或和的两倍。 ::::info[题解] 唯一做过的。
| 异或这里拆不了位那构造零,然后发现第一个数是序列和,第二个数是现在序列的和做完了。 |
|---|
::::success[代码]
// 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
构造
::::info[题解] 还行,在床上想出来了。
容易发现
| 由于知道当前步后只有右下两个选择,每一斜行 01 染色即可用倒推回走法。 |
|---|
::::success[代码]
// 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
给你一个可重集合的元素,每次操作可以:
- 给区间的元素数量减一(这个区间每个数至少有一)
- 删光一种元素
求最少的操作使集合清空。
::::info[题解]
首先如果
然后 YES。还有减 |
|---|
::::success[代码]
// 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
假设
给你一个字符串
::::info[题解] 出生题目。
若有字母只出现一次那就以此开头做完了。
否则的话,字母数多可以这样:tutttttttttttttttttvuuuuuuuuvvvv
字母数少可以这样:
wxxxxxxwwwwwwwwww
剩下的就是两种颜色互相切换,比较繁琐细节多。
::::
::::success[代码]
// 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;
}
::::