1. 程式人生 > >CodeForce 837 A/B/C解題報告

CodeForce 837 A/B/C解題報告

word 結果 only 代碼 技術分享 ios 輸入 sea stdin

A Text Volume

題面:

You are given a text of single-space separated words, consisting of small and capital Latin letters.
Volume of the word is number of capital letters in the word. Volume of the text is maximum volume of all words in the text.
Calculate the volume of the given text.

Input
The first line contains one integer number n (1?≤?n?≤?200) — length of the text.
The second line contains text of single-space separated words s1,?s2,?…,?si, consisting only of small and capital Latin letters.

Output
Print one integer number — volume of text.
input
7
NonZERO
output
5
input
24
this is zero answer text
output
0
input
24
Harbour Space University
output
1

題目大意:

對於每一個數據,有一個數字代表下一行的字符串的長度。
一個字符串裏可能有很多個單詞,問在這其中的單詞裏,大寫字母最多的單詞中有多少個大寫字母。

大致思路:

純水題,記錄一下已經讀進去的字符串長度,到了n直接跳出。
每一個單詞掃一遍,記錄大寫字母的個數。與記錄的最大值取一個max
然後輸出結果就可以了。

代碼:

技術分享
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     ios::sync_with_stdio(false);
 6     //freopen("in.txt","r",stdin);
 7     int n,len=0,maxn=0,cnt;
 8     char str[210];
 9     cin>>n;
10     while(cin>>str)
11     {
12         cnt=0;
13         int l=strlen(str);
14 for(int i=0;i<l;++i) 15 if(isupper(str[i])) 16 cnt++; 17 maxn=max(maxn,cnt); 18 19 len+=l; 20 if(len>=n) 21 break; 22 len++; 23 } 24 cout<<maxn<<endl; 25 return 0; 26 }
View Code

B Flag of Berland

題面:

The flag of Berland is such rectangular field n?×?m that satisfies following conditions:

Flag consists of three colors which correspond to letters ‘R’, ‘G’ and ‘B’.
Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color.
Each color should be used in exactly one stripe.
You are given a field n?×?m, consisting of characters ‘R’, ‘G’ and ‘B’. Output “YES” (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print “NO” (without quotes).

Input
The first line contains two integer numbers n and m (1?≤?n,?m?≤?100) — the sizes of the field.

Each of the following n lines consisting of m characters ‘R’, ‘G’ and ‘B’ — the description of the field.

Output
Print “YES” (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print “NO” (without quotes).

Examples
input
6 5
RRRRR
RRRRR
BBBBB
BBBBB
GGGGG
GGGGG
output
YES
input
4 3
BRG
BRG
BRG
BRG
output
YES
input
6 7
RRRGGGG
RRRGGGG
RRRGGGG
RRRBBBB
RRRBBBB
RRRBBBB
output
NO
input
4 4
RRRR
RRRR
BBBB
GGGG
output
NO
Note
The field in the third example doesn’t have three parralel stripes.

Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights — 2, 1 and 1.

題目大意:

現在有一個長寬已知的矩形。
一個合法的矩形要求是裏面只有三個形狀一樣的區域,且分別是R,G,B這三個字母組成的。
然後給你一個矩形,讓你判斷是否合法

大致思路:

這個題看起來很簡單,但寫起來有點惡心。
首先可以知道,如果這個矩形裏三個字母的數量不相等,那麽肯定不合法。
所以可以在輸入矩陣時將他們的ASC碼記錄,然後看是否能被R,G,B的ASC碼之和整除。
如果不能,則肯定不合法
然後看矩形的第一個字母是什麽,然後通過掃描矩陣來確定第一個區域的形狀。是橫著擺還是豎著擺。有一個結論:如果能三分一個矩形,則這個形狀至少橫著占滿一行或者豎著占滿一列。所以可以把第一個區域的長寬與橫豎確定。然後再區域內掃一遍看是否有其他顏色的點。
之後再根據形狀來掃另外的區域,看是否都合法。

代碼:

技術分享
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=110;
 4 char mart[maxn][maxn];
 5 int cnt=0,c,l;
 6 int n,m,num=0;
 7 bool flag_line=true;//判斷形狀的flag
 8 bool rpp()//判斷第一個區域的形狀
 9 {
10     char color=mart[1][1];
11     for(int i=1;i<=m;++i)
12         if(color!=mart[1][i]){
13             flag_line=false;
14             l=i-1;
15             break;
16         }
17     for(int i=1;i<=n;++i){
18         if(color!=mart[i][1]){
19             if(!flag_line)
20                 return false;
21             c=i-1;
22             break;
23         }
24     }
25     for(int i=1;i<=c;++i)
26         for(int j=1;j<=l;++j)
27             if(color!=mart[i][j])
28                 return false;
29     return true;
30 }
31 bool other()
32 {
33     char color1=mart[1][1];
34     char color2;
35     if(flag_line){
36         //
37         color2=mart[1+c][1];
38         for(int i=1+c;i<=n&&i<=2*c;++i)
39             for(int j=1;j<=m;++j)
40                 if(color2!=mart[i][j]){
41                     return false;
42                 }
43 
44         color2=mart[1+2*c][1];
45         for(int i=1+2*c;i<=n;++i)
46             for(int j=1;j<=m;++j)
47                 if(color2!=mart[i][j]){
48                     return false;
49                 }
50     }else{
51         color2=mart[1][1+l];
52         for(int i=1;i<=n;++i)
53             for(int j=1+l;j<=2*l&&j<=m;++j)
54                 if(color2!=mart[i][j])
55                     return false;
56 
57         color2=mart[1][1+2*l];
58         for(int i=1;i<=n;++i)
59             for(int j=1+l*2;j<=m;++j)
60                 if(color2!=mart[i][j])
61                     return false;
62     }
63     return true;
64 }
65 int main()
66 {
67     ios::sync_with_stdio(false);
68     //freopen("in.txt","r",stdin);
69     cin>>n>>m;
70     c=n;
71     l=m;
72     for(int i=1;i<=n;++i)
73         for(int j=1;j<=m;++j){
74             cin>>mart[i][j];
75             num+=mart[i][j];
76         }
77     if(num%(R+G+B))
78         cout<<"NO"<<endl;
79     else if(!rpp())
80         cout<<"NO"<<endl;
81     else if(!other())
82         cout<<"NO"<<endl;
83     else
84         cout<<"YES"<<endl;
85     return 0;
86 }
View Code

C Two Seals

題面:

One very important person has a piece of paper in the form of a rectangle a?×?b.

Also, he has n seals. Each seal leaves an impression on the paper in the form of a rectangle of the size xi?×?yi. Each impression must be parallel to the sides of the piece of paper (but seal can be rotated by 90 degrees).

A very important person wants to choose two different seals and put them two impressions. Each of the selected seals puts exactly one impression. Impressions should not overlap (but they can touch sides), and the total area occupied by them should be the largest possible. What is the largest area that can be occupied by two seals?

Input
The first line contains three integer numbers n, a and b (1?≤?n,?a,?b?≤?100).

Each of the next n lines contain two numbers xi, yi (1?≤?xi,?yi?≤?100).

Output
Print the largest total area that can be occupied by two seals. If you can not select two seals, print 0.

Examples
input
2 2 2
1 2
2 1
output
4
input
4 10 9
2 3
1 1
5 10
9 11
output
56
input
3 10 10
6 6
7 7
20 5
output
0
Note
In the first example you can rotate the second seal by 90 degrees. Then put impression of it right under the impression of the first seal. This will occupy all the piece of paper.

In the second example you can’t choose the last seal because it doesn’t fit. By choosing the first and the third seals you occupy the largest area.

In the third example there is no such pair of seals that they both can fit on a piece of paper.

題目大意:

有一張長寬已知的矩形。
現在有很多個小矩形,長寬已知,擺放方式可以旋轉90°。
現在取兩個小矩形放在大矩形上,要求兩個小矩形不能有互相覆蓋的地方。
求可以覆蓋的最大面積,如果沒有合法解,輸出0

大致思路:

貪心+枚舉
把每一個小矩形根據面積排序。
我在開始的時候把每一個矩形(包括大矩形)的x,y按照大小排好。
這樣在取第一個矩形的時候就可以貪心的讓剩下的面積最大。
然後再挨個試第二個矩形,取成功之後和保存的最大值取一個max。

代碼:

技術分享
#include<bits/stdc++.h>
using namespace std;
const int maxn=110;
typedef struct{
    int x,y;
    int s;
}Type;
bool cmp(Type a,Type b)
{
    return a.s>b.s;
}
bool check(int nx,int ny,int x,int y)//看取的矩形和現在剩下的區域能否放下
{
    if((nx<=x&&ny<=y)||(nx<=y&&ny<=x))
        return true;
    return false;
}
int main()
{
    //freopen("in.txt","r",stdin);
    Type Node[maxn];
    int n,x,y;
    cin>>n>>x>>y;
    if(x>y)
        swap(x,y);
    for(int i=0;i<n;++i){
        cin>>Node[i].x>>Node[i].y;
        if(Node[i].x>Node[i].y)
            swap(Node[i].x,Node[i].y);
        Node[i].s=Node[i].x*Node[i].y;
    }
    sort(Node,Node+n,cmp);
    int nx,ny,maxx=0,ans;
    for(int i=0;i<n-1;++i){
        if(!check(Node[i].x,Node[i].y,x,y))
            continue;
        ans=Node[i].s;
        if(Node[i].y<x){
            nx=x-Node[i].y;
            ny=y-Node[i].x;
        }else{
            nx=x-Node[i].x;
            ny=y-Node[i].y;
        }
        for(int j=i+1;j<n;++j){
            if(check(Node[j].x,Node[j].y,nx,y)||
            check(Node[j].x,Node[j].y,x,ny)){
                maxx=max(maxx,ans+Node[j].s);
            }
        }
    }
    cout<<maxx<<endl;
    return 0;
}
View Code

CodeForce 837 A/B/C解題報告