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. 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?
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.
Print a single integer – the answer to the problem.
15 10
DZFDFZDFDDDDDDF
82
6 4
YJSNPI
4
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张,得分为每次选出牌的面值与选该牌的数量。求最大得分。
PS:
贪心。从牌数多的面值開始选。
代码例如以下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef __int64 LL;
int main()
{
LL n, k;
LL a[47];
char s[100017];
while(~scanf("%I64d%I64d",&n,&k))
{
memset(a,0,sizeof(a));
scanf("%s",s);
for(int i = 0; i < n; i++)
{
a[s[i]-'A']++;
}
sort(a,a+26);
LL ans = 0;
for(int i = 25; i >= 0; i--)
{
if(k >= a[i])
{
ans+=a[i]*a[i];
k -= a[i];
if(k == 0)
break;
}
else
{
ans+=k*k;
break;
}
}
printf("%I64d\n",ans);
}
return 0;
}
CodeForces 462B Appleman and Card Game(贪心)的更多相关文章
- CodeForces - 462B Appleman and Card Game
是一道简单题 将字母从个数多到小排序 然后 再按题目算法得到最多 但是注意 数据类型声明 money要为long long #include <iostream> #include < ...
- Codeforces 263B. Appleman and Card Game
B. Appleman and Card Game time limit per test 1 second memory limit per test 256 megabytes input ...
- Codeforces 388C Fox and Card Game (贪心博弈)
Codeforces Round #228 (Div. 1) 题目链接:C. Fox and Card Game Fox Ciel is playing a card game with her fr ...
- Codeforces 437C The Child and Toy(贪心)
题目连接:Codeforces 437C The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...
- Codeforces 461B Appleman and Tree(木dp)
题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...
- Codeforces Round #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...
- 【BZOJ4391】[Usaco2015 dec]High Card Low Card(贪心)
[BZOJ4391][Usaco2015 dec]High Card Low Card(贪心) 题面 BZOJ 题解 预处理前缀后缀的结果,中间找个地方合并就好了. #include<iostr ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- Codeforces Educational Codeforces Round 3 C. Load Balancing 贪心
C. Load Balancing 题目连接: http://www.codeforces.com/contest/609/problem/C Description In the school co ...
随机推荐
- QTcpSocket通信编程时阻塞与非阻塞的问题
目标,qt程序作为客户端,windows下winsock作为服务器端,实现两端通信. 开始时写了一个小函数测试: [cpp] view plaincopy QTcpSocket tmpSock; t ...
- Qt技巧:Win7下打包发布Qt程序(解释的比较清楚,把exe和dll伪装合并成一个文件)
转自:http://www.stardrad.com/blog/qt-5%E7%A8%8B%E5%BA%8F%E5%9C%A8windows%E4%B8%8A%E7%9A%84%E5%8F%91%E5 ...
- 经典switch
- vs提示“当前不会命中断点,源代码与原始版本不同”的一种解决办法
将出问题的cpp文件用notepad打开,另存为‘unicode’类型,覆盖源文件即可
- B - 畅通工程(并查集)
对并查集理解之后就可以做这种题了,虽说这种题做的不多,这道题做过才这么快搞定,可是还是挺happy滴,加油 Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接 ...
- TTL电平与RS232电平的区别
TTL电平与RS232电平的区别 工作中遇到一个关于电平选择的问题,居然给忘记RS232电平的定义了,当时无法反应上来,回来之后查找资料才了解两者之间的区别,视乎两年多的时间,之前非常熟悉的一些常 ...
- Visual Studio 2012中编写C程序
换了win7系统后,突然发现VC++6.0不兼容了,我听说有的同学的行,反正我是不行. 那就用VS2012呗.... 我们来看看怎么用: 打开文件->新建->项目,新建一个项目 选择win ...
- BZOJ 2157: 旅游( 树链剖分 )
树链剖分.. 样例太大了根本没法调...顺便把数据生成器放上来 -------------------------------------------------------------------- ...
- OpenGL ES 如何能看到一个物体内部和象3dmax中能只显示网格线
上一篇 OpenGL ES 正反面设置指令 中分析了正反面的判区方法,那么正反面有什么用呢?接下来我们就要引入一个叫做背面消除的概念.在3dmax中有个选项,当你用挤压修改器挤出一个中空的长方体时,在 ...
- django学习之Model(四)MakingQuery
上一篇写到MakingQuey中的filter,本篇接着来. 10)-扩展多值的关系 如果对一个ManyToManyField或ForeignKey的表进行filter过滤查询的话,有2中方法可以用. ...