AtCoder Beginner Contest 116 D - Various Sushi (贪心+栈)
D - Various Sushi
Time Limit: 2 sec / Memory Limit: 1024 MB
Score : 400400 points
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+1~n的时候,如果这个结构体的类型没加入过,那么把他加入,然后类型数目+1,弹出栈顶的元素,减去他的x值贡献,然后用新结构体取尝试更新最大值。
还主要用到了stack的先进后出的思想,巧妙的最优的替换了每一个能替换掉的是当前选择中贡献最小的。
贪心好题,(口胡结束)。细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rt return
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n,k;
struct info
{
int x,v;
}a[maxn];
bool cmp(info one,info two)
{
return one.v>two.v;
}
int vis[maxn];
stack<int> s;
int main()
{
scanf("%d %d",&n,&k);
repd(i,,n)
{
scanf("%d %d",&a[i].x,&a[i].v);
}
sort(a+,a++n,cmp);
ll cnt=;
ll tp=;
ll res=;
ll ans=0ll;
repd(i,,n)
{
if(cnt<k)
{
if(vis[a[i].x]==)
{
vis[a[i].x]=;
tp++;
}else
{
s.push(a[i].v);
}
res+=a[i].v;
cnt++;
ans=max(ans,res+1ll*tp*tp);
}
else{
if(s.empty())
break;
if(vis[a[i].x])
continue;
vis[a[i].x]=;
tp++;
res-=s.top();
res+=a[i].v;
s.pop();
ans=max(ans,res+tp*tp);
}
}
printf("%lld\n",ans);
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
AtCoder Beginner Contest 116 D - Various Sushi (贪心+栈)的更多相关文章
- AtCoder Beginner Contest 116 D - Various Sushi 【贪心+栈】
Problem Statement There are NN pieces of sushi. Each piece has two parameters: "kind of topping ...
- 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 ...
随机推荐
- 洗礼灵魂,修炼python(78)--全栈项目实战篇(6)—— 多级目录菜单之地址管理系统
相信各位都在在网上买过东西吧?那么今天我的主题就是写个在线购物系统,是不可能的,哈哈(后期确实有这个项目),那么购物都填写过快递地址吧?然后网上查个地址都有地址管理吧? 要求: 1.打印出省.市.县等 ...
- [Hive_4] Hive 插入数据
0. 说明 Hive 插入数据的方法 && Hive 插入数据的顺序 && 插入复杂数据的方法 && load 命令详解 1. Hive 插入数据的方法 ...
- C++实现程序单实例运行的两种方式
简介 在我们编写程序的时候,经常会注意到的一个问题就是如何能够让程序只运行一个实例,确保不会让同一个程序多次运行,从而产生诸多相同进程,给我们的带来不便呢?那么常用的有以下四种方法,第一种方法是通过扫 ...
- Window快捷键
1.带windows 键 Win + E 打开文件夹 Win + R 调出运行命令 Win + T 显示任务栏,并在在其中切换 Win +Pause 显示系统属性 Win + Home 最小化 / 还 ...
- LeetCode算法题-Reverse Bits(Java实现)
这是悦乐书的第185次更新,第187篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第44题(顺位题号是190).给定32位无符号整数,求它的反转位.例如: 输入:4326 ...
- 【算法】LeetCode算法题-Reverse Integer
这是悦乐书的第143次更新,第145篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第2题(顺位题号是7),给定32位有符号整数,然后将其反转输出.例如: 输入: 123 ...
- python的shelve模块
shelve shelve是一额简单的数据存储方案,他只有一个函数就是open(),这个函数接收一个参数就是文件名,并且文件名必须是.bat类型的.然后返回一个shelf对象,你可以用他来存储东西,就 ...
- 12.scrapy框架之递归解析和post请求
今日概要 递归爬取解析多页页面数据 scrapy核心组件工作流程 scrapy的post请求发送 今日详情 1.递归爬取解析多页页面数据 - 需求:将糗事百科所有页码的作者和段子内容数据进行爬取切持久 ...
- Django-rest-framework 接口实现 认证:(auth | authentication)
认证:(auth | authentication) REST framework提供了一些开箱即用的身份验证方案,并且还允许你实现自定义方案. 在 rest_framework.authentica ...
- tomcat 设置虚拟路径的4种方法
通常使用方法1或者方法2 方法1 (添加配置文件):推荐使用,不需要重启服务器 在Tomcat根目录下的/conf/Catalina/localhost/ 路径下新建一个filename.xml,并 ...