CF_462B_ApplemanAndCardGame
来源:互联网 发布:ios天气插件数据不跟新 编辑:程序博客网 时间:2024/05/30 22:57
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 10DZFDFZDFDDDDDDF
82
6 4YJSNPI
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.
题意要拿k张卡片
每张卡片可以得到同种卡片总张数的钱
问这个收益的最大值
那么这个问题只需要简单的贪心就可以了
当卡片数目都小于k时优先拿卡片数量最多的即可
注意下数据范围
#include <iostream>#include <stdio.h>using namespace std;const int M=1e5+5;char s[M];int ns[26];int main(){ int n,k; long long mo=0; scanf("%d%d%s",&n,&k,s); for(int i=0;i<n;i++) ns[s[i]-'A']++; while(k) { int maxn=0;int p; for(int i=0;i<26;i++) { if(ns[i]>k) { //cout<<k; mo+=(long long)k*k;k=0; break; } if(ns[i]>maxn) { p=i; maxn=ns[i]; } } if(k) { k-=maxn; ns[p]=0; mo+=(long long)maxn*maxn; } } printf("%I64d\n",mo); return 0;}