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

解题:贪心。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
int letter[];
char str[];
bool cmp(const int &x,const int &y){
return x > y;
}
int main() {
int n,k;
LL ans;
while(~scanf("%d %d",&n,&k)){
memset(letter,,sizeof(letter));
scanf("%s",str);
for(int i = ; str[i]; i++) letter[str[i]-'A']++;
sort(letter,letter+,cmp);
for(int i = ans = ; k && i < ; i++){
ans += 1LL*min(k,letter[i])*min(k,letter[i]);
k -= min(k,letter[i]);
}
cout<<ans<<endl;
}
return ;
}

Codeforces 263B. Appleman and Card Game的更多相关文章

  1. CodeForces 462B Appleman and Card Game(贪心)

    题目链接:http://codeforces.com/problemset/problem/462/B Appleman has n cards. Each card has an uppercase ...

  2. CodeForces - 462B Appleman and Card Game

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

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

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

  4. codeforces 462C Appleman and Toastman 解题报告

    题目链接:http://codeforces.com/problemset/problem/461/A 题目意思:给出一群由 n 个数组成的集合你,依次循环执行两种操作: (1)每次Toastman得 ...

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

  6. Codeforces 461B. Appleman and Tree[树形DP 方案数]

    B. Appleman and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  7. Codeforces 461B Appleman and Tree

    http://codeforces.com/problemset/problem/461/B 思路:dp,dp[i][0]代表这个联通块没有黑点的方案数,dp[i][1]代表有一个黑点的方案数 转移: ...

  8. CodeForces 461B Appleman and T

    题目链接:http://codeforces.com/contest/461/problem/B 题目大意: 给定一课树,树上的节点有黑的也有白的,有这样一种分割树的方案,分割后每个子图只含有一个黑色 ...

  9. Codeforces 461B Appleman and Tree:Tree dp

    题目链接:http://codeforces.com/problemset/problem/461/B 题意: 给你一棵树(编号从0到n-1,0为根节点),每个节点有黑白两种颜色,其中黑色节点有k+1 ...

随机推荐

  1. E20171229-hm

    specification  n. 规格; 说明书; 详述;

  2. [SDOI2011]消防(单调队列,树的直径,双指针)

     消防 2011年  时间限制: 2 s  空间限制: 256000 KB  题目等级 : 大师 Master   题目描述 Description 某个国家有n个城市,这n个城市中任意两个都连通且有 ...

  3. hdu5922Minimum’s Revenge

    Minimum's Revenge Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  4. IBatis的分页研究

    IBatis的分页研究 博客分类: Ibatis学习   摘自: http://cpu.iteye.com/blog/311395 yangtingkun   Oracle分页查询语句 ibaits. ...

  5. What is JSON

    JSON (JavaScript Object Notation)  是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON具有以下这些形式: 对象是一个无序的“‘名称/值’对” ...

  6. 隐藏win10任务栏输入法M图标

    在任务栏右键=>任务栏设置=>打开或关闭系统图标=>(关闭)输入指示

  7. Anniversary Cake

    Anniversary Cake Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15704   Accepted: 5123 ...

  8. HDU_1011_Starship Troopers_树型dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 Starship Troopers Time Limit: 10000/5000 MS (Jav ...

  9. Java基础(七)--Exception异常处理

    发现错误的理想时机是程序运行之前(编译期),然后不太现实,很多异常无法被发现(特别是业务上的数据),需要在运行时解决. 错误恢复机制保证代码健壮性的方式,异常处理在程序中很常见,也是必须的,必须考虑有 ...

  10. java虚拟机(八)--java性能监控与故障处理工具

    问题定位: 除了个人经验,知识,工具也是很重要的,通过数据进行问题分析,包括:运行日志.异常堆栈.GC日志.线程快照(threaddump/javacore文件 ).堆转储快照(heapdump/hp ...