Codeforces Beta Round #31 (Div. 2, Codeforces format)

Codeforces Beta Round #31 (Div. 2, Codeforces format)

http://codeforces.com/contest/31

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 pb push_back
 7 #define maxn 1000005
 8 typedef long long ll;
 9 typedef unsigned long long ull;
10 /*#ifndef ONLINE_JUDGE
11         freopen("1.txt","r",stdin);
12 #endif */
13 
14 int a[105];
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     for(int i=1;i<=n;i++) cin>>a[i];
24     for(int i=1;i<=n;i++){
25         for(int j=1;j<=n;j++){
26             for(int k=1;k<=n;k++){
27                 if(i!=j&&j!=k){
28                     if(a[i]==a[j]+a[k]){
29                         cout<<i<<" "<<j<<" "<<k<<endl;
30                         return 0;
31                     }
32                 }
33             }
34         }
35     }
36     cout<<-1<<endl;
37 }
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 pb push_back
 7 #define maxn 1000005
 8 typedef long long ll;
 9 typedef unsigned long long ull;
10 /*#ifndef ONLINE_JUDGE
11         freopen("1.txt","r",stdin);
12 #endif */
13 
14 int a[105];
15 
16 int Check(string str){
17     int pre=-1;
18     int pos=-1;
19     for(int i=0;i<str.length();i++){
20         if(str[i]=='@'){
21             pos=i;
22             if(pre<i-1&&i+1<str.length()){
23                 pre=i+1;
24             }
25             else{
26                 return -1;
27             }
28         }
29     }
30     return pos;
31 }
32 
33 int main(){
34     #ifndef ONLINE_JUDGE
35      //   freopen("1.txt","r",stdin);
36     #endif
37     std::ios::sync_with_stdio(false);
38     string str;
39     cin>>str;
40     int pos=Check(str);
41     if(pos!=-1){
42         for(int i=0;i<str.length();i++){
43             cout<<str[i];
44             if(str[i]=='@'){
45 
46                 if(pos!=i){
47                     cout<<str[i+1];
48                     i++;
49 
50                     if(i<str.length()-1){
51                         cout<<',';
52                     }
53                 }
54             }
55         }
56     }
57     else cout<<"No solution"<<endl;
58 }
View Code

 

C

暴力

 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 pb push_back
 7 #define maxn 1000005
 8 typedef long long ll;
 9 typedef unsigned long long ull;
10 /*#ifndef ONLINE_JUDGE
11         freopen("1.txt","r",stdin);
12 #endif */
13 struct sair{
14     int s,e,pos;
15 }a[5005];
16 
17 bool cmp(sair a,sair b){
18     if(a.s==b.s) return a.e<b.e;
19     return a.s<b.s;
20 }
21 
22 int main(){
23     #ifndef ONLINE_JUDGE
24      //   freopen("1.txt","r",stdin);
25     #endif
26     std::ios::sync_with_stdio(false);
27     int n;
28     cin>>n;
29     for(int i=0;i<n;i++){
30         cin>>a[i].s>>a[i].e;
31         a[i].pos=i+1;
32     }
33     sort(a,a+n,cmp);
34     vector<int>ans;
35     int flag;
36     for(int i=0;i<n;i++){
37         int pre=-1;
38         flag=0;
39         for(int j=0;j<n;j++){
40             if(i!=j){
41                 if(pre==-1){
42                     pre=a[j].e;
43                 }
44                 else{
45                     if(pre>a[j].s){
46                         flag=1;
47                     }
48                     else{
49                         pre=a[j].e;
50                     }
51                 }
52             }
53         }
54         if(!flag){
55             ans.pb(a[i].pos);
56         }
57     }
58     cout<<ans.size()<<endl;
59     sort(ans.begin(),ans.end());
60     for(int i=0;i<ans.size();i++){
61         cout<<ans[i]<<" ";
62     }
63 }
View Code

 

D

bfs求连通块,模拟剪纸的过程

 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 pb push_back
 7 #define maxn 1000005
 8 typedef long long ll;
 9 typedef unsigned long long ull;
10 /*#ifndef ONLINE_JUDGE
11         freopen("1.txt","r",stdin);
12 #endif */
13 
14 int n,m,k;
15 int book[505][505];
16 int dir[4][2]={0,1,1,0,0,-1,-1,0};
17 
18 int bfs(int x,int y){
19     queue<pair<int,int> >Q;
20     int ans=1;
21     book[x][y]=1;
22     Q.push(make_pair(x,y));
23     pair<int,int>p;
24     while(!Q.empty()){
25         p=Q.front();
26         Q.pop();
27         for(int i=0;i<4;i++){
28             int xx=p.first+dir[i][0];
29             int yy=p.second+dir[i][1];
30             if(xx>=0&&xx<=2*n&&yy>=0&&yy<=2*m&&!book[xx][yy]){
31                 book[xx][yy]=1;
32                 if((xx%2)&&(yy%2)){
33                     ans++;
34                 }
35                 Q.push(make_pair(xx,yy));
36             }
37         }
38     }
39     return ans;
40 }
41 
42 int main(){
43     #ifndef ONLINE_JUDGE
44         freopen("1.txt","r",stdin);
45     #endif
46     std::ios::sync_with_stdio(false);
47     cin>>n>>m>>k;
48     int a,b,c,d;
49     while(k--){
50         cin>>a>>b>>c>>d;///边为偶数
51         a*=2,b*=2,c*=2,d*=2;
52         if(a==c){
53             if(b>d) swap(b,d);
54             for(int i=b;i<=d;i++){
55                 book[a][i]=1;
56             }
57         }
58         else{
59             if(a>c) swap(a,c);
60             for(int i=a;i<=c;i++){
61                 book[i][b]=1;
62             }
63         }
64     }
65     vector<int>ans;
66     for(int i=1;i<=2*n;i++){
67         for(int j=1;j<=2*m;j++){
68             if(!book[i][j]&&(i%2)&&(j%2)){
69                 ans.pb(bfs(i,j));
70             }
71         }
72     }
73     sort(ans.begin(),ans.end());
74     for(int i=0;i<ans.size();i++){
75         cout<<ans[i]<<" ";
76     }
77     cout<<endl;
78 }
View Code

 

E

题意:给2*n个数字,A和B各选n个各组成一个数,使得A+B的和最大

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 pb push_back
 7 #define maxn 1000005
 8 typedef long long ll;
 9 typedef unsigned long long ull;
10 /*#ifndef ONLINE_JUDGE
11         freopen("1.txt","r",stdin);
12 #endif */
13 
14 ll dp[25][25];
15 ll p[25];
16 
17 int main(){
18     #ifndef ONLINE_JUDGE
19       //  freopen("1.txt","r",stdin);
20     #endif
21     std::ios::sync_with_stdio(false);
22     int n;
23     string str;
24     cin>>n;
25     cin>>str;
26     int len=2*n;
27     p[0]=1;
28     for(int i=1;i<=18;i++){
29         p[i]=p[i-1]*10;
30     }
31     ///dp[i][j] 表示A选了i个,B选了j个,dp[i][j]记录的是A+B的和
32     for(int i=0;i<=n;i++){
33         for(int j=0;j<=n;j++){
34             ll tmp=str[2*n-i-j]-'0';///从后向前推
35             if(i){
36                 dp[i][j]=dp[i-1][j]+tmp*p[i-1];
37             }
38             if(j){
39                 dp[i][j]=max(dp[i][j],dp[i][j-1]+tmp*p[j-1]);
40             }
41             cout<<dp[i][j]<<endl;
42         }
43     }
44     int i=n,j=n;
45     while(i||j){
46         ll tmp=str[2*n-i-j]-'0';
47         if(i>0&&(tmp*p[i-1]+dp[i-1][j]==dp[i][j])){
48             cout<<'H';
49             i--;
50         }
51         else{
52             cout<<'M';
53             j--;
54         }
55     }
56     cout<<endl;
57 }
View Code

 

posted on 2019-02-15 12:12  Fighting_sh  阅读(174)  评论(0编辑  收藏  举报

导航