Code Forces 462B Appleman and Card Game
2015-08-16 14:21
459 查看
D - D
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d
& %I64u
SubmitStatusPracticeCodeForces
462B
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
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 Input
Input
Output
Input
Output
Hint
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个字符,使得得分最大。求最大值。
要找最大,就要贪心。排序后从最大值开始找。
注意要用long long.
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d
& %I64u
SubmitStatusPracticeCodeForces
462B
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
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 Input
Input
15 10 DZFDFZDFDDDDDDF
Output
82
Input
6 4 YJSNPI
Output
4
Hint
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个字符,使得得分最大。求最大值。
要找最大,就要贪心。排序后从最大值开始找。
注意要用long long.
#include <stdio.h> #include <string.h> #include <algorithm> #define N 100005 using namespace std; int n; long long k; char s ; long long letter[26]; int main() { memset(letter,0,sizeof(letter)); scanf("%d%I64d",&n,&k); scanf("%s",s); for(int i=0;i<n;i++) letter[s[i]-'A']++; sort(letter,letter+26); long long ans=0; for(int i=25;i>=0;i--) { if(letter[i]<=k) { ans+=letter[i]*letter[i]; k-=letter[i]; } else { ans=ans+k*k; break; } } printf("%I64d\n",ans); return 0; }
相关文章推荐
- [转载] iPhone/iOS Core Animation开发总结(CALayer)
- 下拉刷新,在 ActionBar 上显示效果图
- Unity脚本事件执行顺序
- Android的事件处理之监听与回调
- swift入门-实现简单的登录界面
- iOS
- 布局动画 属性初始 android:animateLayoutChanges
- quick-cocos2d-x游戏开发【5】——创建菜单
- Android:EditText中切换密码的显示与隐藏
- android 5.1 源码编译adb
- Unity 3D: Working with touch input
- Java微信支付(公众号,扫码)
- Android中自定义RatingBar
- webview 中使用css适配简单的页面
- 解决百度地图230错误(App Scode码校验失败)
- SDK学院--易接网游SDK中间件接入使用说明-Unity3D游戏接口
- Android 使用android-support-multidex解决Dex超出方法数的限制问题,让你的应用不再爆棚
- Unity5 音效一
- SDK学院--易接网游SDK中间件接入使用说明-Unity3D游戏接口
- cocos2d-x3.0 lua学习(一个)