NUC_TeamTEST_B(贪心)
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
15 10
DZFDFZDFDDDDDDF
82
6 4
YJSNPI
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.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int max_size = 100000+10; int main()
{
int n, k;
char str[max_size];
long long arry[26];
while(scanf("%d %d", &n, &k) == 2)
{
getchar();
memset(arry, 0, sizeof(arry));
scanf("%s", str);
int len = strlen(str);
int i;
for(i = 0; i < len; ++i)
{
arry[str[i]-'A']++;
}
i = 25;
sort(arry, arry+26);
//printf("%d\n", arry[25]);
long long sum = 0;
long long now = k;
do{
if(now > arry[i])
{
sum += arry[i] * arry[i];
now -= arry[i];
//printf("%d\n", sum);
i--;
}else if(now <= arry[i]){
sum += now*now; ///这个比较坑,没考虑到超int
break;
}
}while(now != 0);
cout << sum << endl;
}
return 0;
}
NUC_TeamTEST_B(贪心)的更多相关文章
- BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]
1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1383 Solved: 582[Submit][St ...
- HDOJ 1051. Wooden Sticks 贪心 结构体排序
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]
1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 786 Solved: 391[Submit][S ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【BZOJ-4245】OR-XOR 按位贪心
4245: [ONTAK2015]OR-XOR Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 486 Solved: 266[Submit][Sta ...
- code vs 1098 均分纸牌(贪心)
1098 均分纸牌 2002年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 有 N 堆纸牌 ...
- 【BZOJ1623】 [Usaco2008 Open]Cow Cars 奶牛飞车 贪心
SB贪心,一开始还想着用二分,看了眼黄学长的blog,发现自己SB了... 最小道路=已选取的奶牛/道路总数. #include <iostream> #include <cstdi ...
- 【贪心】HDU 1257
HDU 1257 最少拦截系统 题意:中文题不解释. 思路:网上有说贪心有说DP,想法就是开一个数组存每个拦截系统当前最高能拦截的导弹高度.输入每个导弹高度的时候就开始处理,遍历每一个拦截系统,一旦最 ...
随机推荐
- Mysql复制之路由
在主从复制读写分离的思路下,要想使所有写都到MasterServer,所有读都路由到Slave Server;就需要使用一些路由策略. 可以使用MysqlProxy[Mysql代理],据说MysqlP ...
- 八皇后(dfs+回溯)
重看了一下刘汝佳的白板书,上次写八皇后时并不是很懂,再写一次: 方法1:逐行放置皇后,然后递归: 代码: #include <bits/stdc++.h> #define MAXN 8 # ...
- ZOJ 2136 Longest Ordered Subsequence
#include<time.h> #include <cstdio> #include <iostream> #include<algorithm> # ...
- haproxy 安装 各个参数的测试
---------------------------------------------------------------------------------------------------- ...
- EasyUi – 4.datagrid
测试的时候用Json来测试就好啦. <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> ...
- 与你相遇好幸运,Tippecanoe用法
//todo 基本用法: tippecanoe -o file.mbtiles [file.json ...] 参数解释: <必须> -o myFileName.mbtiles 或者 ...
- 让那些为Webkit优化的网站也能适配IE10(转载)
转载地址:http://www.w3cplus.com/css3/adapting-your-webkit-optimized-site-for-internet-explorer-10.html 特 ...
- ASP.NET MVC 伪静态的实现
public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.Ignore ...
- SQL数据库约束
针对维护数据库的完整性,关系型数据库SQL提供了数据约束来管理数据,常用的约束有:外键.唯一.主键. 主键约束:标识数据的唯一,便于数据查询索引: 唯一约束:保证数据的唯一性:常用语法 alter t ...
- 【Agorithm】一次一密加密解密算法
#include<iostream> #include<cstdio> #include<cstdlib> #include<ctime> #inclu ...