Codeforces 263B. Appleman and Card Game
1 second
256 megabytes
standard input
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?
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 10
DZFDFZDFDDDDDDF
82
6 4
YJSNPI
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.
解题:贪心。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
int letter[];
char str[];
bool cmp(const int &x,const int &y){
return x > y;
}
int main() {
int n,k;
LL ans;
while(~scanf("%d %d",&n,&k)){
memset(letter,,sizeof(letter));
scanf("%s",str);
for(int i = ; str[i]; i++) letter[str[i]-'A']++;
sort(letter,letter+,cmp);
for(int i = ans = ; k && i < ; i++){
ans += 1LL*min(k,letter[i])*min(k,letter[i]);
k -= min(k,letter[i]);
}
cout<<ans<<endl;
}
return ;
}
Codeforces 263B. Appleman and Card Game的更多相关文章
- CodeForces 462B Appleman and Card Game(贪心)
题目链接:http://codeforces.com/problemset/problem/462/B Appleman has n cards. Each card has an uppercase ...
- CodeForces - 462B Appleman and Card Game
是一道简单题 将字母从个数多到小排序 然后 再按题目算法得到最多 但是注意 数据类型声明 money要为long long #include <iostream> #include < ...
- Codeforces 461B Appleman and Tree(木dp)
题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...
- codeforces 462C Appleman and Toastman 解题报告
题目链接:http://codeforces.com/problemset/problem/461/A 题目意思:给出一群由 n 个数组成的集合你,依次循环执行两种操作: (1)每次Toastman得 ...
- Codeforces 388C Fox and Card Game (贪心博弈)
Codeforces Round #228 (Div. 1) 题目链接:C. Fox and Card Game Fox Ciel is playing a card game with her fr ...
- Codeforces 461B. Appleman and Tree[树形DP 方案数]
B. Appleman and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces 461B Appleman and Tree
http://codeforces.com/problemset/problem/461/B 思路:dp,dp[i][0]代表这个联通块没有黑点的方案数,dp[i][1]代表有一个黑点的方案数 转移: ...
- CodeForces 461B Appleman and T
题目链接:http://codeforces.com/contest/461/problem/B 题目大意: 给定一课树,树上的节点有黑的也有白的,有这样一种分割树的方案,分割后每个子图只含有一个黑色 ...
- Codeforces 461B Appleman and Tree:Tree dp
题目链接:http://codeforces.com/problemset/problem/461/B 题意: 给你一棵树(编号从0到n-1,0为根节点),每个节点有黑白两种颜色,其中黑色节点有k+1 ...
随机推荐
- bzoj 2199: [Usaco2011 Jan]奶牛议会【2-SAT】
好久没写2-SAT了啊,还以为是网络流 设点x为选,x'为不选,因为一头牛至少要满足一个条件,所以对于牛条件的两个点,选了一个不符合的点,就要选另一个符合的点,这样连两条边 然后枚举所有议案的选和不选 ...
- 17年day4
/* 嗯,又一天 上午考试,睡了两觉(我不会把我第二觉流了口水这件事说出去) 状态比较玄学,上午困得要死,下午无比精神(感觉NOIP要完). 复习了概率期望.发现以前做过的题还是不会做,好像连印象都比 ...
- 在 Vue 项目中(vue-cli2,vue-cli3)使用 pug 简化 HTML 的编写
使用 pug 的原因: 使得 HTML 写起了来更加清晰和快捷 用法: Vue 的用法没有变化: <template lang="pug"> transition(na ...
- SQL 事务篇和约束
数据库事务: 数据库事务(Database Transaction) ,是指作为单个逻辑工作单元执行的一系列操作,要么完全地执行,要么完全地不执行 事务是恢复和并发控制的基本单位.事务应该具有4个属性 ...
- HTML空格占位
它叫不换行空格,全称No-Break Space,它是最常见和我们使用最多的空格,大多数的人可能只接触了 ,它是按下space键产生的空格.在HTML中,如果你用空格键产生此空格,空格是不会累加的 ...
- es6 export-from用法
语法: export * from "foo"; export {foo} from "foo"; export {foo, bar} from "f ...
- CSS垂直居中和水平居中的几种方法
垂直居中 方法一 这个方法把div 的显示方式设置为表格,因此我们可以使用表格的 vertical-align属性. <!DOCTYPE html> <html lang=" ...
- Selenium Grid操作使用指南
一.实现串行多浏览器执行脚本1.启动selenium-server-standalonejava -jar selenium-server-standalone-2.44.0.jar2.脚本代码 fr ...
- Sqoop hive 和mysql 交互 完整案例
本文完成:在hive里建管理表:注入部分数据:利用sqoop导入mysql中 期间:解决中文乱码问题 飞行报告故障表 建表命令 查看表 人工灌入少量数据 Windows系统向Linux系统数据传输 ...
- java web 学习笔记 - Java Bean
1. Java Bean 是一个简单的 java 类,一般放在WEB-INF下的 classes目录下(如果没有则需要手工新建) 一个简单的Bean包括属性,getter ,setter方法,如果没有 ...