https://codeforces.com/contest/1379/problem/C

题意:

给m种花(a,b),从中取出n朵,每种花可以取0和无限朵,如果取出第i朵花的个数为c>0,则贡献度为ai+(c-1)*bi,如果c=0,则贡献度为0,求最大的贡献度。

解法:

可以看出取了每种花,第0,第1,第2,第3朵,...的贡献度分别是 0,a,b,b,...,

显然,贪心一下,最优方案b取一种(或0),先取比它大的a,最后再取这种b一直取到最后即可。

所以直接枚举最后取哪个b,然后贪心取所有比它大的a(求个前缀和即可),复杂度o(m*logm)。

#include<bits/stdc++.h>
#define sz(v) (int)v.size() using namespace std; typedef long long ll;
const int maxn=1000100;
const int INF=(1<<29); int n,m;
struct Node{
int a,b;
ll s;
friend bool operator<(Node A,Node B){
return A.a>B.a;
}
};Node p[maxn]; int bin_a(int l,int r,int b){
int res=0;
while(l<=r){
int m=(l+r)>>1;
if(p[m].a>=b) res=max(res,m),l=m+1;
else r=m-1;
}
return res;
} void solve(){
cin>>n>>m;
for(int i=1;i<=m;i++) scanf("%d%d",&p[i].a,&p[i].b);
sort(p+1,p+m+1);
p[1].s=p[1].a;
for(int i=2;i<=m;i++) p[i].s=p[i-1].s+p[i].a;
ll res=0;
for(int i=1;i<=m;i++){
int x=bin_a(1,m,p[i].b); // >=b last
if(x==0){
res=max(res,1LL*p[i].a+1LL*(n-1)*p[i].b);
continue;
}
if(x>=n){
res=max(res,p[n].s);
continue;
}
if(x<i) res=max(res,p[x].s+1LL*p[i].a+1LL*(n-x-1)*p[i].b);
else res=max(res,p[x].s+1LL*(n-x)*p[i].b);
}
cout<<res<<endl;
} int main(){
// freopen("in.txt","r",stdin);
int T;cin>>T;
while(T--) solve();
return 0;
}

最新文章

  1. Java集合---Array类源码解析
  2. web前端基础知识 jQuery
  3. CMD和AMD区别的概括
  4. WindowsFormsHost使用问题
  5. 前端见微知著番外篇:GIT舍我其谁?
  6. canvas对象arc函数的使用-遁地龙卷风
  7. 原生js实现增加(addclass),删除(removeclass),判断是否存在(hasclass),如果存在删除,如果不存在添加(toggleclass)和获取类名(getbyclass)的方法
  8. UITableView 详解 ()
  9. Windows平台分布式架构-负载均衡(高并发)
  10. Sqlite: unable to open database file
  11. ubuntu下php开发环境搭建,nginx+(cgi)php5fpm+memcached+xdebug
  12. angular中实现jQuery的Document Ready
  13. Python基础之 正则表达式指南
  14. C# orderby子句
  15. Codeforces 438D The Child and Sequence
  16. 重命名Apache日志,新日志文件会放在哪里
  17. 题解 CF540D 【Bad Luck Island】
  18. AKA “Project” Milestone
  19. 【JavaScript从入门到精通】第三课 初探JavaScript魅力-03
  20. ionic框架使用步骤

热门文章

  1. pat乙级 1017 A除以B 模拟除法
  2. 需要登陆,请求数据 session
  3. Linux改密码失败authentication token manipulation error 的处理办法
  4. 异常:java.sql.SQLException: HOUR_OF_DAY: 0 -> 1解决
  5. ubuntu22.04 git升级
  6. ubuntu 安装php7.2 oracle扩展
  7. CentOS7 搭建 PXE 安装系统
  8. cdn全栈加速nginx二层代理实现
  9. Dilworth
  10. iOS 绘制虚线