Codeforces Beta Round #25 (Div. 2 Only)

Codeforces Beta Round #25 (Div. 2 Only)

http://codeforces.com/contest/25

A

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,mid,rt<<1
 4 #define rson mid+1,r,rt<<1|1
 5 #define sqr(x) ((x)*(x))
 6 #define maxn 500005
 7 typedef long long ll;
 8 /*#ifndef ONLINE_JUDGE
 9         freopen("1.txt","r",stdin);
10 #endif */
11 
12 int n;
13 int a[105];
14 map<int,int>mp;
15 
16 int main(){
17     #ifndef ONLINE_JUDGE
18   //      freopen("1.txt","r",stdin);
19     #endif
20     std::ios::sync_with_stdio(false);
21     int n;
22     cin>>n;
23     int d=0,s=0;
24     int posd,poss;
25     for(int i=1;i<=n;i++){
26         cin>>a[i];
27         if(a[i]%2) d++,posd=i;
28         else s++,poss=i;
29     }
30     if(d>s) cout<<poss<<endl;
31     else cout<<posd<<endl;
32 
33 }
View Code

 

B

模拟

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,mid,rt<<1
 4 #define rson mid+1,r,rt<<1|1
 5 #define sqr(x) ((x)*(x))
 6 #define maxn 500005
 7 typedef long long ll;
 8 /*#ifndef ONLINE_JUDGE
 9         freopen("1.txt","r",stdin);
10 #endif */
11 
12 int n;
13 int a[105];
14 map<int,int>mp;
15 
16 int main(){
17     #ifndef ONLINE_JUDGE
18   //      freopen("1.txt","r",stdin);
19     #endif
20     std::ios::sync_with_stdio(false);
21     int n;
22     cin>>n;
23     string str;
24     cin>>str;
25     int co=0;
26     while(n){
27         if(n>3){
28             n-=2;
29             cout<<str[co]<<str[co+1]<<'-';
30             co+=2;
31         }
32         else{
33             if(n==3)
34                 cout<<str[co]<<str[co+1]<<str[co+2]<<endl;
35             else
36                 cout<<str[co]<<str[co+1]<<endl;
37             n=0;
38         }
39     }
40 }
View Code

 

C

用floyd跑,类似DP的思想

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,mid,rt<<1
 4 #define rson mid+1,r,rt<<1|1
 5 #define sqr(x) ((x)*(x))
 6 #define maxn 500005
 7 typedef long long ll;
 8 /*#ifndef ONLINE_JUDGE
 9         freopen("1.txt","r",stdin);
10 #endif */
11 
12 ll dp[305][305];
13 
14 int main(){
15     #ifndef ONLINE_JUDGE
16         freopen("1.txt","r",stdin);
17     #endif
18     //std::ios::sync_with_stdio(false);
19     int n;
20     cin>>n;
21     for(int i=0;i<n;i++){
22         for(int j=0;j<n;j++){
23             cin>>dp[i][j];
24         }
25     }
26     int m;
27     cin>>m;
28     ll u,v,c;
29     while(m--){
30         cin>>u>>v>>c;
31         u--,v--;
32         ll ans=0;
33         for(int i=0;i<n;i++){
34             for(int j=0;j<n;j++){
35                 dp[i][j]=min(dp[i][j],min(dp[i][u]+c+dp[v][j],dp[i][v]+c+dp[u][j]));
36                 ans+=dp[i][j];
37             }
38         }
39         cout<<ans/2<<" ";
40     }
41 }
View Code

 

D

并查集,判断是否成环,成环的话把可以构成环的边删去并找到另一个集合把他们连起来

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,mid,rt<<1
 4 #define rson mid+1,r,rt<<1|1
 5 #define sqr(x) ((x)*(x))
 6 #define maxn 500005
 7 typedef long long ll;
 8 /*#ifndef ONLINE_JUDGE
 9         freopen("1.txt","r",stdin);
10 #endif */
11 
12 int fa[1005];
13 
14 int Find(int x){
15     int r=x,y;
16     while(x!=fa[x]){
17         x=fa[x];
18     }
19     while(r!=x){
20         y=fa[r];
21         fa[r]=x;
22         r=y;
23     }
24     return x;
25 }
26 
27 bool join(int x,int y){
28     int xx=Find(x);
29     int yy=Find(y);
30     if(xx!=yy){
31         fa[xx]=yy;
32         return true;
33     }
34     return false;
35 }
36 
37 int main(){
38     #ifndef ONLINE_JUDGE
39         freopen("1.txt","r",stdin);
40     #endif
41     //std::ios::sync_with_stdio(false);
42     int n;
43     cin>>n;
44     int u,v;
45     vector<pair<int,int> >ve;
46     for(int i=0;i<=1001;i++) fa[i]=i;
47     for(int i=1;i<n;i++){
48         cin>>u>>v;
49         if(!join(u,v)) ve.push_back(make_pair(u,v));
50     }
51     vector<pair<pair<int,int>,pair<int,int> > >ans;
52     for(int i=0;i<ve.size();i++){
53         u=ve[i].first;
54         for(int j=1;j<=n;j++){
55             if(join(u,j)){
56                 ans.push_back(make_pair(make_pair(u,ve[i].second),make_pair(u,j)));
57                 break;
58             }
59         }
60     }
61     cout<<ans.size()<<endl;
62     for(int i=0;i<ans.size();i++){
63         cout<<ans[i].first.first<<" "<<ans[i].first.second<<" "<<ans[i].second.first<<" "<<ans[i].second.second<<endl;
64     }
65 }
View Code

 

E

字符串hash 找到一个前缀和另一个后缀最长相同的长度,也可以用kmp做

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,mid,rt<<1
 4 #define rson mid+1,r,rt<<1|1
 5 #define sqr(x) ((x)*(x))
 6 #define maxn 500005
 7 typedef long long ll;
 8 typedef unsigned long long ull;
 9 const ull MOD=257;
10 /*#ifndef ONLINE_JUDGE
11         freopen("1.txt","r",stdin);
12 #endif */
13 int Check(string s1,string s2){
14     int len=0;
15     if(s1.find(s2)!=-1) return s1.length();
16     if(s2.find(s1)!=-1) return s2.length();
17     int len1=s1.length();
18     int len2=s2.length();
19     int ans=len1+len2;
20     int i=len1-1,j=0;
21     ull aa=0,bb=0;
22     ull flag=1;
23    // cout<<s1<<" "<<s2<<endl;
24     while(i>=0&&j<len2){
25         aa=s1[i]*flag+aa;
26         flag=flag*MOD;
27         bb=bb*MOD+s2[j];
28        // cout<<s1[i]<<" "<<s2[j]<<" "<<aa<<" "<<bb<<endl;
29         if(aa==bb){
30             len=j+1;
31         }
32         i--,j++;
33     }
34     return ans-len;
35 }
36 
37 int main(){
38     #ifndef ONLINE_JUDGE
39         freopen("1.txt","r",stdin);
40     #endif
41     //std::ios::sync_with_stdio(false);
42     string s1,s2,s3;
43     cin>>s1>>s2>>s3;
44     int len1=s1.length(),len2=s2.length(),len3=s3.length();
45    // cout<<len1<<" "<<len2<<" "<<len3<<" "<<len1+len2+len3<<endl;
46     int ans=0x3f3f3f3f;
47     ans=min(ans,(Check(s1,s2)+Check(s2,s3)-len2));
48     ans=min(ans,Check(s1,s3)+Check(s3,s2)-len3);
49     ans=min(ans,Check(s2,s1)+Check(s1,s3)-len1);
50     ans=min(ans,Check(s2,s3)+Check(s3,s1)-len3);
51     ans=min(ans,Check(s3,s1)+Check(s1,s2)-len1);
52     ans=min(ans,Check(s3,s2)+Check(s2,s1)-len2);
53     cout<<ans<<endl;
54 }
View Code

 

posted on 2019-02-11 11:44  Fighting_sh  阅读(166)  评论(0编辑  收藏  举报

导航