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

Copy
5 3
1 9
1 7
2 6
2 5
3 1

Sample Output 1 Copy

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

Copy
7 4
1 1
2 1
3 1
4 6
4 5
4 5
4 5

Sample Output 2 Copy

Copy
25

It is optimal to eat Sushi 1,2,31,2,3 and 44.


Sample Input 3 Copy

Copy
6 5
5 1000000000
2 990000000
3 980000000
6 970000000
6 960000000
4 950000000

Sample Output 3 Copy

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 【贪心+栈】的更多相关文章

  1. AtCoder Beginner Contest 116 D - Various Sushi (贪心+栈)

    D - Various Sushi Time Limit: 2 sec / Memory Limit: 1024 MB Score : 400400 points Problem Statement ...

  2. Atcoder Beginner Contest 118 C-Monsters Battle Royale(贪心)

    题目链接 题意就是要让给出的数字去互相取余,看看能得到最小的数事多少. 那么就可以从小到大排序,每一次都贪心地把最小的数作为攻击者,去攻击其他的数字(也就是大的取余小的),然后再一次排序,循环这个过程 ...

  3. AtCoder Beginner Contest 137 D题【贪心】

    [题意]一共有N个任务和M天,一个人一天只能做一个任务,做完任务之后可以在这一天之后的(Ai-1)天拿到Bi的工资,问M天内最多可以拿到多少工资. 链接:https://atcoder.jp/cont ...

  4. AtCoder Beginner Contest 181 E - Transformable Teacher (贪心,二分)

    题意:有一长度为奇数\(n\)的数组\(a\),和长度为\(m\)的数组\(b\),现要求从\(b\)中选择一个数放到\(a\)中,并将\(a\)分成\((n+1)/2\)个数对,求最小的所有数对差的 ...

  5. 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. 最多 ...

  6. 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 ...

  7. AtCoder Beginner Contest 153 题解

    目录 AtCoder Beginner Contest 153 题解 A - Serval vs Monster 题意 做法 程序 B - Common Raccoon vs Monster 题意 做 ...

  8. AtCoder Beginner Contest 173 题解

    AtCoder Beginner Contest 173 题解 目录 AtCoder Beginner Contest 173 题解 A - Payment B - Judge Status Summ ...

  9. AtCoder Beginner Contest 172 题解

    AtCoder Beginner Contest 172 题解 目录 AtCoder Beginner Contest 172 题解 A - Calc B - Minor Change C - Tsu ...

随机推荐

  1. setting中executable for debug session对话框

  2. windows自动化测试 (python)

    装插件 pywinauto 命令 pip install --upgrade pywinauto   最简单的一个例子,中文环境下   from pywinauto.application impor ...

  3. Centos7.3 安装MYSQL

    安装mysql         1.进入到要存放安装包的位置             mkdir /home/lnmp             cd /home/lnmp         2.查看系统 ...

  4. asp.net core-14.JWT认证授权 生成 JWT Token

    源码下载 语言组织能力不好 ,看这个 视频 用visual studio code打开文件,运行dotnet watch run 之后在postman里面去访问 拿到Token后

  5. git this exceeds GitHub's file size limit of 100.00 MB

    git push origin master过程中,出现如下错误 关键词:this exceeds GitHub's file size limit of 100.00 MB 推的时候忽略文件的操作: ...

  6. (三)使用Intent在活动中穿梭:显式和隐式Intent

    一.显式Intent @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstan ...

  7. Uwl.Admin开源框架(一)

    1.前言 作为一个忠实的软粉,一直期待微软出跨平台,一直在等待.Net Core,因为刚毕业对于.Net的很多东西不是很熟知,就开始了.Net Core的摸索,一路上坎坎坷坷,对于新技术一直很期待,就 ...

  8. CritterAI 翻译 Configuration Parameters

    翻译自: http://www.critterai.org/projects/nmgen_study/config.html 参考: http://blog.csdn.net/kun1234567/a ...

  9. 用js原生加jquery实现下拉跳转至商品详情页,上拉回到商品简介

    在做一个商城的项目时,做到商品详情页的时候需要实现这种下拉跳转到商品详情页加载许多图片,上拉回到商品简介的效果,并且需要用户在滑动时有一种费力的感觉.最初是通过iscroll插件实现的,但这个插件在使 ...

  10. 有选择性的启用SAP UI5调试版本的源代码

    在低版本的SAP UI5应用中,我们一旦切换成调试模式,那么应用程序源代码和UI5框架程序的源代码的调试版本都会重新加载,耗时很长. 我最近发现UI5新版本1.66.1提供了选择性加载调试版本的源代码 ...