题目链接: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?

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 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张,得分为每次选出牌的面值与选该牌的数量。求最大得分。

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(贪心)的更多相关文章

  1. CodeForces - 462B Appleman and Card Game

    是一道简单题 将字母从个数多到小排序 然后 再按题目算法得到最多 但是注意 数据类型声明 money要为long long #include <iostream> #include < ...

  2. Codeforces 263B. Appleman and Card Game

    B. Appleman and Card Game time limit per test  1 second memory limit per test  256 megabytes input  ...

  3. 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 ...

  4. Codeforces 437C The Child and Toy(贪心)

    题目连接:Codeforces 437C  The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...

  5. Codeforces 461B Appleman and Tree(木dp)

    题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...

  6. Codeforces Round #546 (Div. 2) D 贪心 + 思维

    https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...

  7. 【BZOJ4391】[Usaco2015 dec]High Card Low Card(贪心)

    [BZOJ4391][Usaco2015 dec]High Card Low Card(贪心) 题面 BZOJ 题解 预处理前缀后缀的结果,中间找个地方合并就好了. #include<iostr ...

  8. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  9. Codeforces Educational Codeforces Round 3 C. Load Balancing 贪心

    C. Load Balancing 题目连接: http://www.codeforces.com/contest/609/problem/C Description In the school co ...

随机推荐

  1. 解决Jetty Maven Plugin:Please initialize the log4j system properly(转)

    解决Jetty Maven Plugin:Please initialize the log4j system properly.Jetty Maven Plugin环境: <plugin> ...

  2. 实用推荐:12款Linux系统恢复工具

    12款Linux系统恢复工具 电脑死机,系统崩溃,总会给电脑使用者带来一定的损失.你是否不小心删除你的纪念图片?安装新系统时候,擦除了分区表?无法读取旧CD里面的数据?别急嘛-我们将会给您推荐一些免费 ...

  3. ios7下二维码功能的实现

    苹果公司升级到IOS7后自己的PassBook自带二维码扫描功能,所以现在使用二维码功能不需要在借助第三方库了 使用前请先导入AVFoundation.frameWork // //  YHQView ...

  4. fedora 下安装 文泉驿正黑体

    1. 可以到文泉驿正黑体的 主页 http://wenq.org/wqy2/index.cgi?%E9%A6%96%E9%A1%B5 最好能去官网表示一下支持 2. 也可以直接使用命令 :  sudo ...

  5. Maven--几个需要补充的问题(三)

    <Maven--搭建开发环境(一)> <Maven--构建企业级仓库(二)> <Maven—几个需要补充的问题(三)> 前两篇由于篇幅太长,为了给读者理解方便,这篇 ...

  6. IOS 时间格式 时间转换 大总结

    //实例化一个NSDateFormatter对象 NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init]; //设定时间格式,这里 ...

  7. SQL语句简单记录

    SQL SERVER 新增列与默认值 alter table 表名 add 列明 bit default 0 not null 删除列(容易删除失败) alter table 表名 drop colu ...

  8. 【Hibernate】Remember that ordinal parameters are 1-based!

    此错误的官方解释:1.当hql中不需要参数,而传递了参数导致,2.set参数时没有从0开始. 但此问题不属这两种. 检查导入的libraries无错误. 最后在网络搜索到:http://qihaihu ...

  9. CodeForces 546D Soldier and Number Game 打表(求质因子个数)

    题目:戳我这个题与HDUOJ 5317有异曲同工之妙 题意:题意看懂了上面的一大串英文之后其实很简单,就是给你一个正整数n,问你n有多少个质因子,不过这里n是通过a!/b!给定的,也就是说n=(a!/ ...

  10. 【QT相关】类头文件解读、QT编辑模式、读取text文本

    Wizard产生的头文件类包含了必须的#include文件.构造函数.析构函数和UI对象: #include <QMainWindow> namespace Ui {class Notep ...