思路:贪心。首先能+1的位置肯定先加1。那么考虑什么情况下,+1会使得串d变小。那就是 a[i]+b[i] 等于上一位的数。那么这位数就等于浪费了。第一位肯定是+1的。然后用pre上一位。遍历到 i 的时候,如果+1 会使得和前一位相同,那么就+0,反之+1。
AC代码:
#include<iostream>#define int long long#define mk make_pair#define gcd __gcdusingnamespace std;constdouble eps =1e-10;constint mod =1e9+7;constint N =3e5+7;int n,m,k,t =1,cas =1;int a[N],b[N];
string s,s1,s2,s3;struct node{int x,y;node(){}node(int xx,int yy){
x = xx;
y = yy;}};intqpow(int a,int b){int res =1%mod;int tmp = a%mod;while(b){if(b&1) res = res*tmp%mod;
tmp = tmp*tmp%mod;
b <<=1;}return res;}boolcmp(node a1,node a2){returntrue;}signedmain(){
cin>>t;while(t--){
cin>>n;
cin>>s;int pre =0;for(int i =0; i < n ;i ++){if(s[i]=='1'){if(pre ==2){
cout<<0;
pre =1;}else{
cout<<1;
pre =2;}}else{if(pre ==1){
cout<<0;
pre =0;}else{
cout<<1;
pre =1;}}}
cout<<endl;}}
#include<iostream>#include<bits/stdc++.h>#define int long long#define mk make_pair#define gcd __gcdusingnamespace std;constdouble eps =1e-10;constint mod =1e9+7;constint N =3e3+7;int n,m,k,t =1,cas =1;//int a[N],b[N];
string s,s1,s2,s3;int mark[N];
map<int,int> mp;struct node1{int x,id;}a[N];boolcmp(node1 a1,node1 a2){return a1.x > a2.x;}boolcheck(int x,int j){for(int i =1; i < n ; i ++){if(i == j)continue;if(!mp[a[i].x])continue;if(!mp[x-a[i].x]){returnfalse;}else{if(a[i].x == x-a[i].x && mp[a[i].x]==1){returnfalse;}
mp[a[i].x]--;
mp[x-a[i].x]--;//cout<<x<<" "<<a[i].x<<" "<<x-a[i].x<<endl;
x =max(a[i].x,x-a[i].x);}}returntrue;}voidans(int x,int j){
cout<<x+a[j].x<<endl;
cout<<x<<" "<<a[j].x<<endl;for(int i =1; i < n ; i ++){if(i == j)continue;if(!mp[a[i].x])continue;
mp[a[i].x]--;
mp[x-a[i].x]--;
cout<<a[i].x<<" "<<x-a[i].x<<endl;
x =max(a[i].x,x-a[i].x);}}signedmain(){
cin>>t;while(t--){
mp.clear();
cin>>n;
n *=2;for(int i =0; i < n ; i ++){
cin>>a[i].x;
a[i].id = i;
mp[a[i].x]++;}sort(a,a+n,cmp);int sel =-1;for(int j =1; j < n ; j ++){
mp[a[j].x]--;if(check(a[0].x,j)){
sel = j;break;}
mp.clear();for(int i =1; i < n ; i ++){
mp[a[i].x]++;}}
mp.clear();for(int i =1; i < n ; i ++){
mp[a[i].x]++;}if(sel !=-1){
cout<<"YES"<<endl;
mp[a[sel].x]--;ans(a[0].x,sel);}else{
cout<<"NO"<<endl;}}}
#include<iostream>#include<bits/stdc++.h>#define int long long#define mk make_pair#define gcd __gcdusingnamespace std;constdouble eps =1e-10;constint mod =1e9+7;constint N =3e5+7;int n,m,k,t =1,cas =1;int a[N],b[N];int sel =0;int pre[N];int suf[N];signedmain(){
cin>>t;while(t--){
cin>>n;for(int i =1; i <= n ;i ++)
cin>>a[i];for(int i=1; i <= n ; i ++){if(pre[i-1]==-1|| a[i]< pre[i-1]){
pre[i]=-1;}else{
pre[i]= a[i]-pre[i-1];}}
suf[n+1]=0;for(int i = n ; i >=1; i --){if(suf[i+1]==-1|| a[i]< suf[i+1]){
suf[i]=-1;}else{
suf[i]= a[i]-suf[i+1];}}int res =0;if(pre[n]==0) res =1;for(int i =1; i < n ; i ++){if(pre[i-1]==-1|| suf[i+2]==-1)continue;else{// swap a[i] with a[i-1]if(a[i]>= suf[i+2]&& a[i+1]>= pre[i-1]&&
a[i+1]-pre[i-1]== a[i]-suf[i+2]){
res =1;break;}}}puts(res ?"YES":"NO");
cas++;}}