ACM_Appleman and Card Game(简单贪心)
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(简单贪心)的更多相关文章
- CF 628C --- Bear and String Distance --- 简单贪心
CF 628C 题目大意:给定一个长度为n(n < 10^5)的只含小写字母的字符串,以及一个数d,定义字符的dis--dis(ch1, ch2)为两个字符之差, 两个串的dis为各个位置上字符 ...
- Uva 11729 Commando War (简单贪心)
Uva 11729 Commando War (简单贪心) There is a war and it doesn't look very promising for your country. N ...
- CDOJ 1502 string(简单贪心)
题目大意:原题链接 相邻两个字母如果不同,则可以结合为前一个字母,如ac可结合为a.现给定一个字符串,问结合后最短可以剩下多少个字符串 解体思路:简单贪心 一开始读题时,就联想到之前做过的一道题,从后 ...
- ACM_发工资(简单贪心)
发工资咯: Time Limit: 2000/1000ms (Java/Others) Problem Description: 作为广财大的老师,最盼望的日子就是每月的8号了,因为这一天是发工资的日 ...
- ACM_Ruin of Titanic(简单贪心)
Ruin of Titanic Time Limit: 2000/1000ms (Java/Others) Problem Description: 看完Titanic后,小G做了一个梦.梦见当泰坦尼 ...
- hdu 2037简单贪心--活动安排问题
活动安排问题就是要在所给的活动集合中选出最大的相容活动子集合,是可以用贪心算法有效求解的很好例子.该问题要求高效地安排一系列争用某一公共资源的活动.贪心算法提供了一个简单.漂亮的方法使得尽可能多的活动 ...
- ACM_ICPC hdu-2111(简单贪心算法)
一道非常简单的贪心算法,但是要注意输入的价值是单位体积的价值,并不是这个物品的总价值!#include <iostream> #include <stdio.h> #inclu ...
- CodeForces 462B Appleman and Card Game(贪心)
题目链接:http://codeforces.com/problemset/problem/462/B Appleman has n cards. Each card has an uppercase ...
- [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 ...
随机推荐
- POJ 2785_4 Values whose Sum is 0
题意: A,B,C,D四组数中各取一个数,使这四个数相加为0,问有多少组取法? 分析: 四个数列有n4种取法,规模较大,但是可以将他们分成AB,CD两组,分别枚举,最后再合并. 代码: #includ ...
- [bzoj3513][MUTC2013]idiots_FFT
idiots bzoj-3513 MUTC-2013 题目大意:给定$n$根木棍,问随机选择三根能构成三角形的概率. 注释:$1\le n\le 3\cdot 10^5$,$1\le a_i\le 1 ...
- 携程Apollo(阿波罗)配置中心在Spring Boot项目快速集成
前提:先搭建好本地的单机运行项目:http://www.cnblogs.com/EasonJim/p/7643630.html 说明:下面的示例是基于Spring Boot搭建的,对于Spring项目 ...
- rsyslogd系统日志服务总结
简单介绍 syslog系统日志服务协议,标准出来的比较晚 用于记录系统日志或者用户程序产生的日志 采用C/S架构,本地可以通过socket和syslogd守护进程通讯,远程通过TCP/UDP协议通信, ...
- spring 数据源JNDI 基于tomcat mysql配置
关键代码 <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean&q ...
- CSS+Jquery实现QQ分组列表
实现效果图如下: 说明: 1.css隐藏分组下的好友内容: 2.Jquery实现点击分组项事件,实现好友内容的显示和隐藏: 3.样式1,可展开多个分组:样式2,只能有一个分组展开: 源码: <! ...
- 【CV论文阅读】Image Captioning 总结
初次接触Captioning的问题,第一印象就是Andrej Karpathy好聪明.主要从他的两篇文章开始入门,<Deep Fragment Embeddings for Bidirectio ...
- 改动ScrollView的滑动速度和解决ScrollView与ViewPager的冲突
话不多说,非常easy,能够从凝视中知道做法,直接上代码: 1.改动ScrollView的滑动速度: public class MyHorizontalScrollView extends Horiz ...
- 仅仅需一步教你解决Win10下Android Studio terminal无法使用的问题
Android Studio集成和加入了一些有用的工具.当中一个便是terminal. 在Windows平台下Android Studio中的terminal在原理上实际使用的是window中的cmd ...
- HTML5开发移动web应用——SAP UI5篇(6)
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...