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:

  1. NN KK
  2. t1t1 d1d1
  3. t2t2 d2d2
  4. ..
  5. ..
  6. ..
  7. tNtN dNdN

Output

Print the maximum satisfaction that you can obtain.


Sample Input 1 Copy

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

Sample Output 1 Copy

Copy
  1. 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
  1. 7 4
  2. 1 1
  3. 2 1
  4. 3 1
  5. 4 6
  6. 4 5
  7. 4 5
  8. 4 5

Sample Output 2 Copy

Copy
  1. 25

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


Sample Input 3 Copy

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

Sample Output 3 Copy

Copy
  1. 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的先进后出的思想,巧妙的最优的替换了每一个能替换掉的是当前选择中贡献最小的。

贪心好题,(口胡结束)。细节见代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <queue>
  7. #include <stack>
  8. #include <map>
  9. #include <set>
  10. #include <vector>
  11. #define rt return
  12. #define sz(a) int(a.size())
  13. #define all(a) a.begin(), a.end()
  14. #define rep(i,x,n) for(int i=x;i<n;i++)
  15. #define repd(i,x,n) for(int i=x;i<=n;i++)
  16. #define pii pair<int,int>
  17. #define pll pair<long long ,long long>
  18. #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
  19. #define MS0(X) memset((X), 0, sizeof((X)))
  20. #define MSC0(X) memset((X), '\0', sizeof((X)))
  21. #define pb push_back
  22. #define mp make_pair
  23. #define fi first
  24. #define se second
  25. #define eps 1e-6
  26. #define gg(x) getInt(&x)
  27. #define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
  28. using namespace std;
  29. typedef long long ll;
  30. ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
  31. ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
  32. 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;}
  33. inline void getInt(int* p);
  34. const int maxn=;
  35. const int inf=0x3f3f3f3f;
  36. /*** TEMPLATE CODE * * STARTS HERE ***/
  37. int n,k;
  38. struct info
  39. {
  40. int x,v;
  41. }a[maxn];
  42. bool cmp(info one,info two)
  43. {
  44. return one.v>two.v;
  45. }
  46. int vis[maxn];
  47. stack<int> s;
  48. int main()
  49. {
  50. scanf("%d %d",&n,&k);
  51. repd(i,,n)
  52. {
  53. scanf("%d %d",&a[i].x,&a[i].v);
  54. }
  55. sort(a+,a++n,cmp);
  56. ll cnt=;
  57. ll tp=;
  58. ll res=;
  59. ll ans=0ll;
  60. repd(i,,n)
  61. {
  62. if(cnt<k)
  63. {
  64. if(vis[a[i].x]==)
  65. {
  66. vis[a[i].x]=;
  67. tp++;
  68. }else
  69. {
  70. s.push(a[i].v);
  71. }
  72. res+=a[i].v;
  73. cnt++;
  74. ans=max(ans,res+1ll*tp*tp);
  75. }
  76. else{
  77. if(s.empty())
  78. break;
  79. if(vis[a[i].x])
  80. continue;
  81. vis[a[i].x]=;
  82. tp++;
  83. res-=s.top();
  84. res+=a[i].v;
  85. s.pop();
  86. ans=max(ans,res+tp*tp);
  87. }
  88. }
  89. printf("%lld\n",ans);
  90. return ;
  91. }
  92.  
  93. inline void getInt(int* p) {
  94. char ch;
  95. do {
  96. ch = getchar();
  97. } while (ch == ' ' || ch == '\n');
  98. if (ch == '-') {
  99. *p = -(getchar() - '');
  100. while ((ch = getchar()) >= '' && ch <= '') {
  101. *p = *p * - ch + '';
  102. }
  103. }
  104. else {
  105. *p = ch - '';
  106. while ((ch = getchar()) >= '' && ch <= '') {
  107. *p = *p * + ch - '';
  108. }
  109. }
  110. }

AtCoder Beginner Contest 116 D - Various Sushi (贪心+栈)的更多相关文章

  1. AtCoder Beginner Contest 116 D - Various Sushi 【贪心+栈】

    Problem Statement There are NN pieces of sushi. Each piece has two parameters: "kind of topping ...

  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. MongoDB数据创建与使用

    MongoDB数据创建与使用 创建数据库 代码功能:读取本地文本文件,并保存到数据库中 import pymongo #连接mongo数据库 client = pymongo.MongoClient( ...

  2. .gho文件检查

    虽然目前windows10的接受程度越来越广泛,但我接触到的一些非IT人士还是钟爱于windows7系统,本文记录一下在使用ghost还原系统遇到的问题. gho还原失败 在还原ghost系统过程中, ...

  3. adb连接手机模拟器

    首先,要保证模拟器和电脑在同一网段(手机模拟器设置为桥接模式即可) 我使用的是夜神模拟器. 然后查看一下adb版本, adb version 需要1.0.31往上的版本才能连接. 然后输入 adb c ...

  4. php学习----文件系统

    PHP文件系统之读取文件内容 PHP具有丰富的文件操作函数,最简单的读取文件的函数为file_get_contents,可以将整个文件全部读取到一个字符串中. $content = file_get_ ...

  5. 聚类——WKFCM的matlab程序

    聚类——WKFCM的matlab程序 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 在聚类——WKFCM文章中已介绍了WKFCM算法的理论知识,现在用 ...

  6. 力扣算法题—060第K个排列

    给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "132&qu ...

  7. Spring的AOP开发的相关术语

    转载自 https://www.cnblogs.com/ltfxy/p/9873618.html SpringAOP简介: AOP思想最早是由AOP联盟组织提出的.Spring使用这种思想最好的框架. ...

  8. Python3 实现简易局域网视频聊天工具

    Python3 实现简易局域网视频聊天工具   1.环境 操作系统为 Ubuntu 16.04 python 3.5opencv-python 3.4.1.15numpy 1.14.5PyAudio ...

  9. 【Python语言】Python介绍

    目前在大数据的行业中有3种语言:1. Java ---> 用于大数据工程2. Scala ---> 用于大数据工程和数据科学3.Python ---> 用于数据科学 Python是一 ...

  10. Android 6.0以后的版本报错:open failed: EACCES (Permission denied)

    Android 6.0以后的版本报错:open failed: EACCES (Permission denied) 在开发项目中,遇见要进行文件操作,遇见Caused by: android.sys ...