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 }
时间: 2024-08-05 13:18:02

CodeForces - 462B Appleman and Card Game的相关文章

CodeForces 462B Appleman and Card Game(贪心)

题目链接:http://codeforces.com/problemset/problem/462/B Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards

Codeforces 263B. Appleman and Card Game

B. Appleman and Card Game time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's

Codeforces 461B Appleman and Tree(木dp)

题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k条边,使得树变成k+1个联通分量.保证每一个联通分量有且仅有1个黑色节点.问有多少种切割方法. 解题思路:树形dp,dp[i][0]和dp[i][1]分别表示子树一下的切割方法中,i节点所在联通块不存在黑节点和已经存在一个黑节点的方案数. #include <cstdio> #include &l

Educational Codeforces Round 21 F. Card Game(网络流之最大点权独立集)

题目链接:Educational Codeforces Round 21 F. Card Game 题意: 有n个卡片,每个卡片有三个值:p,c,l; 现在让你找一个最小的L,使得满足选出来的卡片l<=L,并且所有卡片的p的和不小于k. 选择卡片时有限制,任意两张卡片的c之和不能为质数. 题解: 和hdu 1565 方格取数(2)一样,都是求最大点权独立集. 不难看出来,这题再多一个二分. 注意的是在构造二部图的时候,按照c值的奇偶性构造. 当c==1时要单独处理,因为如果有多个c==1的卡片,

Codeforces 461B Appleman and Tree(树形dp)

题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每个节点的父亲节点,以及每个点的颜色(0表示白色,1表示黑色),切断这棵树的k条边,使得树变成k+1个联通分量,保证每个联通分量有且仅有1个黑色节点.问有多少种分割方法. 解题思路:树形dp,dp[i][0]和dp[i][1]分别表示子树一下的分割方法中,i节点所在联通块不存在黑节点和已经存在一个黑节点的方案数. #include <cstdio> #include <c

Codeforces 461C Appleman and a Sheet of Paper(模拟)

题目链接:Codeforces 461C Appleman and a Sheet of Paper 题目大意:就是一个叠被子的过程,穿插着询问一段区间上被子的单位厚度. 解题思路:用前缀和数组模拟即可.因为对于折超过一半的处理为将令一半叠上来,所以需要变量记录当前被子的正反状态.处理好下标关系即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const

codeforces 462C Appleman and Toastman 解题报告

题目链接:http://codeforces.com/problemset/problem/461/A 题目意思:给出一群由 n 个数组成的集合你,依次循环执行两种操作: (1)每次Toastman得到一个集合,他计算所有数的和,并且将它加入到score里.之后他将这个集合传给Appleman. (2)Appleman得到的集合如果只有一个数,就把它弃之,否则将这个集合分成 两个不相交且不空的集合,传回给Toastman. 这些操作不断执行直到集合个数变为0,也就是通过使集合都变成只有一个元素而

Codeforces 263A. Appleman and Easy Task

A. Appleman and Easy Task time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Toastman came up with a very easy task. He gives it to Appleman, but Appleman doesn't know how to solve it. Can you

Codeforces 263C. Appleman and Toastman

C. Appleman and Toastman time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Appleman and Toastman play a game. Initially Appleman gives one group of nnumbers to the Toastman, then they start