题目链接:http://codeforces.com/contest/1138/problem
第一题(暴力):
#include<bits/stdc++.h>
using namespace std;
#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long
#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define fi first
#define se second
#define mk(x,y) make_pair(x,y)
const int mod=1e9+7;
const int maxn=1e5+5;
const int INF=1e9;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
int n,x[maxn],ans=0;
int cnt1=0,cnt2=0;
int tmp[maxn],cnt=0;
int main(){
cin>>n;
rep(i,0,n) cin>>x[i];
rep(i,0,n){
cnt1=1;
while(i+1<n&&x[i+1]==x[i]) i++,cnt1++;
tmp[cnt++]=cnt1;
}
rep(i,0,cnt-1){
ans=max(ans,min(tmp[i],tmp[i+1]));
}
cout<<2*ans<<endl;
return 0;
}
第二题(暴力枚举):
#include<bits/stdc++.h>
using namespace std;
#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long
#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define fi first
#define se second
#define mk(x,y) make_pair(x,y)
const int mod=1e9+7;
const int maxn=5e3+5;
const int INF=1e9;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
int n;
string s1,s2;
struct node{
int idx;
};
node p[4][maxn];
int cnt0,cnt1,cnt2,cnt3;
int main(){
cin>>n>>s1>>s2;
rep(i,0,n){
if(s1[i]=='0'&&s2[i]=='1') p[1][cnt1++]=node{i};
else if(s1[i]=='0'&&s2[i]=='0') p[0][cnt0++]=node{i};
else if(s1[i]=='1'&&s2[i]=='0') p[2][cnt2++]=node{i};
else if(s1[i]=='1'&&s2[i]=='1') p[3][cnt3++]=node{i};
}
int flag=1;
for(int i=0;i<=cnt2&&flag;i++) for(int j=0;j<=cnt3&&flag;j++){
int tp1=cnt2-i,tp2=cnt3-j;
int tp3=i+j-tp2;///取(0,1)的数量
if(tp3<0||tp3>cnt1) continue;
tp3=cnt1-tp3;
if(tp3+i+j>n/2) continue;
int tp4=n/2-i-j-tp3;
if(tp4>cnt0||tp4<0) continue;
flag=0;
for(int tp=0;tp<i;tp++) cout<< p[2][tp].idx+1<<" ";
for(int tp=0;tp<j;tp++) cout<< p[3][tp].idx+1<<" ";
for(int tp=0;tp<tp3;tp++) cout<< p[1][tp].idx+1<<" ";
for(int tp=0;tp<tp4;tp++) cout<< p[0][tp].idx+1<<" ";
}
if(flag) puts("-1");
return 0;
}
第三题(暴力+离散化):
#include<bits/stdc++.h>
using namespace std;
#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long
#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define fi first
#define se second
#define mk(x,y) make_pair(x,y)
const int mod=1e9+7;
const int maxn=1e3+5;
const int INF=1e9;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
int n,m;
int a[maxn][maxn],b[maxn][maxn],c[maxn][maxn];
int idx1[maxn],idx2[maxn];
int main(){
scanf("%d%d",&n,&m);
rep(i,0,n) rep(j,0,m) scanf("%d",&a[i][j]),b[i][j]=a[i][j],c[j][i]=a[i][j];
rep(i,0,n){
sort(b[i],b[i]+m);
idx1[i]=unique(b[i],b[i]+m)-b[i];
}
rep(i,0,m){
sort(c[i],c[i]+n);
idx2[i]=unique(c[i],c[i]+n)-c[i];
}
rep(i,0,n){ rep(j,0,m){
int tp1=0,tp2=0;
tp1=lower_bound(b[i],b[i]+idx1[i],a[i][j])-b[i];
tp2=lower_bound(c[j],c[j]+idx2[j],a[i][j])-c[j];
int sz1=idx1[i],sz2=idx2[j];
if(tp1<tp2) printf("%d ",max(sz1-tp1,sz2-tp2)+tp2);
else printf("%d ",max(sz1-tp1,sz2-tp2)+tp1);
/// cout<<tp1<<" "<<tp2<<" "<<sz1<<" "<<sz2<<endl;
}
puts("");
}
return 0;
}
第四题(KMP+贪心):
#include<bits/stdc++.h>
using namespace std;
#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long
#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define fi first
#define se second
#define mk(x,y) make_pair(x,y)
const int mod=1e9+7;
const int maxn=5e5+5;
const int ub=1e6;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
char s1[maxn],s2[maxn];
char suf[maxn];
int cnt[2],nxt[maxn];
void getnxt(char *str){
int len = strlen(str);
int i = 0, j = -1;
nxt[0] = -1;
while(i<len){
if(j==-1 || str[i] == str[j]){
i++,j++;
nxt[i] = j;
}else{
j = nxt[j];
}
}
}
int main(){
scanf("%s%s",s1,s2);
int sz1=strlen(s1),sz2=strlen(s2);
rep(i,0,sz1) cnt[s1[i]-'0']++;
rep(i,0,sz2) cnt[s2[i]-'0']--;
if(cnt[0]<0||cnt[1]<0) puts(s1);
else{
getnxt(s2);
int len=nxt[sz2];
int tp0=0,tp1=0;
rep(i,len,sz2){
suf[i-len]=s2[i];
if(s2[i]=='0') tp0++;
else tp1++;
}
int tp=maxn;
if(tp0) tp=min(cnt[0]/tp0,tp);
if(tp1) tp=min(tp,cnt[1]/tp1);
printf("%s",s2);
rep(i,0,tp) printf("%s",suf);
cnt[0]-=tp*tp0,cnt[1]-=tp*tp1;
rep(i,0,cnt[0]) printf("0");
rep(i,0,cnt[1]) printf("1");
}
return 0;
}