CodeForces462B
Appleman and Card Game
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
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
//2016.8.2
#include<iostream>
#include<algorithm>
#include<cstring> using namespace std; int ch[]; bool cmp(int a, int b)
{
return a>b;
} int min(int a, int b)
{
return a > b ? b : a;
} int main()
{
int n, k;
long long ans, x;
char c;
while(cin >> n >> k)
{
memset(ch, , sizeof(ch));
getchar();
ans = ;
for(int i = ; i < n; i++)
{
c = getchar();
ch[c-'A']++;
}
sort(ch, ch+, cmp); for(int i = ; k > ;i++)
{
x = min(k, ch[i]);
ans += x*x;
k -= x;
}
cout << ans << endl;
}
return ;
}
CodeForces462B的更多相关文章
随机推荐
- 推荐!国外程序员整理的Java资源大全
http://www.importnew.com/14429.html 本文由 ImportNew - 唐尤华 翻译自 github akullpp.欢迎加入翻译小组.转载请见文末要求. 构建 这里搜 ...
- lwip移植到stm32上-enc28j60,103mcu(2)
前面小玩了一下ucos和lwip,但是都还不是真正的网络多任务,真正的网络多任务应该是什么样子的呢?应该是有一个专门的任务负责网络的通讯,他负责将数据发送出去,将数据接收回来,而其他的需要用到网络的任 ...
- Codeforces #350
A题: 题意:判断火星上的节假日最多和最少 分析:除以7,然后我们对原数模7的余数进行判断一下即可 #include <iostream> #include <cstdio> ...
- WCF应用场景
WCF全称Windows Communication Foundation,是Microsoft为构建面向服务的应用提供的分布式通信编程框架,是.NET Framework 3.5的重要组成部分.使用 ...
- SSO单点登录设计
关键字: 单点登录 SSO Session 单点登录在现在的系统架构中广泛存在,他将多个子系统的认证体系打通,实现了一个入口多处使用,而在架构单点登录时,也会遇到一些小问题,在不同的应用环境中可以采用 ...
- C++类的存储(部分可用与c的结构体)
c++中最重要的就是类,那么给你一个类的对象,你知道它在内存中如何存储的吗?它占内存中多少个字节? 首先确定类的构成: 1,数据成员:可以是内置类型,类类型. 2,函数成员:虚函数,非虚函数 1)数据 ...
- 一种新的隐藏-显示模式诞生——css3的scale(0)到scale(1)
.dropdown-menu { background: rgba(255, 255, 255, 0.98) none repeat scroll 0 0; box-shadow: 0 1px 2 ...
- AJAX异步加载
AJAX含义: 即"Asynchronous Javascript And XML"(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. AJAX可以跨 ...
- Android动画之translate(位移动画)(转)
转自:http://www.cnblogs.com/bavariama/archive/2013/01/29/2881225.html 上一篇文章讲了 Android的左右滑动切换,实现过程是非常简单 ...
- iOS 加载动态库报错问题
dyld: Library not loaded: @rpath Referenced from: Reason: no suitable image found. Did find: 要么使用静态 ...