Codeforces Round #585 (Div. 2) C,D

C title Address: http://codeforces.com/contest/1215/problem/C

The meaning of problems: there are two strings of the same length, by "a", "b" composed of two single character strings can be interchanged at any position, the minimum number Q become the same exchange of the two strings, You can not become the same as the output "-1."

Ideas: certainly not the same exchange, the number of ab and ba see the odd certainly can not become the same, the two-step ab or ba may become the same, ab and ba to two steps.

AC :( limited level of code, the code slightly longer please understand)

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 typedef long long ll;
 6 int main(){
 7     int n;
 8     cin>>n;
 9     char s1[200005],s2[200005];
10     for(int i=1;i<=n;i++)
11         cin>>s1[i];
12     getchar();
13     for(int i=1;i<=n;i++)
14         cin>>s2[i];
15     int num_ab=0,num_ba=0;
16     int aa[200005]={0},ab[200005]={0};
17     for(int i=1;i<=n;i++){
18         if(s1[i]=='a'&&s2[i]=='b'){
19             num_ab++;
20             aa[num_ab]=i;
21         }
22         else if(s1[i]=='b'&&s2[i]=='a'){
23             num_ba++;
24             ab[num_ba]=i;
25         }
26     }
27     if((num_ab+num_ba)&1) cout<<"-1"<<endl;
28     else{
29         if(num_ab&1){
30             cout<<num_ab/2+num_ba/2+2<<endl;
31             for(int i=1;i<num_ab;i+=2)
32                 cout<<aa[i]<<" "<<aa[i+1]<<endl;
33             for(int i=1;i<num_ba;i+=2)
34                 cout<<ab[i]<<" "<<ab[i+1]<<endl;
35             cout<<aa[num_ab]<<" "<<aa[num_ab]<<endl<<aa[num_ab]<<" "<<ab[num_ba]<<endl;
36         }
37         else{
38             cout<<(num_ab+num_ba)/2<<endl;
39             for(int i=1;i<=num_ab;i+=2)
40                 cout<<aa[i]<<" "<<aa[i+1]<<endl;
41             for(int i=1;i<=num_ba;i+=2)
42                 cout<<ab[i]<<" "<<ab[i+1]<<endl;
43         }
44     }
45     return 0;
46 }

D topics Address: http://codeforces.com/contest/1215/problem/D

Meaning of the questions: Monocarp and Bicarp two people to fill the ticket numbers (0-9), the table can be filled, digital table already, if after filling out the first half of the sum equal to the sum of the second half, Bicarp wins, otherwise Monocarp wins'? " , Monocarp to fill in, fill in the number of tickets and the total length of all even numbers.

Thinking: Direct consider winning Bicarp of the first half of the known set and the number of known and sum1 and SUM2, respectively, can fill in the first half and the rear half of num1, the second half may be filled number num2. When sum1 equal sum2, equal to only num1 num2, Bicarp to win.

sum1> When sum2, a simple analysis can be obtained when equal sum2 -sum1 (num1-num2) * 9/2, because the only control Bicarp (num1-num2) * 9/2 this number. sum2> Similarly when sum1.

AC Code:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 typedef long long ll;
 6 int main(){
 7     int n;
 8     cin>>n;
 9     char c;
10     int num1=0,num2=0,sum1=0,sum2=0;
11     for(int i=1;i<=n;i++){
12         cin>>c;
13         if(i<=n/2){
14             if(c=='?') num1++;
15             else sum1+=(c-'0');
16         }
17         else{
18             if(c=='?') num2++;
19             else sum2+=(c-'0');
20         }
21     }
22     bool flag;
23     if(sum1>sum2&&sum1-sum2==(num2-num1)*9/2) flag=false;
24     else if(sum1<sum2&&sum2-sum1==(num1-num2)*9/2) flag=false;
25     else if(sum1==sum2&&num1==num2) flag=false;
26     else flag=true;
27     if(flag) cout<<"Monocarp"<<endl;
28     else cout<<"Bicarp"<<endl;
29     return 0;
30 }

Why does it feel both questions than B and that good writing, too uncomfortable.

Guess you like

Origin www.cnblogs.com/xunzf0402/p/11545135.html