题目链接:https://codeforces.com/contest/1379/problem/C

题意

有 $m$ 种花,每种花数量无限,第一次购买一种花收益为 $a_i$,之后每一次购买收益为 $b_i$,问买 $n$ 朵花的最大收益为多少。

题解

感觉是个 $dp$,实际上是个贪心。

一定是一种花买了很多,其他花不买或只买一次,当其他花第一次购买的收益 $a_j$ 大于要买较多花的 $b_i$ 时,可以将一个 $b_i$ 换为其他花的 $a_j$ 。

枚举哪种花买的较多,然后在 $a$ 中二分该花的 $b_i$,并用前缀和计算可以增加的收益即可。

代码

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
constexpr int N = 1e5 + 100; int n, m;
ll sum[N];
pair<int, int> a[N]; ll getans(int i) {
//第一个大于 b[i] 的 a[j] 的位置
int j = upper_bound(a + 1, a + 1 + m, make_pair(a[i].second, 0)) - a;
//如果可以替换所有当前花的所有 b[i],那么返回最大的 n 个 a[j] 的和即可
if (m - j + 1 >= n) {
return sum[m - n + 1];
}
//剩余的 b[i] 个数,-1 是减去第一次购买的 a[i]
int rest = n - (m - j + 1) - 1;
//如果 j <= i,此时 a[i] 已经在 sum[j] 里计入过一次了,多出来的买为 b[i] 即可
if (j <= i) {
++rest;
return 1LL * rest * a[i].second + sum[j];
}
return 1LL * rest * a[i].second + sum[j] + a[i].first;
} void solve() {
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
cin >> a[i].first >> a[i].second;
}
sort(a + 1, a + 1 + m);
sum[m + 1] = 0;
for (int i = m; i >= 1; --i) {
sum[i] = sum[i + 1] + a[i].first;
}
ll ans = 0;
for (int i = 1; i <= m; ++i) {
ans = max(ans, getans(i));
}
cout << ans << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}

参考代码

https://codeforces.com/contest/1379/submission/87302809

Codeforces Round #657 (Div. 2) C. Choosing flowers(贪心)的更多相关文章

  1. 树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland

    题目传送门 /* 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计 ...

  2. Codeforces Round #340 (Div. 2) C. Watering Flowers 暴力

    C. Watering Flowers 题目连接: http://www.codeforces.com/contest/617/problem/C Descriptionww.co A flowerb ...

  3. Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland dfs

    D. Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes i ...

  4. Codeforces Round #657 (Div. 2) B. Dubious Cyrpto(数论)

    题目链接:https://codeforces.com/contest/1379/problem/B 题意 给出三个正整数 $l,r,m$,判断在区间 $[l,r]$ 内是否有 $a,b,c$ 满足存 ...

  5. Codeforces Round #657 (Div. 2) A. Acacius and String(字符串)

    题目链接:https://codeforces.com/contest/1379/problem/A 题意 给出一个由 '?' 和小写字母组成的字符串,可以将 '?' 替换为小写字母,判断是否存在一种 ...

  6. Codeforces Round #246 (Div. 2) A. Choosing Teams

    给定n k以及n个人已参加的比赛数,让你判断最少还能参加k次比赛的队伍数,每对3人,每个人最多参加5次比赛 #include <iostream> using namespace std; ...

  7. Codeforces Round #135 (Div. 2) D - Choosing Capital for Treeland(两种树形DP)

  8. Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland

    time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standa ...

  9. Codeforces Round #180 (Div. 2) D. Fish Weight 贪心

    D. Fish Weight 题目连接: http://www.codeforces.com/contest/298/problem/D Description It is known that th ...

随机推荐

  1. 解放双手,markdown文章神器,Typora+PicGo+七牛云图床实现自动上传图片

    本文主要分享使用Typora作为Markdown编辑器,PicGo为上传图片工具,使用七牛云做存储来解放双手实现图片的自动化上传与管理.提高写作效率,提升逼格.用过 Markdown 的朋友一定会深深 ...

  2. 【剑指 Offer】03.数组中重复的数字

    题目描述 找出数组中重复的数字. 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次.请找出数组中 ...

  3. Invalid bound statement (not found): com.xxx.xxx.dao.ShopMapper.insertShop

    mybatis在编写完SQL,进行测试的时候出现了错误,显示 org.apache.ibatis.binding.BindingException: Invalid bound statement ( ...

  4. jenkins + Ansible Plugin + ansi-color 让结果显示颜色

    1 安装jenkins: 此处省略百余字......   2 安装jenkins的插件: Ansible Plugin AnsiColor Plugin    3 设置job 内容 让ansible ...

  5. sa-token 之权限验证

    权限验证 核心思想 所谓权限验证,验证的核心就是当前账号是否拥有一个权限码 有:就让你通过.没有:那么禁止访问 再往底了说,就是每个账号都会拥有一个权限码集合,我来验证这个集合中是否包括我需要检测的那 ...

  6. 以我的亲身经历,聊聊学python的流程,同时推荐学python的书

    因为干活要用到,所以我大概于19年5月开始学python,大概学了1个月后,我就能干公司的活了,而且这python项目还包含了机器学习等要素,大概3个月后,我还承担了项目里开发机器学习数据分析的任务. ...

  7. LuoguP5075 [JSOI2012]分零食

    题意 有\(A\)个人,\(m\)个糖,你可以选择一个\(k\),使第\(1\)$k$个人每个人至少得到一个糖,并且第$k+1$\(A\)个人都得不到糖.\(m\)个糖必须给完.对于每个方案都有一个欢 ...

  8. windows 系统 MySQL_5.6.21安装教程

      1.双击安装文件 mysql_installer_community_V5.6.21.1_setup.1418020972.msi,等待安装界面出现,见下图: 2.勾选:I accept thel ...

  9. JAVA高并发集合详解

    Queue(队列)主要是为了高并发准备的容器Deque:双端队列,可以反方向装或者取 最开始jdk1.0只有Vector和hashtable 默认所有方法都实现了synchronized锁,线程安全但 ...

  10. Web自动化测试python环境中安装 --selenium安装、火狐和火狐驱动版本、谷歌和谷歌驱动版本、测试

    一.安装selenium Windows命令行(cmd)输入pip install selenium(无须指定版本默认最新)或 pip install selenium==3.141.0(可指定版本) ...