Codeforces Round #508 (Div. 2)

Codeforces Round #508 (Div. 2)

http://codeforces.com/contest/1038

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 IT set<node>::iterator
 6 #define sqr(x) ((x)*(x))
 7 #define pb push_back
 8 #define eb emplace_back
 9 #define maxn 100005
10 #define eps 1e-8
11 #define pi acos(-1.0)
12 #define rep(k,i,j) for(int k=i;k<j;k++)
13 typedef long long ll;
14 typedef pair<int,int> pii;
15 typedef pair<ll,ll>pll;
16 typedef pair<ll,int> pli;
17 typedef pair<pair<int,string>,pii> ppp;
18 typedef unsigned long long ull;
19 const long long MOD=1e9+7;
20 const double oula=0.57721566490153286060651209;
21 using namespace std;
22 
23 int a[27];
24 
25 int main(){
26     std::ios::sync_with_stdio(false);
27     int n,m;
28     string str;
29     cin>>n>>m>>str;
30 
31     for(int i=0;i<n;i++){
32         a[str[i]-'A']++;
33     }
34     sort(a,a+26);
35     int ans=0x3f3f3f3f;
36     int co=0;
37     for(int i=25;i>=0;i--){
38 
39         if(a[i]==0) break;
40         ans=min(ans,a[i]);
41         co++;
42     }
43     if(co<m) cout<<0;
44     else cout<<ans*co;
45 }
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 IT set<node>::iterator
 6 #define sqr(x) ((x)*(x))
 7 #define pb push_back
 8 #define eb emplace_back
 9 #define maxn 100005
10 #define eps 1e-8
11 #define pi acos(-1.0)
12 #define rep(k,i,j) for(int k=i;k<j;k++)
13 typedef long long ll;
14 typedef pair<int,int> pii;
15 typedef pair<ll,ll>pll;
16 typedef pair<ll,int> pli;
17 typedef pair<pair<int,string>,pii> ppp;
18 typedef unsigned long long ull;
19 const long long MOD=1e9+7;
20 const double oula=0.57721566490153286060651209;
21 using namespace std;
22 
23 int main(){
24     std::ios::sync_with_stdio(false);
25     int n;
26     cin>>n;
27     if(n==1||n==2){
28         cout<<"No"<<endl;
29     }
30     else{
31         cout<<"Yes"<<endl;
32         if(n&1){
33             cout<<1<<" "<<n<<endl;
34             cout<<n-1;
35             for(int i=1;i<n;i++) cout<<" "<<i;
36         }
37         else{
38             cout<<1<<" "<<n/2<<endl;
39             cout<<n-1;
40             for(int i=1;i<=n;i++){
41                 if(i!=n/2) cout<<" "<<i;
42             }
43         }
44     }
45 }
View Code

C

题意:有两个人,每人有n个数字,两个人轮流操作,有两种操作方式:1.去掉对方的一个数字 2.选择自己的一个数字加入自己的分数中

思路:排序后从大到小贪心比较两个人的分数,如果当前是A操作,且分数a[L]>b[R],A就选择操作2,否则选择操作1,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 IT set<node>::iterator
 6 #define sqr(x) ((x)*(x))
 7 #define pb push_back
 8 #define eb emplace_back
 9 #define maxn 100005
10 #define eps 1e-8
11 #define pi acos(-1.0)
12 #define rep(k,i,j) for(int k=i;k<j;k++)
13 typedef long long ll;
14 typedef pair<int,int> pii;
15 typedef pair<ll,ll>pll;
16 typedef pair<ll,int> pli;
17 typedef pair<pair<int,string>,pii> ppp;
18 typedef unsigned long long ull;
19 const long long MOD=1e9+7;
20 const double oula=0.57721566490153286060651209;
21 using namespace std;
22 
23 int n;
24 ll a[100005],b[100005];
25 
26 
27 int main(){
28     std::ios::sync_with_stdio(false);
29     cin>>n;
30     for(int i=1;i<=n;i++) cin>>a[i];
31     for(int i=1;i<=n;i++) cin>>b[i];
32     sort(a+1,a+n+1);
33     sort(b+1,b+n+1);
34     ll ans1=0,ans2=0;
35     int L=n,R=n;
36     while(L&&R){
37         if(a[L]>b[R]){
38             ans1+=a[L];
39             L--;
40             if(L==0){
41                 ans2+=b[R];
42                 R--;
43             }
44             else{
45                 if(a[L]>b[R]){
46                     L--;
47                 }
48                 else{
49                     ans2+=b[R];
50                     R--;
51                 }
52             }
53         }
54         else{
55             R--;
56             if(R==0){
57                 L--;
58             }
59             else{
60                 if(b[R]>a[L]){
61                     ans2+=b[R];
62                     R--;
63                 }
64                 else{
65                     L--;
66                 }
67             }
68         }
69     }
70     //cout<<L<<" "<<R<<endl;
71     while(L){
72         ans1+=a[L];
73         L-=2;
74     }
75     while(R){
76         R--;
77         ans2+=b[R];
78         R--;
79     }
80     cout<<ans1-ans2<<endl;
81 }
View Code

D

题意:有n只动物,每只动物都有一个分数,每次一只动物可以吃掉左边或者右边动物,然后它的分数会减去被吃掉的动物的分数,问最后剩下的动物的分数的最大值是多少

思路:通过模拟可以发现一个规律:如果全是正数,最大值等于数值之和减去最小值的两倍,全是负数,最大值等于数值绝对值之和减去最大值的两倍,混合的情况等于数值的绝对值之和(感觉和今天的蓝桥杯后缀表达式那题思路有点像)

 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 IT set<node>::iterator
 6 #define sqr(x) ((x)*(x))
 7 #define pb push_back
 8 #define eb emplace_back
 9 #define maxn 100005
10 #define eps 1e-8
11 #define pi acos(-1.0)
12 #define rep(k,i,j) for(int k=i;k<j;k++)
13 typedef long long ll;
14 typedef pair<int,int> pii;
15 typedef pair<ll,ll>pll;
16 typedef pair<ll,int> pli;
17 typedef pair<pair<int,string>,pii> ppp;
18 typedef unsigned long long ull;
19 const long long MOD=1e9+7;
20 const double oula=0.57721566490153286060651209;
21 using namespace std;
22 
23 int n;
24 ll a[500005];
25 
26 
27 int main(){
28     std::ios::sync_with_stdio(false);
29     ll sum=0;
30     cin>>n;
31     ll Min=0x3f3f3f3f,Max=-0x3f3f3f3f;
32     for(int i=0;i<n;i++){
33         cin>>a[i];
34         sum+=abs(a[i]);
35         Min=min(Min,a[i]);
36         Max=max(Max,a[i]);
37     }
38     if(n==1) cout<<a[0];
39     else if(n==2) cout<<max(a[0]-a[1],a[1]-a[0]);
40     else{
41         if(Min>0) sum-=2*Min;
42         else if(Max<0) sum+=2*Max;
43         cout<<sum<<endl;
44     }
45 }
View Code

E

题意:你有n个砖头,每个砖头都有一个值且两端分别有一种颜色,如果两块砖头接触的端点颜色相同,就可以称这两块砖头为一个序列(你可以将一块砖头翻转),一个序列可能由多块砖头构成,序列的值为所构成的砖头的值之和,求所有情况下所有序列的最大值

思路:设 dp[i][j][x][y]表示用第i个到第j个之间的砖头构成一个序列,序列左端颜色为x,右端颜色为y

有三种情况:

1、使用中间的某些砖头dp[i][j][q][w]=max(dp[i][e][q][r]+dp[e+1][j][r][w])

2、将砖头端点的颜色反转dp[i][j][q][w]=max(dp[i][e][r][w]+dp[e+1][j][q][r])

3、不用中间的一部分砖头dp[i][j][q][w]=max(dp[i][e][q][w],dp[e+1][j][q][w])

 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 IT set<node>::iterator
 6 #define sqr(x) ((x)*(x))
 7 #define pb push_back
 8 #define eb emplace_back
 9 #define maxn 100005
10 #define eps 1e-8
11 #define pi acos(-1.0)
12 #define rep(k,i,j) for(int k=i;k<j;k++)
13 typedef long long ll;
14 typedef pair<int,int> pii;
15 typedef pair<ll,ll>pll;
16 typedef pair<ll,int> pli;
17 typedef pair<pair<int,string>,pii> ppp;
18 typedef unsigned long long ull;
19 const long long MOD=1e9+7;
20 const double oula=0.57721566490153286060651209;
21 using namespace std;
22 
23 int n;
24 int dp[105][105][5][5];
25 
26 int main(){
27     std::ios::sync_with_stdio(false);
28     cin>>n;
29     memset(dp,-0x3f3f3f3f,sizeof(dp));
30     int a,b,c;
31     for(int i=1;i<=n;i++){
32         cin>>a>>b>>c;
33         dp[i][i][a][c]=dp[i][i][c][a]=b;
34     }
35     int ans=0;
36     for(int i=n;i;i--){
37         for(int j=i;j<=n;j++){
38             for(int q=1;q<=4;q++){
39                 for(int w=1;w<=4;w++){
40                     for(int e=i;e<=j+1;e++){
41                         dp[i][j][q][w]=max(dp[i][j][q][w],max(dp[i][e][q][w],dp[e+1][j][q][w]));
42                         for(int r=1;r<=4;r++){
43                             dp[i][j][q][w]=max(dp[i][j][q][w],max(dp[i][e][r][w]+dp[e+1][j][q][r],dp[i][e][q][r]+dp[e+1][j][r][w]));
44                         }
45                     }
46                     ans=max(ans,dp[i][j][q][w]);
47                 }
48             }
49         }
50     }
51     cout<<ans<<endl;
52 }
View Code
原文地址:https://www.cnblogs.com/Fighting-sh/p/10590473.html