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 }

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 }

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 }

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 }

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 }

原文地址:https://www.cnblogs.com/Fighting-sh/p/10361352.html

时间: 2024-07-31 17:00:58

Codeforces Beta Round #25 (Div. 2 Only)的相关文章

Codeforces Beta Round #91 (Div. 1 Only) E. Lucky Array

E. Lucky Array Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467are not. Petya has an arra

暴力/DP Codeforces Beta Round #22 (Div. 2 Only) B. Bargaining Table

题目传送门 1 /* 2 题意:求最大矩形(全0)的面积 3 暴力/dp:每对一个0查看它左下的最大矩形面积,更新ans 4 注意:是字符串,没用空格,好事多磨,WA了多少次才发现:( 5 详细解释:http://www.cnblogs.com/cszlg/p/3217478.html 6 */ 7 #include <cstdio> 8 #include <algorithm> 9 #include <cstring> 10 #include <cmath>

图论/暴力 Codeforces Beta Round #94 (Div. 2 Only) B. Students and Shoelaces

题目传送门 1 /* 2 图论/暴力:这是个连通的问题,每一次把所有度数为1的砍掉,把连接的点再砍掉,总之很神奇,不懂:) 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 #include <cmath> 8 using namespace std; 9 10 const int MAXN = 1e2 + 10; 11 const int INF = 0x3f3f3

BFS Codeforces Beta Round #94 (Div. 2 Only) C. Statues

题目传送门 1 /* 2 BFS:三维BFS,坐标再加上步数,能走一个点当这个地方在步数内不能落到.因为雕像最多8步就会全部下落, 3 只要撑过这个时间就能win,否则lose 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <queue> 8 #include <vector> 9 #include <cstring> 10 using namespace std; 11 1

水题 Codeforces Beta Round #70 (Div. 2) A. Haiku

题目传送门 1 /* 2 水题:三个字符串判断每个是否有相应的元音字母,YES/NO 3 下午网速巨慢:( 4 */ 5 #include <cstdio> 6 #include <cstring> 7 #include <string> 8 #include <iostream> 9 #include <algorithm> 10 #include <cmath> 11 using namespace std; 12 13 cons

Codeforces Beta Round #6 (Div. 2 Only)

Codeforces Beta Round #6 (Div. 2 Only) 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 1000010 7 typedef long long ll; 8 /*#ifndef

Codeforces Beta Round #9 (Div. 2 Only)

Codeforces Beta Round #9 (Div. 2 Only) http://codeforces.com/contest/9 A gcd水题 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 1000010 7

Codeforces Beta Round #12 (Div 2 Only)

Codeforces Beta Round #12 (Div 2 Only) http://codeforces.com/contest/12 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 1000010 7 t

Codeforces Beta Round #14 (Div. 2)

Codeforces Beta Round #14 (Div. 2) http://codeforces.com/contest/14 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