题目:

B. Appleman and Card Game
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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?

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 test(s)
input
15 10
DZFDFZDFDDDDDDF
output
82
input
6 4
YJSNPI
output
4
Note

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 cha得残暴又是万恶的 long long

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char s[110000];
int j[500],cn[500];
void solve()
{
int n,k;
long long ans=0;
scanf("%d%d",&n,&k);
scanf("%s",s);
int len=strlen(s);
for(int i=0; i<len; i++)
{
j[s[i]]++;
}
while(k)
{
k--;
int w=0,Max=0;
for(int i='A'; i<='Z'; i++)
{
if(j[i]>Max&&cn[i]<j[i])
{
Max=j[i];
w=i;
}
}
cn[w]++;
}
for(int i='A'; i<='Z'; i++)
{
ans+=1LL*cn[i]*cn[i];
}
cout<<ans<<endl;
}
int main()
{
solve();
return 0;
}


Codeforces Round #263 (Div. 2) proB的更多相关文章

  1. 贪心 Codeforces Round #263 (Div. 2) C. Appleman and Toastman

    题目传送门 /* 贪心:每次把一个丢掉,选择最小的.累加求和,重复n-1次 */ /************************************************ Author :R ...

  2. Codeforces Round #263 (Div. 2)

    吐槽:一辈子要在DIV 2混了. A,B,C都是简单题,看AC人数就知道了. A:如果我们定义数组为N*N的话就不用考虑边界了 #include<iostream> #include &l ...

  3. Codeforces Round #263 (Div. 1)

    B 树形dp 组合的思想. Z队长的思路. dp[i][1]表示以i为跟结点的子树向上贡献1个的方案,dp[i][0]表示以i为跟结点的子树向上贡献0个的方案. 如果当前为叶子节点,dp[i][0] ...

  4. Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】

    题意:给了一棵树以及每个节点的颜色,1代表黑,0代表白,求将这棵树拆成k棵树,使得每棵树恰好有一个黑色节点的方法数 解法:树形DP问题.定义: dp[u][0]表示以u为根的子树对父亲的贡献为0 dp ...

  5. Codeforces Round #263 (Div. 2) D. Appleman and Tree(树形DP)

    题目链接 D. Appleman and Tree time limit per test :2 seconds memory limit per test: 256 megabytes input ...

  6. Codeforces Round #263 (Div. 2) A B C

    题目链接 A. Appleman and Easy Task time limit per test:2 secondsmemory limit per test:256 megabytesinput ...

  7. Codeforces Round #263 (Div. 1) C. Appleman and a Sheet of Paper 树状数组暴力更新

    C. Appleman and a Sheet of Paper   Appleman has a very big sheet of paper. This sheet has a form of ...

  8. Codeforces Round #263 (Div. 2) proC

    题目: C. Appleman and Toastman time limit per test 2 seconds memory limit per test 256 megabytes input ...

  9. Codeforces Round #263 (Div. 2)C(贪心,联想到huffman算法)

    数学家伯利亚在<怎样解题>里说过的解题步骤第二步就是迅速想到与该题有关的原型题.(积累的重要性!) 对于这道题,可以发现其实和huffman算法的思想很相似(可能出题人就是照着改编的).当 ...

随机推荐

  1. Python学习杂记_3_字符串操作的常用方法

    字符串操作 字符串是可以通过下标来进行取值的,但是由于字符串是不可变变量,不能通过下标来修改它的值(形式如 字符串[下标]),下标从0开始,最大下标值是字符串长度减1,即len(string)-1 P ...

  2. poj 2100(尺取法)

    Graveyard Design Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 6107   Accepted: 1444 ...

  3. LeetCode OJ-- Maximum Subarray @

    https://oj.leetcode.com/problems/maximum-subarray/ 给了一个数组一列数,求其中的连续子数组的最大和. O(n)复杂度 class Solution { ...

  4. 16Aspx.com-将15位身份证转换成18位

    //********************************************************************************* //将15位身份证转换成18位时 ...

  5. fauxbar.bak

    {"options": { "almostdone":"0", "backup_searchEngines":" ...

  6. Debugging a SQL Server query with WinDbg

    Debugging a SQL Server query with WinDbg May 13, 2014 · Klaus Aschenbrenner · 5 Comments (Be sure to ...

  7. HTML DOM介绍

    HTML DOM定义了一系列的对象,以及访问和处理HTML的方法.通过DOM可以浏览所有的HTML元素,不但可以修改或者删除元素的文本和属性,而且可以创建新的元素. 一.首先对一个元素进行操作前,要得 ...

  8. 【ecplise】快捷键 集合

    1.查看本方法在哪里被调用过 光标放在本方法名上 快捷键: Ctrl+Shift+G

  9. Linux进程的睡眠和唤醒

    1   Linux进程的睡眠和唤醒 在Linux中,仅等待CPU时间的进程称为就绪进程,它们被放置在一个运行队列中,一个就绪进程的状态标志位为TASK_RUNNING.一旦一个运行中的进程时间片用完, ...

  10. PowerDesigner16 安装包及破解文件

    一.准备工作 PowerDesigner16 安装包:http://pan.baidu.com/s/11Qv9H 或http://cloud.suning.com/cloud-web/share/li ...