Codeforces Beta Round #25 (Div. 2 Only)
http://codeforces.com/contest/25
A
#include<bits/stdc++.h>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define sqr(x) ((x)*(x))
#define maxn 500005
typedef long long ll;
/*#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
#endif */ int n;
int a[];
map<int,int>mp; int main(){
#ifndef ONLINE_JUDGE
// freopen("1.txt","r",stdin);
#endif
std::ios::sync_with_stdio(false);
int n;
cin>>n;
int d=,s=;
int posd,poss;
for(int i=;i<=n;i++){
cin>>a[i];
if(a[i]%) d++,posd=i;
else s++,poss=i;
}
if(d>s) cout<<poss<<endl;
else cout<<posd<<endl; }
B
模拟
#include<bits/stdc++.h>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define sqr(x) ((x)*(x))
#define maxn 500005
typedef long long ll;
/*#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
#endif */ int n;
int a[];
map<int,int>mp; int main(){
#ifndef ONLINE_JUDGE
// freopen("1.txt","r",stdin);
#endif
std::ios::sync_with_stdio(false);
int n;
cin>>n;
string str;
cin>>str;
int co=;
while(n){
if(n>){
n-=;
cout<<str[co]<<str[co+]<<'-';
co+=;
}
else{
if(n==)
cout<<str[co]<<str[co+]<<str[co+]<<endl;
else
cout<<str[co]<<str[co+]<<endl;
n=;
}
}
}
C
用floyd跑,类似DP的思想
#include<bits/stdc++.h>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define sqr(x) ((x)*(x))
#define maxn 500005
typedef long long ll;
/*#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
#endif */ ll dp[][]; int main(){
#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
#endif
//std::ios::sync_with_stdio(false);
int n;
cin>>n;
for(int i=;i<n;i++){
for(int j=;j<n;j++){
cin>>dp[i][j];
}
}
int m;
cin>>m;
ll u,v,c;
while(m--){
cin>>u>>v>>c;
u--,v--;
ll ans=;
for(int i=;i<n;i++){
for(int j=;j<n;j++){
dp[i][j]=min(dp[i][j],min(dp[i][u]+c+dp[v][j],dp[i][v]+c+dp[u][j]));
ans+=dp[i][j];
}
}
cout<<ans/<<" ";
}
}
D
并查集,判断是否成环,成环的话把可以构成环的边删去并找到另一个集合把他们连起来
#include<bits/stdc++.h>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define sqr(x) ((x)*(x))
#define maxn 500005
typedef long long ll;
/*#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
#endif */ int fa[]; int Find(int x){
int r=x,y;
while(x!=fa[x]){
x=fa[x];
}
while(r!=x){
y=fa[r];
fa[r]=x;
r=y;
}
return x;
} bool join(int x,int y){
int xx=Find(x);
int yy=Find(y);
if(xx!=yy){
fa[xx]=yy;
return true;
}
return false;
} int main(){
#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
#endif
//std::ios::sync_with_stdio(false);
int n;
cin>>n;
int u,v;
vector<pair<int,int> >ve;
for(int i=;i<=;i++) fa[i]=i;
for(int i=;i<n;i++){
cin>>u>>v;
if(!join(u,v)) ve.push_back(make_pair(u,v));
}
vector<pair<pair<int,int>,pair<int,int> > >ans;
for(int i=;i<ve.size();i++){
u=ve[i].first;
for(int j=;j<=n;j++){
if(join(u,j)){
ans.push_back(make_pair(make_pair(u,ve[i].second),make_pair(u,j)));
break;
}
}
}
cout<<ans.size()<<endl;
for(int i=;i<ans.size();i++){
cout<<ans[i].first.first<<" "<<ans[i].first.second<<" "<<ans[i].second.first<<" "<<ans[i].second.second<<endl;
}
}
E
字符串hash 找到一个前缀和另一个后缀最长相同的长度,也可以用kmp做
#include<bits/stdc++.h>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define sqr(x) ((x)*(x))
#define maxn 500005
typedef long long ll;
typedef unsigned long long ull;
const ull MOD=;
/*#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
#endif */
int Check(string s1,string s2){
int len=;
if(s1.find(s2)!=-) return s1.length();
if(s2.find(s1)!=-) return s2.length();
int len1=s1.length();
int len2=s2.length();
int ans=len1+len2;
int i=len1-,j=;
ull aa=,bb=;
ull flag=;
// cout<<s1<<" "<<s2<<endl;
while(i>=&&j<len2){
aa=s1[i]*flag+aa;
flag=flag*MOD;
bb=bb*MOD+s2[j];
// cout<<s1[i]<<" "<<s2[j]<<" "<<aa<<" "<<bb<<endl;
if(aa==bb){
len=j+;
}
i--,j++;
}
return ans-len;
} int main(){
#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
#endif
//std::ios::sync_with_stdio(false);
string s1,s2,s3;
cin>>s1>>s2>>s3;
int len1=s1.length(),len2=s2.length(),len3=s3.length();
// cout<<len1<<" "<<len2<<" "<<len3<<" "<<len1+len2+len3<<endl;
int ans=0x3f3f3f3f;
ans=min(ans,(Check(s1,s2)+Check(s2,s3)-len2));
ans=min(ans,Check(s1,s3)+Check(s3,s2)-len3);
ans=min(ans,Check(s2,s1)+Check(s1,s3)-len1);
ans=min(ans,Check(s2,s3)+Check(s3,s1)-len3);
ans=min(ans,Check(s3,s1)+Check(s1,s2)-len1);
ans=min(ans,Check(s3,s2)+Check(s2,s1)-len2);
cout<<ans<<endl;
}