CodeForces462B
Appleman and Card Game
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
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 Input
15 10
DZFDFZDFDDDDDDF
82
6 4
YJSNPI
4
//2016.8.2
#include<iostream>
#include<algorithm>
#include<cstring> using namespace std; int ch[]; bool cmp(int a, int b)
{
return a>b;
} int min(int a, int b)
{
return a > b ? b : a;
} int main()
{
int n, k;
long long ans, x;
char c;
while(cin >> n >> k)
{
memset(ch, , sizeof(ch));
getchar();
ans = ;
for(int i = ; i < n; i++)
{
c = getchar();
ch[c-'A']++;
}
sort(ch, ch+, cmp); for(int i = ; k > ;i++)
{
x = min(k, ch[i]);
ans += x*x;
k -= x;
}
cout << ans << endl;
}
return ;
}
CodeForces462B的更多相关文章
随机推荐
- 写自己的一个pdo数据库操作框架
http://stackoverflow.com/questions/20669850/pdo-database-abstraction-layer-with-multiple-queries-in- ...
- Selenuim+Python之元素定位总结及实例说明
网页自动化最基本的要求就是要定位到各个元素,然后才能对该元素进行各种操作(输入,点击,清除,提交等),所以笔者今天来总结下Selenuim+Python最基本的几种定位方式及实例说明,希望能帮助到大家 ...
- PAT 天梯赛 L2-005 集合相似度
set的应用 题目链接 题解 有点像集合的交并操作,直接利用set进行处理,因为set有去重的功能,而且set是利用红黑树实现的,查找速度快O(logN). 代码如下: #include<cst ...
- 关于textarea的应用--onchage,onpropertychange,oninput
oninput,onpropertychange,onchange的用法 1.onchange触发事件必须满足两个条件: a)当前对象属性改变,并且是由键盘或鼠标事件激发的(脚本触发无效) b)当前对 ...
- JAVA调用c/c++代码
JNI是Java Native Interface的缩写,中文为JAVA本地调用.使用JNI可以很方便的用我们的Java程序调用C/C++程序.很多时候,某些功能用Java无法实现,比如说涉及到底层驱 ...
- [Android]SDK安装
安装Android环境时,出现的问题 //在国内安装Android环境时,经常会因为Google服务器的原因,出现链接失败的问题. Failed to fetch URL http://dl-ssl. ...
- 如何实现简单的位数组(bit array)(转)
源:如何实现简单的位数组(bit array) 在 comp.lang.c 上面看到一则不错的 FAQ,<How can I implement sets or arrays of bits?& ...
- 关于LCD以及BMP和RGB565
源: 关于LCD以及BMP和RGB565
- JQuery checkbox全选多次点击后无效解决方法
1. jquery设置checkbox时: <input type="checkbox" id="ckAll"/> $(function(){ va ...
- iOS开发中视图控制器ViewControllers之间的数据传递
iOS开发中视图控制器ViewControllers之间的数据传递 这里我们用一个demo来说明ios是如何在视图控制器之间传递重要的参数的.本文先从手写UI来讨论,在下一篇文章中讨论在storybo ...