Codeforces Beta Round #55 (Div. 2)

Codeforces Beta Round #55 (Div. 2)

http://codeforces.com/contest/59

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 eb emplace_back
 8 #define maxn 1000005
 9 #define rep(k,i,j) for(int k=i;k<j;k++)
10 typedef long long ll;
11 typedef unsigned long long ull;
12 
13 
14 int main(){
15     #ifndef ONLINE_JUDGE
16        // freopen("input.txt","r",stdin);
17     #endif
18     std::ios::sync_with_stdio(false);
19     string str;
20     cin>>str;
21     int big=0,small=0;
22     for(int i=0;i<str.length();i++){
23         if(str[i]>='A'&&str[i]<='Z'){
24             big++;
25         }
26         else{
27             small++;
28         }
29     }
30     if(big<=small){
31         for(int i=0;i<str.length();i++){
32             if(str[i]>='A'&&str[i]<='Z'){
33                 str[i]+=32;
34             }
35             cout<<str[i];
36         }
37     }
38     else{
39        for(int i=0;i<str.length();i++){
40             if(str[i]>='a'&&str[i]<='z'){
41                 str[i]-=32;
42             }
43             cout<<str[i];
44         } 
45     }
46 }
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 eb emplace_back
 8 #define maxn 1000005
 9 #define rep(k,i,j) for(int k=i;k<j;k++)
10 typedef long long ll;
11 typedef unsigned long long ull;
12 
13 int a[105];
14 
15 int main(){
16     #ifndef ONLINE_JUDGE
17        // freopen("input.txt","r",stdin);
18     #endif
19     std::ios::sync_with_stdio(false);
20     int n;
21     cin>>n;
22     int ans=0;
23     int sum=0;
24     int ji=0x3f3f3f3f,ou=0x3f3f3f3f;
25     for(int i=1;i<=n;i++){
26         cin>>a[i];
27         if(a[i]%2&&a[i]<ji) ji=a[i];
28         if((a[i]%2==0)&&a[i]<ou) ou=a[i];
29         sum+=a[i];
30     }
31     if(sum%2)cout<<sum<<endl;
32     else{
33         if(ji!=0x3f3f3f3f) cout<<sum-ji<<endl;
34         else cout<<0<<endl;
35     }
36 }
View Code

C

模拟,找出前str.length()/2中,str[i]=='?'&&str[str.length()-1-i]=='?'的个数,然后贪心

  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 eb emplace_back
  8 #define maxn 1000005
  9 #define rep(k,i,j) for(int k=i;k<j;k++)
 10 typedef long long ll;
 11 typedef unsigned long long ull;
 12 
 13 
 14 int main(){
 15     #ifndef ONLINE_JUDGE
 16         freopen("input.txt","r",stdin);
 17     #endif
 18     std::ios::sync_with_stdio(false);
 19     int n;
 20     map<char,int>mp;
 21     string str;
 22     cin>>n>>str;
 23     int prex=0,prey=0;
 24     int num=0;
 25     rep(i,0,str.length()){
 26         if(str[i]!='?')
 27             mp[str[i]]=1;
 28     }
 29     int len=str.length()+1;
 30     int L=len-1;
 31     len/=2;
 32     rep(i,0,len){
 33         if(str[i]=='?'&&str[L-i-1]=='?') num++;
 34     }
 35     int k=n-mp.size();
 36     rep(i,0,len){
 37         if(num<k){
 38             cout<<"IMPOSSIBLE"<<endl;
 39             return 0;
 40         }
 41         if(i==L-i-1){
 42             if(str[i]=='?'){
 43                 if(num-1>=k){
 44                     if(mp['a']){
 45                         str[i]='a';
 46                         num--;
 47                     }
 48                     else{
 49                         str[i]='a';
 50                         mp['a']=1;
 51                         num--;
 52                         k--;
 53                     }
 54                 }
 55                 else if(num-1>=k-1){
 56                     int flag=1;
 57                     rep(j,0,26){
 58                         if(mp[char(j+'a')]==0){
 59                             mp[char(j+'a')]=1;
 60                             num--;
 61                             k--;
 62                             str[i]=char(j+'a');
 63                             flag=0;
 64                             break;
 65                         }
 66                     }
 67                     if(flag){
 68                         cout<<"IMPOSSIBLE"<<endl;
 69                         return 0;
 70                     }
 71                 }
 72                 else{
 73                     cout<<"IMPOSSIBLE"<<endl;
 74                     return 0;
 75                 }
 76             }
 77         }
 78         else if(str[i]=='?'&&str[L-i-1]!='?'){
 79             str[i]=str[L-i-1];
 80         }
 81         else if(str[i]!='?'&&str[L-i-1]=='?'){
 82             str[L-i-1]=str[i];
 83         }
 84         else if(str[i]=='?'&&str[L-i-1]=='?'){
 85             prex=i,prey=L-i-1;
 86             if(num-1>=k){
 87                 if(mp['a']){
 88                     str[i]='a';
 89                     str[L-i-1]='a';
 90                     num-=1;
 91                 }
 92                 else{
 93                     mp['a']=1;
 94                     k--;
 95                     num-=1;
 96                     str[i]='a';
 97                     str[L-i-1]='a';
 98                 }
 99             }
100             else if(num-1>=k-1){
101                 int flag=1;
102                 rep(j,0,26){
103                     if(mp[char(j+'a')]==0){
104                         mp[char(j+'a')]=1;
105                         k--;
106                         num-=1;
107                         str[i]=char(j+'a');
108                         str[L-i-1]=char(j+'a');
109                         flag=0;
110                         break;
111                     }
112                 }
113                 if(flag){
114                     cout<<"IMPOSSIBLE"<<endl;
115                     return 0;
116                 }
117             }
118             else{
119                 cout<<"IMPOSSIBLE"<<endl;
120                 return 0;
121             }
122         }
123         else if(str[i]!='?'&&str[L-i-1]!='?'){
124             if(str[i]!=str[L-i-1]){
125                 cout<<"IMPOSSIBLE"<<endl;
126                 return 0;
127             }
128         }
129     }
130    // cout<<k<<endl;
131    // cout<<prex<<" "<<prey<<endl;
132     if(k==1){
133         int flag=1;
134         rep(i,0,26){
135             if(mp[char(i+'a')]==0){
136                 str[prex]=char(i+'a');
137                 str[prey]=char(i+'a');
138                 flag=0;
139                 break;
140                 k--;
141             }
142         }
143         if(flag){
144             cout<<"IMPOSSIBLE"<<endl;
145             return 0;
146         }
147     }
148     cout<<str<<endl;
149 }
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 pb push_back
 7 #define eb emplace_back
 8 #define maxn 1000005
 9 #define rep(k,i,j) for(int k=i;k<j;k++)
10 typedef long long ll;
11 typedef unsigned long long ull;
12 
13 int a[300005],b[300005];
14 int book[300005];
15 
16 int main(){
17     #ifndef ONLINE_JUDGE
18         freopen("input.txt","r",stdin);
19     #endif
20     std::ios::sync_with_stdio(false);
21     int n;
22     cin>>n;
23     rep(i,0,3*n) cin>>b[i];
24     rep(i,0,3*n) cin>>a[i];
25     int k;
26     cin>>k;
27     int pos=0,group,pp=0;
28     while(a[pos]!=k) pos++;
29     group=pos/3+1;
30     rep(i,0,3*(group-1)) book[a[i]]=true;
31     rep(i,0,3*n) if(!book[b[i]]){
32         pp=i;break;
33     }
34     if(a[pos]!=b[pp]) sort(a,a+3*n);
35     else{
36         int flag=-1;
37         rep(i,3*(group-1),3*group) if(i!=pos) flag=max(flag,a[i]);
38         sort(a,a+3*group);
39         int pos=0;
40         while(a[pos]!=flag) pos++;
41         sort(a+pos+1,a+3*n);
42     }
43     rep(i,0,3*n) if(a[i]!=k) cout<<a[i]<<" ";
44 }
View Code

E

用bfs找最短路,book[i][j]表示当前走到j,它的前一步是在i位置,然后book[i][j]记录的是它的前一步在队列中的位置

 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 eb emplace_back
 8 #define maxn 1000005
 9 #define rep(k,i,j) for(int k=i;k<j;k++)
10 typedef long long ll;
11 typedef unsigned long long ull;
12 
13 vector<int>ve[3005];
14 int book[3005][3005];
15 int n,m,k;
16 set< pair<int,pair<int,int> > >se;
17 set< pair<int,pair<int,int> > >::iterator it;
18 
19 struct sair{
20     int pre,now,step;
21 }Q[9000005];
22 
23 int bfs(){
24     sair s,e;
25     s.pre=0,s.now=1,s.step=0;
26     int L=1,R=1;
27     Q[R++]=s;
28     int u;
29     while(L<R){
30         s=Q[L++];
31         rep(i,0,ve[s.now].size()){
32             u=ve[s.now][i];
33             if(!book[s.now][u]){
34                 it=se.find(make_pair(s.pre,make_pair(s.now,u)));
35                 if(it==se.end()){
36                     e.pre=s.now;
37                     e.now=u;
38                     e.step=s.step+1;
39                     book[e.pre][e.now]=L-1;
40                     Q[R++]=e;
41 
42                     if(u==n){
43                         cout<<e.step<<endl;
44                         return R-1;
45                     }
46 
47                 }
48             }
49         }
50     }
51     return 0;
52 }
53 
54 void dfs(int pos){
55     if(pos==0) exit(0);
56     if(pos==1){
57         cout<<Q[pos].now<<" ";
58         return;
59     }
60     dfs(book[Q[pos].pre][Q[pos].now]);
61     cout<<Q[pos].now<<" ";
62 }
63 
64 int main(){
65     #ifndef ONLINE_JUDGE
66         freopen("input.txt","r",stdin);
67     #endif
68     std::ios::sync_with_stdio(false);
69     cin>>n>>m>>k;
70     int u,v;
71     rep(i,0,m){
72         cin>>u>>v;
73         ve[u].pb(v);
74         ve[v].pb(u);
75     }
76     int w;
77     rep(i,0,k){
78         cin>>u>>v>>w;
79         se.insert(make_pair(u,make_pair(v,w)));
80     }
81     int pos=bfs();
82     if(!pos) cout<<-1<<endl;
83     else{
84         dfs(pos);
85     }
86 }
View Code
原文地址:https://www.cnblogs.com/Fighting-sh/p/10436740.html