Codeforces Round #221 (Div. 2) 解题报告

Problem A Lever

题意:大概的意思就是一个杠杆'^'表示支点,1-9表示上面的物体的重量,最后要求判断杠杆的倾斜方向。

思路:水题。先找到支点,然后遍历一遍求出左边与右边分别的重量,最后比较一下大小即可。

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <algorithm>
 6 #include <utility>
 7 #include <queue>
 8 #include <stack>
 9 #include <vector>
10 #define ll long long
11 #define INF 0x7fffffff
12 #define eps 1E-6
13 #define LEN 1000100
14 using namespace std;
15 
16 char str[LEN];
17 
18 int main()
19 {
20 //    freopen("in.txt", "r", stdin);
21 
22     while(scanf("%s", &str)!=EOF){
23         int len = strlen(str);
24         int pos;
25         for(int i=0; i<len; i++){
26             if(str[i]=='^'){
27                 pos = i;
28                 break;
29             }
30         }
31         ll left = 0, right = 0;
32         for(int i=0; i<len; i++){
33             if(i<pos && str[i]>='1' && str[i]<='9')left+=(str[i]-'0')*abs(i-pos);
34             if(i>pos && str[i]>='1' && str[i]<='9')right+=(str[i]-'0')*abs(i-pos);
35         }
36         if(left==right)printf("balance
");
37         else if(left>right)printf("left
");
38         else printf("right
");
39     }
40     return 0;
41 }
View Code

Problem B I.O.U.

题意:在若干个人中存在A 欠B C元钱这种关系,告诉你关系问你在人群中最少拿出多少钱能把钱还清。

思路:解法很简单,就是开一个数组统计若是欠别人钱减去钱数,借给别人则是加上。然后最后累加以下所有正权结点的和即为答案。

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <algorithm>
 6 #include <utility>
 7 #include <queue>
 8 #include <stack>
 9 #include <vector>
10 #define ll long long
11 #define INF 0x7fffffff
12 #define eps 1E-6
13 #define LEN 210
14 
15 using namespace std;
16 
17 int d[LEN];
18 
19 int main()
20 {
21 //    freopen("in.txt", "r", stdin);
22 
23     int n, m;
24     while(scanf("%d%d", &n, &m)!=EOF){
25         int a, b, val;
26         memset(d, 0, sizeof d);
27         for(int i=0; i<m; i++){
28             scanf("%d%d%d", &a, &b, &val);
29             d[a]-=val;
30             d[b]+=val;
31         }
32         int ans = 0;
33         for(int i=1; i<=n; i++){
34             if(d[i]>=0)ans+=d[i];
35         }
36         printf("%d
", ans);
37     }
38     return 0;
39 }
View Code

Problem D Maximum Submatrix 2

题意:有一个包含0,1的矩阵,其中行可以随便交换,问你最后得到的全一子矩阵最大有多少个一.

思路:贪心递推即可。先分析一下由于行可以随便交换,限制主要是在列上我们先开一个矩阵存储到当前位置每一列最长的一的个数。然后统计i行中连续一为j个的序列个数。

这样一来我们就可以利用贪心的思想把所有序列最长的一尽量排在最前面,这样从大到小统计即可得出最大值。这题很容易超时,首先不能排序,排了肯定超,另外一开始用scanf读入竟然也超时(第一次遇到0.0)比赛后换成gets就过了。。。

代码如下:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define LEN 5002
 4 
 5 int n, m, mlen[LEN][LEN], cnt[LEN][LEN];
 6 char Map[LEN][LEN];
 7 
 8 int max(int a, int b){return a>b?a:b;}
 9 
10 int main()
11 {
12 //    freopen("in.txt", "r", stdin);
13 
14     while(scanf("%d%d", &n, &m)!=EOF){
15         getchar();
16         for(int i=1; i<=n; i++){
17             gets(Map[i]+1);
18         }
19         for(int i=1; i<=n; i++){
20             for(int j=1; j<=m; j++){
21                 if(Map[i][j]=='1'){
22                     mlen[j][i] = mlen[j-1][i]+1;
23                     cnt[j][mlen[j][i]]++;
24                 }
25             }
26         }
27         int ans = 0, loc;
28         for(int i=1; i<=m; i++){
29             loc = 0;
30             for(int j=m; j>=1; j--){
31                 loc+=cnt[i][j];
32                 ans = max(ans, loc*j);
33             }
34         }
35         printf("%d
", ans);
36     }
37     return 0;
38 }
View Code
奔跑吧!少年!趁着你还年轻
原文地址:https://www.cnblogs.com/shu-xiaohao/p/3489921.html