试着找出一个比较靠谱的思路先,重点在于物品买完第一次后会变化价格,之后都不会再变了,贪心地,如果我们重复地购买了一件物品,那么很显然我们只会重复购买一件物品多次,但这个物品可能不是 m a x ( b i ) max(b_i) max(bi​).
我们考虑枚举每个物品作为这个购买了多次 b i b_i bi​.显然,如果我们认为当前这个物品购买了多次 b i b_i bi​,一定不会购买其他物品的 b i b_i bi​,而只会购买他们的 a i a_i ai​.那么把 a i a_i ai​降序排序,利用二分搜索找到一个 p o s pos pos,使得 a p o s > b i a_{pos}>b_i apos​>bi​.
求得这个前缀和,也就是我们要购买 a 1 到 a p o s 所有的 a a_1到a_{pos}所有的a a1​到apos​所有的a,余下的 n − p o s 全部用来购买物品 i n-pos全部用来购买物品i n−pos全部用来购买物品i.

/*+
You held me down but I broke free,
I found the love inside of me.
Now I don't need a hero to survive
Cause I already saved my life.
*/
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+5;
const int INF = 1e9+7;
typedef long long ll;
typedef pair<int,int> pii;
#define all(a) (a).begin(), (a).end()
#define pb(a) push_back(a)
vector<int> G[maxn];
struct Node{int a,b;bool operator < (const Node&rhs)const{return a>rhs.a;}
}t[maxn];
int n,m;
ll pre[maxn];
int find(int k){//lower_bound to find a_pos > b[k]int ans = 0,L = 1,R = m;while(L<=R){int mid = (L+R)/2;if(t[mid].a>t[k].b) ans = mid,L = mid+1;else R = mid-1;}return ans;
}
int main(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int T;cin>>T;while(T--){cin>>n>>m;for(int i=1;i<=m;i++){cin>>t[i].a>>t[i].b;}sort(t+1,t+1+m);for(int i=1;i<=m;i++) pre[i] = pre[i-1] + t[i].a;ll ans = 0;for(int i=1;i<=m;i++){//认为选了物品i,int pos = find(i);pos = min(pos,n);//最多选n个ll sum = pre[pos];//选了前pos个aint res = n - pos;//还能选res个iif(res>=1){if(pos>=i) sum+= 1LL*res*t[i].b;else sum+= t[i].a + 1LL*(res-1)*t[i].b;}ans = max(sum,ans); }cout<<ans<<"\n";}
}

CF1379C Choosing flowers相关推荐

  1. CodeForces-1379C Choosing flowers

    原题链接: https://vjudge.net/problem/CodeForces-1379C AC代码: #include <algorithm> #include <cmat ...

  2. codeforces1379C Choosing flowers

    https://codeforces.com/contest/1379/problem/C 按a拍个序,然后枚举每种花 只多拿这一种 剩下的大于bi的aj的必须拿,因为可以通过少拿一个bi来换一个更大 ...

  3. C——Flowers

    Problem Description As you know, Gardon trid hard for his love-letter, and now he's spending too muc ...

  4. Codeforces Round #657 (Div. 2)

    A. Acacius and String 爆零!太菜了,下来终于把A题代码调AC了 #define IO ios::sync_with_stdio(false);cin.tie();cout.tie ...

  5. LeetCode刷题记录8——605. Can Place Flowers(easy)

    LeetCode刷题记录8--605. Can Place Flowers(easy) 目录 LeetCode刷题记录8--605. Can Place Flowers(easy) 题目 语言 思路 ...

  6. I - Watering Flowers CodeForces - 617C

    I - Watering Flowers CodeForces - 617C int -2-31 ~ 231-1 == 2e9 longlong -2-63 ~ 263-1 == 9e18 有两种方法 ...

  7. 吴恩达机器学习笔记55-异常检测算法的特征选择(Choosing What Features to Use of Anomaly Detection)

    吴恩达机器学习笔记55-异常检测算法的特征选择(Choosing What Features to Use of Anomaly Detection) 对于异常检测算法,使用特征是至关重要的,下面谈谈 ...

  8. [BZOJ] 1634: [Usaco2007 Jan]Protecting the Flowers 护花

    1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 827  S ...

  9. USACO 保护花朵 Protecting the Flowers, 2007 Jan

    Description 约翰留下了 N 只奶牛呆在家里,自顾自地去干活了,这是非常失策的.他还在的时候,奶牛像 往常一样悠闲地在牧场里吃草.可是当他回来的时候,他看到了一幕惨剧:他的奶牛跑进了他的花园 ...

最新文章

  1. Markdown中常用的转义字符
  2. 自己动手写web服务器一(浏览器的访问信息)
  3. jquery --- 事件处理函数的event对象的几个属性(方法)说明
  4. pipedreader_Java PipedReader ready()方法与示例
  5. 第二次作业+105032014149
  6. vmware workstation不可恢复错误:(vthread-7)
  7. LAMP兄弟连PHP高薪就业班2012召集令
  8. 【论文精读】TransE 及其实现
  9. 软件工程uml画图期末考试(新闻发布系统)
  10. gvf snake matlab,GVF Snake 学习的分析总结
  11. 莫烦 Matplotlib
  12. 齐杰文学CMS - 关关采集器2021可用19条采集规则
  13. 暑期作息时间表模板_最新暑假作息时间表excel模板-暑假作息时间模板
  14. MIPI 系列之 DSI
  15. ASP网站访问量计数器
  16. 树莓派3B 之IIC通信
  17. 和99.9%的人的认知完全相反的富人思维
  18. HTTP和RTSP简介
  19. 十个鲜为人知的Linux命令-【3】
  20. 20万粉丝的技术大V是怎样练成的——胡忠想访谈

热门文章

  1. 物联网专业并不好_在广阔的物联网中,“智能”仍然并不意味着安全
  2. 转贴-刻录品质经典文章之一
  3. C语言期末考试成绩奖励编码,C语言期末考试总结,看完保你过
  4. 告别繁琐界面,酷我音乐带你轻松HiFi
  5. 微前端系列讲解--应用集成方案(qiankun+umi+vue)
  6. 2012年3月2日专业学习
  7. 5分钟就能做一个Excel动态图表,你确定不学学?
  8. 【C语言】猴子吃桃问题。猴子第1天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。第2天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想……
  9. npoi2.0版本word中插入图片
  10. 如何零基础学习法语,陈家桥法语零基础入门学习