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 ...
随机推荐
- laravel中间件的实现原理
中间件的实现原理 运用 array_reduce 以及 call_user_func 实现 interface Middleware { public static function handle(C ...
- MongoDB部分
- Scratch教程:谁是真悟空
在西游记中,有一集是“真假悟空”,六耳猕猴变成了悟空的模样与真悟空真假难辨,打的不可开交. 在Scartch中,我们常常会使用一个本体来生成多个克隆体,这在开发过程中有重要的意义.但在实际操作中,每个 ...
- js 监听键盘的enter键
// js 版本 window.onload=function(){ document.onkeydown=function(ev){ var event=ev ||event if(event.ke ...
- [ZJOI2007]捉迷藏 (线段树,括号序列)
大意: 给定树, 要求维护一个点集, 支持删点添点, 询问点集直径. 本题做法比较多. 一个显然的做法是, 线段树维护区间直径, 然后根据点集直径的性质, 合并后直径端点一定是四个端点其中两个, 枚举 ...
- 十三、Vue中的computed属性
以下抄自https://www.cnblogs.com/gunelark/p/8492468.html 看了网上很多资料,对vue的computed讲解自己看的都不是很清晰,今天忙里抽闲,和同事们又闲 ...
- springboot mvc自动配置(一)自动配置DispatcherServlet和DispatcherServletRegistry
所有文章 https://www.cnblogs.com/lay2017/p/11775787.html 正文 springboot的自动配置基于SPI机制,实现自动配置的核心要点就是添加一个自动配置 ...
- ajax检查用户名重复
1.获取ajax对象 new XMLHttpRequest(); IE6-8: new ActiveXOject("Microsoft.XMLHTTP"); 兼容判断:if(XML ...
- 【Salesforce】入门篇
Salesforce.com 一开始是一个云端的销售自动化(Sales Force Automation, SFA)以及客户关系管理工具(Customer Relationship Managemen ...
- 【Day5】3.反爬策略之模拟登录
import urllib.request as ur import user_agent import lxml.etree as le request = ur.Request( url='htt ...