Codeforces Round 879 (Div. 2)

发布时间 2023-07-05 10:52:36作者: 江上舟摇

其实昨天就应该写了,不过D不太会,但是今天补题发现D还是不太会

A:

给定包含-1和1的数字序列,要求满足这个序列

 

并且你可以将序列中的-1改成1,也可以将序列中的1改成-1,

让你确定将一个随机序列改成满足要求的序列的最小操作数

 

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 #include<iostream>
  5 #include<string>
  6 #include<vector>
  7 #include<stack>
  8 #include<bitset>
  9 #include<cstdlib>
 10 #include<cmath>
 11 #include<set>
 12 #include<list>
 13 #include<deque>
 14 #include<map>
 15 #include<queue>
 16 #include <iomanip>
 17 #include<ctime>
 18 using namespace std;
 19 #define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
 20 #define TLE (double)clock()/CLOCKS_PER_SEC<=0.95
 21 #define int long long 
 22 #define double long double
 23 #define endl '\n'
 24 #define inf LLONG_MAX
 25 #define iinf INT_MAX
 26 typedef pair<int,int> PII;
 27 const double PI = acos(-1.0);
 28 const double eps = 1e-6;
 29 const int INF = 0x3f3f3f3f;
 30 const int N = 1e2+10;
 31 int n,a[N];
 32 void solved()
 33 {
 34     bool flag1=false;
 35     cin>>n;
 36     int cnt1=0;
 37     int cnt2=0;
 38     for(int i=1;i<=n;i++)
 39     {
 40         cin>>a[i];
 41         if(a[i]!=1)
 42         {
 43             flag1=true;
 44             cnt2++;
 45         }
 46         else
 47         {
 48             cnt1++;
 49         }
 50     }
 51     if(!flag1)
 52     {
 53         cout<<0<<endl;
 54         return ;
 55     }
 56     bool flag2=false;
 57     for(int i=1;i<=n;i++)
 58     {
 59         if(a[i]==-1)
 60         {
 61             flag2=true;
 62             break;
 63         }
 64     }
 65     if(n%2==0&&!flag2)
 66     {
 67         cout<<n/2<<endl;
 68         return ;
 69     }
 70     if(n%2!=0&&!flag2)
 71     {
 72         cout<<n<<endl;
 73         return ;
 74     }
 75     int ans=0;
 76     if(cnt2&1)//如果-1的个数是奇数就要满足将他的个数变成偶数才能满足乘积为1
 77     {
 78         cnt1++;
 79         cnt2--;
 80         ans++;//此时需要操作+1
 81     }
 82     while(cnt1<cnt2)//然后是对-1的个数进行两两操作,保证最后的和>=0
 83     {
 84         cnt1+=2;
 85         cnt2-=2;
 86         ans+=2;
 87     }
 88     cout<<ans<<endl;
 89     
 90 }
 91 signed main()
 92 {
 93     IOS;
 94     int t;
 95     cin>>t;
 96     while(t--)
 97     {
 98         solved();
 99     }
100     return 0;
101 }

 

B:

给定两串数字x,y,要求从[x,y]区间内找出两个数字a,b使得a,b的每位数字相减求绝对值最后相加的最大值

 

这个找出第一位不相等的处理完之后后面的补9就可以了

 

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<iostream>
 5 #include<string>
 6 #include<vector>
 7 #include<stack>
 8 #include<bitset>
 9 #include<cstdlib>
10 #include<cmath>
11 #include<set>
12 #include<list>
13 #include<deque>
14 #include<map>
15 #include<queue>
16 #include <iomanip>
17 #include<ctime>
18 using namespace std;
19 #define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
20 #define TLE (double)clock()/CLOCKS_PER_SEC<=0.95
21 #define int long long 
22 #define double long double
23 #define endl '\n'
24 #define inf LLONG_MAX
25 #define iinf INT_MAX
26 typedef pair<int,int> PII;
27 const double PI = acos(-1.0);
28 const double eps = 1e-6;
29 const int INF = 0x3f3f3f3f;
30 void solved()
31 {
32     string s1,s2;
33     cin>>s1>>s2;
34     if(s1==s2)
35     {
36         cout<<0<<endl;
37         return ;
38     }
39     if(s1.length()>s2.length())
40     {
41         int x=s1.size()-s2.size();
42         string tmp="";
43         while(x--)
44         {
45             tmp+='0';
46         }
47         s2=tmp+s2;
48     }
49     else if(s1.length()<s2.length())
50     {
51         int x=s2.size()-s1.size();
52         string tmp="";
53         while(x--)
54         {
55             tmp+='0';
56         }
57         s1=tmp+s1;
58     }
59     int idx=0;
60     int ans=0;
61     for(int i=0;i<s2.length();i++)
62     {
63         if(s1[i]!=s2[i])
64         {
65             ans+=abs((s1[i]-'0')-(s2[i]-'0'));
66             idx=i;
67             break;
68         }
69         else    continue;
70     }
71     //cout<<idx<<endl;
72     if(idx>=s2.length()-1)    
73     {
74         cout<<ans<<endl;
75         return ;
76     }
77     ans+=(s1.length()-idx-1)*9;
78     cout<<ans<<endl;
79 }
80 signed main()
81 {
82     IOS;
83     int t;
84     cin>>t;
85     while(t--)    solved();
86     return 0;
87 }

 

 

 

C:初始给出两个字符串s,t,Alice可以对字符串进行修改字符的操作,bob可以对字符串进行反转,规定Alice先手操作,每次两人操作消耗的时间为1,问最少多少时间才能使得字符串相等

思路︰
Bob操作偶数次,相当于没有反转,无论是两次都反转同一,或者各自反转,最终都是同一情况。
首先A有两种结束游戏方式∶一种按照开局直接按照当前顺序改变s-t,或者把s变成反转t。
操作A和操作B,都是等价的。
如果按照当前顺序改变s-t,那么要保证,B操作次数是偶数,否则需要再翻转一次。
如果A改变反B,那么B要奇数次,因为必须要翻转─次。
做法︰
统计两种方式操作次数,判断奇偶情况,对于第二种来说,即使s和t反转完全相同,但是仍然需要两次操作,因为必须翻转一次,所以和2取max值

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 #include<iostream>
  5 #include<string>
  6 #include<vector>
  7 #include<stack>
  8 #include<bitset>
  9 #include<cstdlib>
 10 #include<cmath>
 11 #include<set>
 12 #include<list>
 13 #include<deque>
 14 #include<map>
 15 #include<queue>
 16 #include <iomanip>
 17 #include<ctime>
 18 using namespace std;
 19 #define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
 20 #define TLE (double)clock()/CLOCKS_PER_SEC<=0.95
 21 #define int long long 
 22 #define double long double
 23 #define endl '\n'
 24 #define inf LLONG_MAX
 25 #define iinf INT_MAX
 26 typedef pair<int,int> PII;
 27 const double PI = acos(-1.0);
 28 const double eps = 1e-6;
 29 const int INF = 0x3f3f3f3f;
 30 const int N = 1e5+10;
 31 void solved()
 32 {
 33     string s1,s2;
 34     int n;
 35     cin>>n;
 36     cin>>s1;
 37     cin>>s2;
 38 //    cout<<s1<<" "<<s2<<endl;
 39     if(s1==s2)
 40     {
 41         cout<<0<<endl;
 42         return ;
 43     }
 44     string tmp1=s1;
 45     reverse(tmp1.begin(),tmp1.end());
 46     string tmp2=s2;
 47     reverse(tmp2.begin(),tmp2.end());
 48     if(s1==tmp2||s2==tmp1)
 49     {
 50         cout<<2<<endl;
 51         return ;
 52     }
 53     int cnt1=0;
 54     int cnt2=0;
 55     for(int i=0;i<n;i++)
 56     {
 57         if(s1[i]!=s2[i])
 58         {
 59             cnt1++;
 60         }
 61         if(s1[i]!=s2[n-i-1])
 62         {
 63             cnt2++;
 64         }
 65     }
 66     //cout<<cnt1<<" "<<cnt2<<endl;
 67     // if(cnt1==1)
 68     // {
 69         // cout<<1<<endl;
 70         // return ;
 71     // }
 72     // if(cnt2==1)
 73     // {
 74         // cout<<2<<endl;
 75         // return ;
 76     // }
 77     // if(cnt1>cnt2)
 78     // {
 79         // if(cnt2&1)
 80         // {
 81             // cout<<2*cnt2<<endl;
 82         // }
 83         // else
 84         // {
 85             // cout<<cnt2+2<<endl;
 86         // }
 87     // }
 88     // else
 89     // {
 90         // if(cnt1&1)
 91         // {
 92             // cout<<2*cnt1-1<<endl;
 93         // }
 94         // else
 95         // {
 96             // cout<<cnt1+1<<endl;
 97         // }
 98     // }
 99     if(cnt1<=1)
100     {
101         cout<<cnt1<<endl;
102         return ;
103     }
104     if(cnt2<=1)
105     {
106         cout<<2<<endl;
107         return ;
108     }
109     int ans1=0;
110     if(cnt1&1)
111     {
112         ans1=2*cnt1-1;
113     }
114     else
115     {
116         ans1=2*cnt1;
117     }
118     int ans2=0;
119     if(cnt2&1)
120     {
121         ans2=2*cnt2;    
122     }
123     else
124     {
125         ans2=max(2ll,2*cnt2-1);
126     }
127     cout<<min(ans1,ans2)<<endl;
128     
129 }
130 signed main()
131 {
132     IOS;
133     int t;
134     cin>>t;
135     while(t--)
136     {
137         solved();
138     }
139     return 0;
140 }