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 cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman‘s card i you should calculate how much Toastman‘s cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.

Given the description of Appleman‘s cards. What is the maximum number of coins Toastman can get?

Input

The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman.

Output

Print a single integer – the answer to the problem.

Sample test(s)

input

15 10DZFDFZDFDDDDDDF

output

82

input

6 4YJSNPI

output

4

Note

In the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin.

解题:贪心。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 int letter[27];
18 char str[100100];
19 bool cmp(const int &x,const int &y){
20     return x > y;
21 }
22 int main() {
23     int n,k;
24     LL ans;
25     while(~scanf("%d %d",&n,&k)){
26         memset(letter,0,sizeof(letter));
27         scanf("%s",str);
28         for(int i = 0; str[i]; i++) letter[str[i]-‘A‘]++;
29         sort(letter,letter+26,cmp);
30         for(int i = ans = 0; k && i < 26; i++){
31             ans += 1LL*min(k,letter[i])*min(k,letter[i]);
32             k -= min(k,letter[i]);
33         }
34         cout<<ans<<endl;
35     }
36     return 0;
37 }

时间: 2024-10-03 15:01:54

Codeforces 263B. 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 - 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但是

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