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. 顺序表添加与删除元素以及 php实现顺序表实例

    对顺序表的操作,添加与删除元素. 增加元素 如下图所示  对顺序列表 Li [1328,693,2529,254]  添加一个元素 111 ,有三种方式: a)尾部端插入元素,时间复杂度O(1);  ...

  2. 解决 pycharm [Errno 11001] getaddrinfo failed 错误提示!

    我看网上很多问题对这个问题的解决方法也是一只半解的,可能产生问题的原因不一样吧,今天我说下我的经验 解决办法: 原因就是你的本地dns解析的host文件,里面的的解析地址被注释了! 打开本地的hous ...

  3. 【数学】Prime-Factor Prime

    Prime-Factor Prime 题目描述 A positive integer is called a "prime-factor prime" when the numbe ...

  4. 【Transact-SQL】找出不包含字母、不包含汉字的数据

    原文:[Transact-SQL]找出不包含字母.不包含汉字的数据 测试的同事,让我帮忙写个sql语句,找出表中xx列不包含汉字的行. 下面的代码就能实现. IF EXISTS(SELECT * FR ...

  5. eclipse复制工作空间配置步骤

    多个workspace,把每个workspace的设置共享,省去每次都重新配置一次. 总结一下,复制工作空间配置步骤如下: [最好是在新的workspace创建项目之前操作] 1 使用eclipse新 ...

  6. POJ2945(Find the Clones)--字典树,map

    题意:给你n个规定长度的单词,问你其中出现了1次的单词,出现两次的单词...出现n次单词分别有多少个. 当然这题map也能过,但是这里介绍字典树的做法. 首相对于n个单词存入树中,当然建树过程中遇到一 ...

  7. css3 transform实现水平和垂直居中

    代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  8. MySql Host is blocked because of many connection errors 问题的解决方法

    错误日志: message from server: "Host '10.250.112.141' is blocked because of many connection errors; ...

  9. Oracle nvl()函数处理null值

    首先我先说一下什么是Oracle的函数,曾经有一位大牛,让我说说熟悉的oracle函数,我当时竟一头雾水,心想“什么oracle函数啊,不就是那些SQL语句吗“,当时我竟然说出了select之类的回答 ...

  10. Image Processing and Analysis_8_Edge Detection:Design of steerable filters for feature detection using canny-like criteria ——2004

    此主要讨论图像处理与分析.虽然计算机视觉部分的有些内容比如特 征提取等也可以归结到图像分析中来,但鉴于它们与计算机视觉的紧密联系,以 及它们的出处,没有把它们纳入到图像处理与分析中来.同样,这里面也有 ...