Educational Codeforces Round 93 (Rated for Div. 2)(A B C D)

Practice link: https://codeforces.ml/contest/1398

 

A. Bad Triangle

代码:

 1 //#include<bits/stdc++.h>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<iostream>
 6 #include<string>
 7 #include<vector>
 8 #include<stack>
 9 #include<bitset>
10 #include<cstdlib>
11 #include<cmath>
12 #include<set>
13 #include<list>
14 #include<deque>
15 #include<map>
16 #include<queue>
17 #define ll long long
18 #define MOD 1000000007
19 #define INF 0x3f3f3f3f
20 #define mem(a,x) memset(a,x,sizeof(a))
21 #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
22 using namespace std;
23 const int maxn=1000005;
24 inline int rd() {
25     int res = 0,flag = 0;
26     char ch;
27     if ((ch = getchar()) == '-')flag = 1;
28     else if(ch >= '0' && ch <= '9')res = ch - '0';
29     while ((ch = getchar()) >= '0' && ch <= '9')res = (res<<1) + (res<<3) + (ch - '0');
30     return flag ? -res : res;
31 }
32 int a[50005];
33 int main()
34 {
35     int T;
36     int n;
37     cin>>T;
38     while(T--){
39         n=rd();
40         int x=0;
41         for(int i=1;i<=n;i++){
42             a[i]=rd();
43             if(i>2&&a[i]>=a[1]+a[2]&&x==0){
44                 x=i;
45             }
46         }
47         if(x){
48             printf("1 2 %d\n",x);
49         }else{
50             printf("-1\n");
51         }
52     }
53     return 0;
54 }
View Code

 

B. Substring Removal Game

代码:

 1 //#include<bits/stdc++.h>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<iostream>
 6 #include<string>
 7 #include<vector>
 8 #include<stack>
 9 #include<bitset>
10 #include<cstdlib>
11 #include<cmath>
12 #include<set>
13 #include<list>
14 #include<deque>
15 #include<map>
16 #include<queue>
17 #define ll long long
18 #define MOD 1000000007
19 #define INF 0x3f3f3f3f
20 #define mem(a,x) memset(a,x,sizeof(a))
21 #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
22 using namespace std;
23 const int maxn=1000005;
24 inline int rd() {
25     int res = 0,flag = 0;
26     char ch;
27     if ((ch = getchar()) == '-')flag = 1;
28     else if(ch >= '0' && ch <= '9')res = ch - '0';
29     while ((ch = getchar()) >= '0' && ch <= '9')res = (res<<1) + (res<<3) + (ch - '0');
30     return flag ? -res : res;
31 }
32 vector<int>v;
33 bool cmp(int x,int y){
34     return x>y;
35 }
36 int main()
37 {
38     int T;
39     string s;
40     cin>>T;
41     while(T--){
42         v.clear();
43         cin>>s;
44         int k=0;
45         for(int i=0;i<s.size();i++){
46             if(s[i]=='1'){
47                 k++;
48             }else{
49                 v.push_back(k);
50                 k=0;
51             }
52         }
53         if(k!=0)v.push_back(k);
54         sort(v.begin(),v.end(),cmp);
55         ll sum=0;
56         for(int i=0;i<v.size();i+=2){
57             sum+=v[i];
58         }
59         cout<<sum<<endl;
60     }
61     return 0;
62 }
View Code

 

C. Good Subarrays

题意:求一个字符串中符合 这个条件的子串的数量。

思路:我们可以把每个元素都减一,那么问题就转化成了一个字符串中有多少个和为 0 的子串,对于这个问题自然而然想到前缀和,就是如果两个前缀和相等,就说明这个子串的和为 0,所以我们可以记录从 1~n 的所有前缀和的数量,然后每次碰到相同的前缀和,就加上前面部分字符串中拥有这个前缀和的数量。

代码:

 1 //#include<bits/stdc++.h>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<iostream>
 6 #include<string>
 7 #include<vector>
 8 #include<stack>
 9 #include<bitset>
10 #include<cstdlib>
11 #include<cmath>
12 #include<set>
13 #include<list>
14 #include<deque>
15 #include<map>
16 #include<queue>
17 #define ll long long
18 #define MOD 1000000007
19 #define INF 0x3f3f3f3f
20 #define mem(a,x) memset(a,x,sizeof(a))
21 #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
22 using namespace std;
23 const int maxn=1000005;
24 inline int rd() {
25     int res = 0,flag = 0;
26     char ch;
27     if ((ch = getchar()) == '-')flag = 1;
28     else if(ch >= '0' && ch <= '9')res = ch - '0';
29     while ((ch = getchar()) >= '0' && ch <= '9')res = (res<<1) + (res<<3) + (ch - '0');
30     return flag ? -res : res;
31 }
32 /*
33 int head[maxn];
34 int num=0;
35 struct edg{
36     int next,to,w;
37 }edge[maxn];
38 void add_edge(int u,int v,int w)
39 {
40     num++;
41     edge[num].next=head[u];edge[num].to=v;edge[num].w=w;head[u]=num;
42     edge[++num].next=head[v];edge[num].to=u;edge[num].w=w;head[v]=num;
43 } */
44 //-----------
45 map<int,int>mp;
46 int num[maxn];
47 int main()
48 {
49     int T,n;
50     string s;
51     cin>>T;
52     while(T--){
53         cin>>n;
54         cin>>s;
55         int sum=0;
56         ll ans=0;
57         for(int i=1;i<=n;i++){
58             num[i]=s[i-1]-'0'-1;
59         }
60         mp.clear();
61         mp[0]=1;
62         for(int i=1;i<=n;i++){
63             sum+=num[i];
64             if(mp[sum])ans+=mp[sum],mp[sum]++;
65             else mp[sum]++;
66         }
67         printf("%lld\n",ans);
68     }
69     return 0;
70 }
71  
View Code

 

D. Colored Rectangles

题意:给你R对红色的木棒,G对绿色的木棒,B对蓝色的木棒,你每次可以选择两对颜色不同的木棒组成一个矩形,问你这些木棒最大可以组成矩形的总面积。

思路:dp题,dp[ r ][ g ][ b ]表示对这三种不同颜色的木棒的选择方式,前面是选择好的dp[ r ][ b ][ g ],比如dp[ r ][ g+1 ][ b+1 ]表示选择了绿色和蓝色的两对木棒,要让面积最大肯定是要选择最大的相乘,所以要对三组木棒从大到小的排序。

        转移方程:dp[ x+1 ][ y+1 ][ z ] = max( dp[ x+1 ][ y+1 ][ z ] , dp[ x ][ y ][ z ] + r[ x+1 ] * g[ y+1 ] ) 

                       dp[ x ][ y+1 ][ z+1 ] = max( dp[ x ][ y+1 ][ z+1 ] , dp[ x ][ y ][ z ] + g[ y+1 ] * b[ z+1 ] )

                       dp[ x+1 ][ y ][ z+1 ] = max( dp[ x+1 ][ y ][ z+1 ] , dp[ x ][ y ][ z ] + r[ x+1 ] * b[ z+1 ] )

                      最后用 ans 和这三个值进行比较即可。

代码:

 1 //#include<bits/stdc++.h>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<iostream>
 6 #include<string>
 7 #include<vector>
 8 #include<stack>
 9 #include<bitset>
10 #include<cstdlib>
11 #include<cmath>
12 #include<set>
13 #include<list>
14 #include<deque>
15 #include<map>
16 #include<queue>
17 #define ll long long
18 #define MOD 1000000007
19 #define INF 0x3f3f3f3f
20 #define mem(a,x) memset(a,x,sizeof(a))
21 #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
22 using namespace std;
23 const int maxn=1000005;
24 inline int rd() {
25     int res = 0,flag = 0;
26     char ch;
27     if ((ch = getchar()) == '-')flag = 1;
28     else if(ch >= '0' && ch <= '9')res = ch - '0';
29     while ((ch = getchar()) >= '0' && ch <= '9')res = (res<<1) + (res<<3) + (ch - '0');
30     return flag ? -res : res;
31 }
32 int r,g,b;
33 int x[205],y[205],z[205];
34 ll dp[205][205][205];
35 bool cmp(int a,int b)
36 {
37     return a>b;
38 }
39 int main()
40 {
41     cin>>r>>g>>b;
42     for(int i=1;i<=r;i++){
43         x[i]=rd();
44     }
45     for(int i=1;i<=g;i++){
46         y[i]=rd();
47     }
48     for(int i=1;i<=b;i++){
49         z[i]=rd();
50     }
51     sort(x+1,x+r+1,cmp);
52     sort(y+1,y+g+1,cmp);
53     sort(z+1,z+b+1,cmp);
54     dp[0][0][0]=0;
55     ll ans=0;
56     for(int i=0;i<=r;i++){
57         for(int j=0;j<=g;j++){
58             for(int k=0;k<=b;k++){
59                 dp[i+1][j+1][k]=max(dp[i+1][j+1][k],dp[i][j][k]+x[i+1]*y[j+1]);
60                 dp[i][j+1][k+1]=max(dp[i][j+1][k+1],dp[i][j][k]+y[j+1]*z[k+1]);
61                 dp[i+1][j][k+1]=max(dp[i+1][j][k+1],dp[i][j][k]+x[i+1]*z[k+1]);
62                 ans=max(ans,max(dp[i+1][j+1][k],max(dp[i+1][j][k+1],dp[i][j+1][k+1])));
63             }
64         }
65     }
66     cout<<ans;
67     return 0;
68 }
View Code

 

posted @ 2020-08-23 20:47  hachuochuo  阅读(132)  评论(0编辑  收藏  举报