首页 > 代码库 > CodeForces - 462B Appleman and Card Game

CodeForces - 462B Appleman and Card Game

是一道简单题 

将字母从个数多到小排序 然后 再按题目算法得到最多

但是注意 数据类型声明 money要为long long

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 
 8 int  num[32];
 9 long long money[32];//因为 虽然 n是到10^5但是有money和money相乘 的操作 再存储在同一位置 就超了 
10 int n, k;
11 
12 bool cmp(int a, int b)
13 {
14     return a > b;
15 }
16 int main()
17 {
18     double sum = 0;
19     freopen("in.txt", "r", stdin);
20     while (~scanf("%d%d", &n, &k))
21     {
22         getchar();
23         sum  = 0;
24         memset(num, 0, sizeof(num));
25         for (int i = 0; i < n; i++)
26         {
27             char temp;
28             scanf("%c", &temp);
29             num[temp-A]++;
30         }
31         sort(num, num+26, cmp);
32         for (int i = 0; i < 26; i++)
33         {
34             money[i] = min(num[i], k);
35             k -= num[i];
36             if (k <= 0) break;
37         }
38         for (int i = 0; i < 26; i++)
39         {
40             sum+= money[i]*money[i];
41         }
42         printf("%.0lf\n", sum);
43     }
44     return 0;
45 
46 }

 

CodeForces - 462B Appleman and Card Game