Codeforces 263B. Appleman and Card Game
1 second
256 megabytes
standard input
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?
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.
Print a single integer – the answer to the problem.
15 10
DZFDFZDFDDDDDDF
82
6 4
YJSNPI
4
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 ;
}
最新文章
- Excel Sheet Column Title
- Spring(三)AOP面向切面编程
- 最详细的hadoop2.2.0集群的HA高可靠的最简单配置
- hdwiki 学习笔记 01
- Android SDK生成时,自定义文件名称,而非系统第一分配的app-release.apk
- ";Your local changes to the following files would be overwritten by merge"; on git
- Can brain stimulation aid memory and brain health?
- Golang学习 - fmt 包
- 9、JPA_映射双向一对一的关联关系
- 12天学好C语言——记录我的C语言学习之路(Day 8)
- PHP 表单防止刷新提交的方法
- html5重力感应事件
- EasyMvc--让MVC区域开发更Easy(提供源码下载)
- Linux的资源管理器
- linux18.04+jdk11.0.2+hadoop3.1.2部署伪分布式
- Druid数据库连接池
- .net core 中的 DependencyInjection - IOC
- 【读书笔记】iOS-OCUnit-单元测试
- 2019-03-05-day004-列表操作
- LINUX block I/O --systemtap