题目:

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. vue 简易toDoList

    vue+bootstrap简易响应式任务管理表: <!DOCTYPE html> <html> <head> <meta charset="UTF- ...

  2. libc++abi.dylib`__cxa_throw: 使用[AVAudioPlayer play]会产生__cxa_throw异常

    libc++abi.dylib`__cxa_throw: 使用[AVAudioPlayer play]会产生__cxa_throw异常 开发中遇到一个奇怪的异常.我调用AVAudioPlayer pl ...

  3. 【HDOJ5510】Bazinga(KMP)

    题意:给定n个由小写字母组成的字符串,第i个字符串为a[i],求最大的j满足存在1<=i<j,a[i]不是a[j]的子串,无解输出-1 T<=50,n<=500,len[i]& ...

  4. Javascript的SEO优化技巧

    原文发布时间为:2010-10-22 -- 来源于本人的百度文章 [由搬家工具导入] 1.外部崁入javascript在撰写一些比较复杂的网页特效,如下拉式选单等,会产生大量的javascript码, ...

  5. Use ASP.NET and DotNetZip to Create and Extract ZIP Files

    原文发布时间为:2011-02-16 -- 来源于本人的百度文章 [由搬家工具导入] Published: 11 Feb 2011By: Scott MitchellDownload Sample C ...

  6. 用正则表达式模仿Mustache插件的功能

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  7. luogu 3407 散步

    题目链接 题意 按从左到右的顺序给出数轴上的一群人,有人向左走,有人向右走,一旦两人相遇就会停在当前位置,后来走到该位置的人也会停在该位置.问经过一段时间这些人分别在什么位置. 思路 可以将这些人分为 ...

  8. 【Visual Studio】“rc.exe”已退出,代码为 5 ("rc.exe" exited with code 5.)

    [解决方案]找到 rc.exe 所在目录,然后 方法1:添加该目录到 VC++ Directories --> Executable Directories中 方法2:添加到系统变量中的Path ...

  9. TCMalloc小记【转】

    转自:http://blog.csdn.net/chosen0ne/article/details/9338591 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 一 原理 二 ...

  10. 安装glibc错误链接导致系统崩溃,u盘启动紧急救援模式下修复系统。

    Sln 命令  创建动态符号链接 用法 sln source  dest 故障案例:一个误操作 导致了一个不小的故障,输入所有命令都无效,直接系统无法启动. 故障描述 sln /usr/lib64/l ...