Appleman and Card Game

Time Limit: 2000/1000ms (Java/Others)

Problem 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:

Input contains multiple test cases. 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:

For each cases, Print a single integer – the answer to the problem.
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.

Sample Input:

15 10
DZFDFZDFDDDDDDF
6 4
YJSNPI

Sample Output:

82
4
解题思路:简单的贪心,先统计每个字母出现的次数,再排序,最后从次数最多到最小进行贪心,水过!
AC代码:
 #include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+;
typedef long long LL;
int n,k,cnt[];char str[maxn];LL m;//这里要用long long,因为10^10溢出int最大值
int main(){
while(~scanf("%d%d",&n,&k)){
memset(cnt,,sizeof(cnt));
getchar();//吃掉回车符就字符串得影响
scanf("%s",str);
for(int i=;i<(int)strlen(str);++i)cnt[str[i]-'A']++;//映射:对应字母出现的次数
sort(cnt,cnt+);m=;
for(int i=;i>=;--i){//从大往小贪心
if(k>=cnt[i]){m+=(LL)cnt[i]*cnt[i];k-=cnt[i];}//累加所取字母个数的平方
else{m+=k*k;k=;}
}
cout<<m<<endl;
}
return ;
}

ACM_Appleman and Card Game(简单贪心)的更多相关文章

  1. CF 628C --- Bear and String Distance --- 简单贪心

    CF 628C 题目大意:给定一个长度为n(n < 10^5)的只含小写字母的字符串,以及一个数d,定义字符的dis--dis(ch1, ch2)为两个字符之差, 两个串的dis为各个位置上字符 ...

  2. Uva 11729 Commando War (简单贪心)

    Uva 11729  Commando War (简单贪心) There is a war and it doesn't look very promising for your country. N ...

  3. CDOJ 1502 string(简单贪心)

    题目大意:原题链接 相邻两个字母如果不同,则可以结合为前一个字母,如ac可结合为a.现给定一个字符串,问结合后最短可以剩下多少个字符串 解体思路:简单贪心 一开始读题时,就联想到之前做过的一道题,从后 ...

  4. ACM_发工资(简单贪心)

    发工资咯: Time Limit: 2000/1000ms (Java/Others) Problem Description: 作为广财大的老师,最盼望的日子就是每月的8号了,因为这一天是发工资的日 ...

  5. ACM_Ruin of Titanic(简单贪心)

    Ruin of Titanic Time Limit: 2000/1000ms (Java/Others) Problem Description: 看完Titanic后,小G做了一个梦.梦见当泰坦尼 ...

  6. hdu 2037简单贪心--活动安排问题

    活动安排问题就是要在所给的活动集合中选出最大的相容活动子集合,是可以用贪心算法有效求解的很好例子.该问题要求高效地安排一系列争用某一公共资源的活动.贪心算法提供了一个简单.漂亮的方法使得尽可能多的活动 ...

  7. ACM_ICPC hdu-2111(简单贪心算法)

    一道非常简单的贪心算法,但是要注意输入的价值是单位体积的价值,并不是这个物品的总价值!#include <iostream> #include <stdio.h> #inclu ...

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

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

  9. [BZOJ4391][Usaco2015 dec]High Card Low Card dp+set+贪心

    Description Bessie the cow is a huge fan of card games, which is quite surprising, given her lack of ...

随机推荐

  1. EditText实时监测内容

    editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequ ...

  2. python之练习-三层菜单

    今天练习编写显示3层城市名称并可以返回上一层以及退出程序. Readme: 程序概述1:程序名称为:menu_three.py2:记录省,市,县的原始文件名为:areafile3:areafile文件 ...

  3. 从零开始写STL-容器-双端队列

    从零开始写STL-容器-双端队列 什么是双端队列?在介绍vector源码,我们发现在vector前端插入元素往往会引起大量元素的重新分配,双端队列(deque)就是为了解决这一问题,双端队列中在首端和 ...

  4. Free Goodies UVA - 12260

    Petra and Jan have just received a box full of free goodies, and want to divide the goodies between ...

  5. [bzoj2527][Poi2011]Meteors_整体二分_树状数组

    Meteors bzoj-2527 Poi-2011 题目大意:题目链接. 注释:略. 想法: 首先答案可以离线,且具有单调性. 这里的单调性就是随着时间的推移,每个国家收集的陨石数增加. 不难想到整 ...

  6. Servlet基础教程:tutorialspoint-servlet

    来自turorialspoint的Servlet基础教程(英文),官网:https://www.tutorialspoint.com/servlets/index.htm 这个教程在国内已经被翻译成中 ...

  7. tomcat8.5.20配置https

    一.使用cmd下生成证书: d: cd d:/java/jdk/jdk1.8 keytool -v -genkey -alias tomcat -keyalg RSA -keystore D:\jav ...

  8. C#中使用 Oracle的事务与存储过程

    1 存储过程 1.1 不带参数,没有返回值 创建表 create table test (ID number, NAME varchar2(), SEX varchar2(), AGE number, ...

  9. android:“新版飞机大战”源码开源啦!

    今天10.24,为了纪念程序猿的节日,把之前写过的一个"飞机大战"的一个源码开源了. 源码地址:https://github.com/nuptboyzhb/newplanegame ...

  10. 总结react中遇到的坑和一些小的知识点

    在使用react 中经常会遇到各种个样的问题,如果对react不熟悉则会对遇到的问题感到莫名其妙而束手无策,接下来分析一下react中容易遇到的问题和注意点. 1.setState()是异步的this ...