AtCoder Beginner Contest 116 D - Various Sushi 【贪心+栈】
Problem Statement
There are NN pieces of sushi. Each piece has two parameters: "kind of topping" titi and "deliciousness" didi. You are choosing KK among these NN pieces to eat. Your "satisfaction" here will be calculated as follows:
- The satisfaction is the sum of the "base total deliciousness" and the "variety bonus".
- The base total deliciousness is the sum of the deliciousness of the pieces you eat.
- The variety bonus is x∗xx∗x, where xx is the number of different kinds of toppings of the pieces you eat.
You want to have as much satisfaction as possible. Find this maximum satisfaction.
Constraints
- 1≤K≤N≤1051≤K≤N≤105
- 1≤ti≤N1≤ti≤N
- 1≤di≤1091≤di≤109
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
NN KK
t1t1 d1d1
t2t2 d2d2
..
..
..
tNtN dNdN
Output
Print the maximum satisfaction that you can obtain.
Sample Input 1 Copy
5 3
1 9
1 7
2 6
2 5
3 1
Sample Output 1 Copy
26
If you eat Sushi 1,21,2 and 33:
- The base total deliciousness is 9+7+6=229+7+6=22.
- The variety bonus is 2∗2=42∗2=4.
Thus, your satisfaction will be 2626, which is optimal.
Sample Input 2 Copy
7 4
1 1
2 1
3 1
4 6
4 5
4 5
4 5
Sample Output 2 Copy
25
It is optimal to eat Sushi 1,2,31,2,3 and 44.
Sample Input 3 Copy
6 5
5 1000000000
2 990000000
3 980000000
6 970000000
6 960000000
4 950000000
Sample Output 3 Copy
4900000016
Note that the output may not fit into a 3232-bit integer type.
题意:给定N个结构体,每一个结构体有两个信息,分别是type 和 x,让你从中选出K个结构体,使之type的类型数的平方+sum{ xi } 最大。
思路:【贪心】将X从大到小排序,然后按顺序取前K个,在取前K个过程中,将已经出现的类型放入栈中。然后,开始遍历K+1----N的元素,使得不断加入没有出现的元素的类型。在此过程中通过弹栈更新最值。
AC代码:
#include<bits/stdc++.h> using namespace std;
#define int long long
#define N 150000
struct str{
int x,y;
}st[N];
bool cmp(str a,str b){
return a.y>b.y;
}
map<int,int> mp;
stack<int> s;
signed main(){
int n,k;
cin>>n>>k;
for(int i=;i<=n;i++){
cin>>st[i].x>>st[i].y;
}
sort(st+,st++n,cmp);
int maxn=;
int type=;
int sum=;
for(int i=;i<=k;i++){
if(!mp[st[i].x]){
mp[st[i].x]=;
type++;
}else{
s.push(st[i].y);
}
sum+=st[i].y;
maxn=max(maxn,type*type+sum);
}
for(int i=k+;i<=n;i++){
if(s.empty())
break;
if(mp[st[i].x])
continue;
mp[st[i].x]=;
type++;
sum-=s.top();
s.pop();
sum+=st[i].y;
maxn=max(maxn,type*type+sum);
}
cout<<maxn;
return ;
}
AtCoder Beginner Contest 116 D - Various Sushi 【贪心+栈】的更多相关文章
- AtCoder Beginner Contest 116 D - Various Sushi (贪心+栈)
D - Various Sushi Time Limit: 2 sec / Memory Limit: 1024 MB Score : 400400 points Problem Statement ...
- Atcoder Beginner Contest 118 C-Monsters Battle Royale(贪心)
题目链接 题意就是要让给出的数字去互相取余,看看能得到最小的数事多少. 那么就可以从小到大排序,每一次都贪心地把最小的数作为攻击者,去攻击其他的数字(也就是大的取余小的),然后再一次排序,循环这个过程 ...
- AtCoder Beginner Contest 137 D题【贪心】
[题意]一共有N个任务和M天,一个人一天只能做一个任务,做完任务之后可以在这一天之后的(Ai-1)天拿到Bi的工资,问M天内最多可以拿到多少工资. 链接:https://atcoder.jp/cont ...
- AtCoder Beginner Contest 181 E - Transformable Teacher (贪心,二分)
题意:有一长度为奇数\(n\)的数组\(a\),和长度为\(m\)的数组\(b\),现要求从\(b\)中选择一个数放到\(a\)中,并将\(a\)分成\((n+1)/2\)个数对,求最小的所有数对差的 ...
- AtCoder Beginner Contest 249 F - Ignore Operations // 贪心 + 大根堆
传送门:F - Keep Connect (atcoder.jp) 题意: 给定长度为N的操作(ti,yi). 给定初值为0的x,对其进行操作:当t为1时,将x替换为y:当t为2时,将x加上y. 最多 ...
- AtCoder Beginner Contest 116 C题 【题意:可以在任意区间【L,R】上加1,求通过最少加1次数得到题目给定的区间】】{思维好题}
C - Grand Garden In a flower bed, there are NN flowers, numbered 1,2,......,N1,2,......,N. Initially ...
- AtCoder Beginner Contest 153 题解
目录 AtCoder Beginner Contest 153 题解 A - Serval vs Monster 题意 做法 程序 B - Common Raccoon vs Monster 题意 做 ...
- AtCoder Beginner Contest 173 题解
AtCoder Beginner Contest 173 题解 目录 AtCoder Beginner Contest 173 题解 A - Payment B - Judge Status Summ ...
- AtCoder Beginner Contest 172 题解
AtCoder Beginner Contest 172 题解 目录 AtCoder Beginner Contest 172 题解 A - Calc B - Minor Change C - Tsu ...
随机推荐
- Win10 鼠标右键新建菜单添加自定义文件
1. 引言 在鼠标右键(右单机)新建菜单中添加自定义文件,例如:写字板,markdown等. 效果图: 2. 操作步骤(以Win10为例) 1. win+R输入regedit进入注册表 2. 这里以添 ...
- ckplayer获取播放时长一
<div id="a1"></div> <div id="nowTime"></div> <script ...
- (三)调用web服务
(二)发布第一个WebService服务与DSWL文档解析讲解了如何发布一个web服务,本章主要讲述如何调用一个web服务. 这里有三种方式: 使用代理模式调用,需要将服务端的接口类拷贝到客户端中.( ...
- vue语法概要二
函数 用途 类别 v-html 用于输出html格式的数据 输出 v-bing 用于输出值 输出 v-model 双向绑定 输入和输出 v-if 逻辑判断 分支语句 v-else 同上 分支语句 v- ...
- C++ 虚函数相关
多态 C++的封装.继承和多态三大特性,封装没什么好说的,就是把事务属性和操作抽象成为类,在用类去实例化对象,从而对象可以使用操作/管理使用它的属性. 至于继承,和多态密不可分.基类可以进行派生,而派 ...
- 封装promise
// new 做了什么 //1.创建一个新的空对象 //2.将构造函数中的this指向这个新空对象 //3.执行构造函数中的代码 //4.返回这个对象 //5.这个对象有一个__proto__指向构造 ...
- 高射炮打蚊子,杀鸡用绝世好剑:在SAP Kyma上运行UI5应用
国人在表述"大材小用"这个场景时,总喜欢用一些实物来类比,比如:高射炮打蚊子. 英国QF 3.7英寸(94mm)高射炮,战斗全重超过9.3吨,全长近5米,最大射程约18公里,最大射 ...
- 开源证书检查工具:fossy(fossology)
工具下载: https://github.com/fossology/fossology 其他说明: http://archive15.fossology.org/projects/fossology ...
- vSphere
VMware vSphere集成容器(VIC)建立了一个在轻量级虚拟机内部署并管理容器的环境.全新的虚拟机环境提供了更高级别的硬件隔离度,灵活性以及可扩展性使得容器对开发人员以及企业应用具有如此大的吸 ...
- ORACLE归档日志满了之后,如何删除归档日志
当ORACLE归档日志满后如何正确删除归档日志 版权声明:本文为博主原创文章,未经博主允许不得转载. 当ORACLE 归档日志满了后,将无法正常登入ORACLE,需要删除一部分归档日志才能正常登入OR ...