2011. Long Statement
Time limit: 0.5 second
Memory limit: 64 MB
Memory limit: 64 MB
Nikita, a schoolboy, is currently taking part in one of programming contests.He is really upset because all the problem statements are so long and unclear. So he took the statement of the first problem and cut itinto pieces in such a way that each piece contained exactly one letter. After that, he threw away all pieces with letter other than“a”, “b” or “c”. Now he has only
n pieces and wants to compilefrom them his own statement that should be shorter and clearer than the original one.
The new statement should be a single word compiled from all
n letters placed in some order. Nikita wondered if he can compile at least six different words of length
n from the letters. If this is not true,he will be ruined and will start solving other problems.Help Nikita to answer this monumental question!
Input
The first line contains an integer
n that is the number of pieces with letters (1 ≤
n ≤ 100).The second line describes these pieces as
n integers from 1 to 3.1 represents a piece with letter “a”, 2 represents a piece with letter “b”,3 represents a piece with letter “c”.
Output
If Nikita can compile at least six different words of length
n, output “Yes”. Otherwise output “No”.
Sample
input | output |
---|---|
6 1 2 2 3 3 3 | Yes |
Problem Source: Ural Regional School Programming Contest 2013
解析:坑爹的计数题。。。开始本以为必须用阶乘搞,最后发现,100!太大,搞不起。不过,我们可以换种思路,找找规律。
分情况讨论满足的情况:
n = 3:必须是1,2,3同时出现才满足。
n = 4:三个数字同时出现或者存在两个数量超过2的数字出现。
n = 5:同n = 4.
n > 5:至少有两个不同的数字出现。
其余的情况均不满足。
AC代码:
#include <bits/stdc++.h>
using namespace std;
int main(){
#ifdef sxk
freopen("in.txt", "r", stdin);
#endif // sxk
int n, a, b, x;
while(~scanf("%d", &n)){
a = b = 0;
for(int i=0; i<n; i++){
scanf("%d", &x);
a += (x == 1); //1出现的次数
b += (x == 2); //2出现的次数
}
int flag = 0;
if(n == 3 && a == 1 && b == 1) flag = 1;
else if((n == 4 || n == 5) && ((a && b && n - a - b) || (a >= 2) + (b >= 2) + (n - a - b >= 2) >= 2))
flag = 1;
else if(n > 5 && (a > 0) + (b > 0) + (n - a - b > 0) >= 2) flag = 1;
puts(flag ? "Yes" : "No");
}
return 0;
}