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 4300: 绝世好题【dp】
设f[i][j]表示数列到i为止最后一项第j位为1的最大子序列长度,每次从i-1中1<<j&a[i]!=0的位+1转移来 然后i维是不需要的,答案直接在dp过程中去max即可 #i ...
- bzoj 3942: [Usaco2015 Feb]Censoring【kmp+栈】
好久没写kmp都不会写了-- 开两个栈,s存当前串,c存匹配位置 用t串在栈s上匹配,栈每次入栈一个原串字符,用t串匹配一下,如果栈s末尾匹配了t则弹栈 #include<iostream> ...
- P3343 [ZJOI2015]地震后的幻想乡
传送门 给积分大佬跪了 再给状压大佬也跪了 //minamoto #include<bits/stdc++.h> #define rint register int #define ll ...
- maptalks 如何加载 ArcGIS 瓦片图层
最近需要加载 ArcGIS 瓦片图层,运行官网加载 ArcGIS 瓦片图层的 demo 是没有问题的.如果把 ArcGIS 瓦片图层 URL 换成是自已发布的 ArcGIS 地图服务,发现加载不出来, ...
- Java常见面试问题: equals()与hashCode()的使用
目录 1 equals()与'=='的区别 2 equals()方法的重写规则 3 为什么重写equals()的同时还需要重写hashCode() 4 JDK 7中对hashCode()方法的改进 5 ...
- Echarts修改legend样式
legend: { icon: 'rect', itemWidth: 20, itemHeight: 10, itemGap: 10}
- 逻辑回归(Logistic Regression)推导
出自BYRans博客:http://www.cnblogs.com/BYRans/ 本文主要讲解分类问题中的逻辑回归.逻辑回归是一个二分类问题. 二分类问题 二分类问题是指预测的y值只有两个取值(0或 ...
- PSP辅助软件开发计划
PSP辅助软件开发计划 作者: 日期:2013年11月14号 1开发目的 鉴于软件开发过程中,程序员往往无法在规定时间内完成任务,而且无法给出拖延的时间从而造成项目进度计划不准确.开发此软件帮助程序员 ...
- PHP 在表单POST提交后数据分页实现,非GET,解决只有第一页显示正确的问题
//PHP 在表单POST提交后数据分页实现,非GET,使用SESSION,分页代码部分不在详述,主要为POST后的 除第一页之外的显示问题 //以下为ACTION页面 内容,仅为事例,当判断到页面未 ...
- WindowsForms获取服务名称
StringBuilder sb = new StringBuilder(); ServiceController[] services = ServiceController.GetServices ...