Appleman and Card Game CodeForces
来源:互联网 发布:火车票买票软件 编辑:程序博客网 时间:2024/05/01 12:51
Appleman and Card Game CodeForces - 462B
题目描述
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 ≤ 10^5). 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.
Example
Input
15 10
DZFDFZDFDDDDDDF
Output
82
Input
6 4
YJSNPI
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.
大致题意
让你从n个写有一个大写字母的卡片里选出k个卡片,你所选的每个卡片的值为你挑选出的k张卡片中写有相同大写字母的卡片的个数,比如你选了6张AAABBC则每张卡片A的值为3,B为2,C为1。然后问你所能选出的最多卡片总和值为多少。
思路:贪心,先统计每个字母对应的卡片数量,然后从最多的那个开始取。
注意,记得开 long long。
代码如下
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#include<vector>#include<cmath>#include<string>#define ll long long #define maxn 100005using namespace std;ll a[26];bool cmp(ll x,ll y){ return x>y;}int main(){ ll s=0; ll n,k; char c; cin>>n>>k; memset(a,0,sizeof(a)); for(ll i=1;i<=n;i++) { cin>>c; a[c-'A']++; } sort(a,a+26,cmp); for(ll i=0;i<26;i++){ if(a[i]<=k) { s+=a[i]*a[i]; k-=a[i]; } else { s+=k*k; break; } } cout<<s; return 0;}
- Appleman and Card Game CodeForces
- 【CODEFORCES】 B. Appleman and Card Game
- Codeforces Round #263 (Div. 2)B. Appleman and Card Game
- Codeforces Round #263 (Div. 2)B. Appleman and Card Game
- codeforces 462 B.Appleman and Card Game (贪心)
- Codeforces 462B Appleman and Card Game(贪心)
- Codeforces Round #263 (Div. 2)-B. Appleman and Card Game
- CodeForces 462B Appleman and Card Game(贪心)
- CodeForces 462B Appleman and Card Game 贪心
- B. Appleman and Card Game
- B. Appleman and Card Game
- 462B - Appleman and Card Game
- CF 462B Appleman and Card Game
- Appleman and Card Game(贪心)
- Codefores Round #263 (div2) B. Appleman and Card Game
- Code Forces 462B Appleman and Card Game
- 【Codeforces Round 263 (Div 2)B】【水题 贪心】Appleman and Card Game 选字符,每个字符个数的平方
- Codeforces---Fox and Card Game
- 搜索范围
- cookie 和session 的区别详解
- 《深入理解Java虚拟机》笔记
- 使得nginx支持pathinfo访问模式
- js Array 交集 并集 差集 去重
- Appleman and Card Game CodeForces
- Socket
- 浏览器内核一些定义
- EasyUI datagrid对特定的某行某些字段打开编辑
- 省赛前的练习A
- 排序的有趣应用:Shuffling(洗牌)
- LeetCode——2. Add Two Numbers
- orcale实现自增长,创建序列实现自增长
- 设置ndk路径