码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 841A 841B题解

时间:2017-08-19 21:22:25      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:isp   algorithm   最优   题解   代码   一个人   位置   pre   ide   

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置。

A. Generous Kefa    B. Godsend

两道水题...

 

A - 题目大意:把n个字母分配给k个人,如果一个人得到了两个或两个以上的相同字母,就会不开心,否则会开心(没有分配到字母也算开心)。请问能否找到一个合理的分配方案,使得每一个人都开心。如果能,输出YES,否则输出NO。

 

分析:

抽屉原理。如果任意一种字母的出现次数大于k,输出NO,否则输出YES。

 

AC代码:

技术分享
 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cmath>
 4 #include<cstring>
 5 
 6 inline void read(int &x)
 7 {
 8     char ch = getchar(),c = ch;x = 0;
 9     while(ch > 9 || ch < 0) c = ch,ch = getchar();
10     while(ch <= 9 && ch >= 0) x = (x<<1)+(x<<3)+ch-0,ch = getchar();
11     if(c == -) x = -x;
12 }
13 
14 int num[30];
15 
16 int main()
17 {
18     int n,k;
19     read(n),read(k);
20     char x = getchar();
21     for(int i = 1;i <= n;++ i)
22     {
23         while(x < a)
24             x = getchar();
25         num[x-a+1] ++;
26         x = getchar();
27     }
28     int mx = 0;
29     for(int i = 1;i <= 26;++ i)
30         if(num[i] > mx) mx = num[i];
31     if(mx > k)    printf("NO\n");
32     else    printf("YES\n");
33     return 0;
34 }
A

 

 

B - 题目大意:两个人玩游戏,给出一段有n个数字的序列,第一个人First每次可以选取一段和为奇数的区间,将其删去;第二个人Second每次可以选取一段和为偶数的区间,将其删去。删除操作执行完毕之后,序列的剩余两部分会自动向中间接起来。每个人都会按照最优策略进行游戏,问最后谁能赢,输出First或Second。

 

分析:

经过一系列推算可以发现,Second赢的情况只有一种,即序列中没有奇数的情况(可怜的Second)。

其他情况。序列中有奇数个奇数或偶数个奇数,都是First赢。

 

AC代码:

技术分享
 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cmath>
 4 #include<cstring>
 5 
 6 const int MAXN = 1000005;
 7 
 8 inline void read(int &x)
 9 {
10     char ch = getchar(),c = ch;x = 0;
11     while(ch > 9 || ch < 0) c = ch,ch = getchar();
12     while(ch <= 9 && ch >= 0) x = (x<<1)+(x<<3)+ch-0,ch = getchar();
13     if(c == -) x = -x;
14 }
15 
16 //FIRST:奇数  SECOND:偶数 
17 
18 int main()
19 {
20     int n,x,sum = 0;
21     read(n);
22     for(int i = 1;i <= n;++ i)
23     {
24         read(x);
25         if(x%2 == 1)
26             sum ++;
27     }
28     if(sum == 0)
29         printf("Second\n");
30     else
31         printf("First\n");
32     return 0;
33 }
B

 

Codeforces 841A 841B题解

标签:isp   algorithm   最优   题解   代码   一个人   位置   pre   ide   

原文地址:http://www.cnblogs.com/shingen/p/7397655.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!