cf-Education 62-C

题目

C. Playlist

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You have a playlist consisting of nn songs. The ii-th song is characterized by two numbers titi and bibi — its length and beauty respectively. The pleasure of listening to set of songs is equal to the total length of the songs in the set multiplied by the minimum beauty among them. For example, the pleasure of listening to a set of 33 songs having lengths [5,7,4][5,7,4] and beauty values [11,14,6][11,14,6] is equal to (5+7+4)⋅6=96(5+7+4)⋅6=96.

You need to choose at most kk songs from your playlist, so the pleasure of listening to the set of these songs them is maximum possible.

Input

The first line contains two integers nn and kk (1≤k≤n≤3⋅1051≤k≤n≤3⋅105) – the number of songs in the playlist and the maximum number of songs you can choose, respectively.

Each of the next nn lines contains two integers titi and bibi (1≤ti,bi≤1061≤ti,bi≤106) — the length and beauty of ii-th song.

Output

Print one integer — the maximum pleasure you can get.

Examples

input

Copy

  1. 4 3
  2. 4 7
  3. 15 1
  4. 3 6
  5. 6 8

output

Copy

  1. 78

input

Copy

  1. 5 3
  2. 12 31
  3. 112 4
  4. 100 100
  5. 13 55
  6. 55 50

output

Copy

  1. 10000

Note

In the first test case we can choose songs 1,3,41,3,4, so the total pleasure is (4+3+6)⋅6=78(4+3+6)⋅6=78.

In the second test case we can choose song 33. The total pleasure will be equal to 100⋅100=10000100⋅100=10000.

题目大意

给你n首歌,在里面最多选k首。每首歌有两个元素长度a和美丽值b,使选出来的歌中所有的长度的和sum乘以其中最低的b的积最大

思路

这类题一般是贪心。。。怎么贪就。。。看题目吧

先把所有的歌按美丽值从小到大排序

然后从后面开始遍历取歌,因为这样取,容易确定b的最小值(当前歌曲的b值),而且sum容易计算(计算队列中的sum值即可),就容易计算ans了。

当队列中的歌曲数大于k时,弹出a值最小的,此时sum最大

代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <sstream>
  7. #include <algorithm>
  8. #include <set>
  9. #include <map>
  10. #include <vector>
  11. #include <queue>
  12. #include <iomanip>
  13. #include <stack>
  14. using namespace std;
  15. typedef long long LL;
  16. const int INF = 0x3f3f3f3f;
  17. const int N = 300005;
  18. const int MOD = 1e9 + 9;
  19. const double pi = 3.1415926;
  20. #define lson l, m, rt << 1
  21. #define rson m + 1, r, rt << 1 | 1
  22. struct P
  23. {
  24. LL a, b;
  25. }p[N];
  26. bool cmp1(P x, P y)
  27. {
  28. return x.b < y.b;
  29. }
  30. struct cmp2
  31. {
  32. bool operator ()(const int &x, const int &y)
  33. {
  34. return x > y;
  35. }
  36. };
  37. int main()
  38. {
  39. int n, k;
  40. cin >> n >> k;
  41. priority_queue<int, vector<int>, cmp2> v;
  42. for(int i = 0;i < n;++i)
  43. cin >> p[i].a >> p[i].b;
  44. sort(p, p + n, cmp1);
  45. LL sum = 0, ans = 0;
  46. for(int i = n - 1;i >= 0;--i)
  47. {
  48. v.push(p[i].a);
  49. sum += p[i].a;
  50. if(v.size() > k)
  51. {
  52. sum -= v.top();
  53. v.pop();
  54. }
  55. ans = max(ans, sum * p[i].b);
  56. }
  57. cout << ans << endl;
  58. return 0;
  59. }

贪心--cf、education62-C的更多相关文章

  1. 贪心 CF 332 C 好题 赞

    题目链接: http://codeforces.com/problemset/problem/332/C 题目意思: 有n个命令,要通过p个,某主席要在通过的p个中选择k个接受. 每个任务有两个值ai ...

  2. Removing Columns 分类: 贪心 CF 2015-08-08 16:10 10人阅读 评论(0) 收藏

    Removing Columns time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  3. 2018CCPC 中国大学生程序设计竞赛 网络赛

    链接 1.括号序列贪心/CF&51nod原题 [分析]: 贪心,每次到i的时候,假如你要在i里面要卖掉股票,获益是a[i], 肯定要在前面要么:1)把已经卖了的变成不买不卖,需要-a[j], ...

  4. CF #374 (Div. 2) D. 贪心,优先队列或set

    1.CF #374 (Div. 2)   D. Maxim and Array 2.总结:按绝对值最小贪心下去即可 3.题意:对n个数进行+x或-x的k次操作,要使操作之后的n个数乘积最小. (1)优 ...

  5. CF 628C --- Bear and String Distance --- 简单贪心

    CF 628C 题目大意:给定一个长度为n(n < 10^5)的只含小写字母的字符串,以及一个数d,定义字符的dis--dis(ch1, ch2)为两个字符之差, 两个串的dis为各个位置上字符 ...

  6. CF 949D Curfew——贪心(思路!!!)

    题目:http://codeforces.com/contest/949/problem/D 有二分答案的思路. 如果二分了一个答案,首先可知越靠中间的应该大约越容易满足,因为方便把别的房间的人聚集过 ...

  7. CF #296 (Div. 1) B. Clique Problem 贪心(构造)

    B. Clique Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  8. CF 435B Pasha Maximizes(贪心)

    题目链接: [传送门][1] Pasha Maximizes time limit per test:1 second     memory limit per test:256 megabytes ...

  9. CF 115B Lawnmower(贪心)

    题目链接: 传送门 Lawnmower time limit per test:2 second     memory limit per test:256 megabytes Description ...

随机推荐

  1. R语言中 fitted()和predict()的区别

    fitted是拟合值,predict是预测值.模型是基于给定样本的值建立的,在这些给定样本上做预测就是拟合.在新样本上做预测就是预测. 你可以找一组数据试试,结果如何. fit<-lm(weig ...

  2. 马婕 2014MBA专硕考试 报刊选读 3 禽流感考验政府的透明度(转)

    http://blog.sina.com.cn/s/blog_3e66af4601015z0n.html Bird flu cases test government transparency 禽流感 ...

  3. Web挖掘

    Web挖掘 Web挖掘的目标是从Web的超链接.网页内容和使用日志中探寻有用的信息.依据Web挖掘任务,可以划分为三种主要类型:Web结构挖掘.Web内容挖掘和Web使用挖掘.Web结构挖掘简单的说就 ...

  4. Oracle物化视图的一般使用

    普通视图和物化视图根本就不是一个东西,说区别都是硬拼到一起的,首先明白基本概念,普通视图是不存储任何数据的,他只有定义,在查询中是转换为对应的定义SQL去查询,而物化视图是将数据转换为一个表,实际存储 ...

  5. iconv用法解读

    iconv是一个字符集转换函数,原型为: size_t iconv(iconv_t cd,              char **inbuf, size_t *inbytesleft,       ...

  6. Fork/Join 型线程池与 Work-Stealing 算法

    JDK 1.7 时,标准类库添加了 ForkJoinPool,作为对 Fork/Join 型线程池的实现.Fork 在英文中有 分叉 的意思,而 Join 有 合并 的意思.ForkJoinPool ...

  7. hdu2364之BFS

    Escape Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  8. TOAD连接ORACLE而不装ORACLE 客户端的方法

    TOAD连接ORACLE而不装ORACLE 客户端的方法 原来连接ORACLE总是要装ORACLE客户端,挺麻烦得,一方面要带ORACLE得盘,另一方面,装这么大得东西也让人很不爽. ORACLE好像 ...

  9. FORM 错误:此责任无可用函数。 更改责任或与您的系统管理员联系。

    错误:此责任无可用函数. 更改责任或与您的系统管理员联系. 2014-07-02 12:20:47 分类: Oracle Symptom 访问Help->Diagnostics->Exam ...

  10. Jurassic.ScriptEngine 使用

    标记: Jurassic,js,net Jurassic.ScriptEngine是一个让net动态执行js的一个引擎.类似的有ironjs等.支持ECMAScript 5,非线程安全 使用 usin ...