Codeforces Round #657 (Div. 2) C. Choosing flowers(贪心)
题目链接: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(贪心)的更多相关文章
- 树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland
题目传送门 /* 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计 ...
- Codeforces Round #340 (Div. 2) C. Watering Flowers 暴力
C. Watering Flowers 题目连接: http://www.codeforces.com/contest/617/problem/C Descriptionww.co A flowerb ...
- 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 ...
- Codeforces Round #657 (Div. 2) B. Dubious Cyrpto(数论)
题目链接:https://codeforces.com/contest/1379/problem/B 题意 给出三个正整数 $l,r,m$,判断在区间 $[l,r]$ 内是否有 $a,b,c$ 满足存 ...
- Codeforces Round #657 (Div. 2) A. Acacius and String(字符串)
题目链接:https://codeforces.com/contest/1379/problem/A 题意 给出一个由 '?' 和小写字母组成的字符串,可以将 '?' 替换为小写字母,判断是否存在一种 ...
- Codeforces Round #246 (Div. 2) A. Choosing Teams
给定n k以及n个人已参加的比赛数,让你判断最少还能参加k次比赛的队伍数,每对3人,每个人最多参加5次比赛 #include <iostream> using namespace std; ...
- Codeforces Round #135 (Div. 2) D - Choosing Capital for Treeland(两种树形DP)
- 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 ...
- 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 ...
随机推荐
- day123:MoFang:直播间列表信息的前后端实现&创建房间的前后端实现
目录 1.服务端提供所有直播间的列表信息 2.前端显示房间列表 3.创建房间 1.服务端提供所有直播间的列表信息 1.marshmallow.py from marshmallow_sqlalchem ...
- 【C++】《Effective C++》第二章
第二章 构造/析构/赋值运算 条款05:了解C++默默编写并调用哪些函数 默认函数 一般情况下,编译器会为类默认合成以下函数:default构造函数.copy构造函数.non-virtual析构函数. ...
- SpringCloud系列之SpringCloud Stream
SpringCloud Stream SpringCloud Config SpringCloud Gatewa SpringCloud Hystrix SpringCloud 第一部分 文章目录 S ...
- 【Docker】runtime create failed: container_linux.go:345: 解决
------------------------------------------------------------------------------------------------- | ...
- 【ORA】ora-39700解决
- gRPC-go源码(1):连接管理
1 写在前面 在这个系列的文章中,我们将会从源码的层面学习和理解gRPC. 整个系列的文章的计划大概是这样的:我们会先从客户端开始,沿着调用路径逐步分析到服务端,以模块为粒度进行学习,考虑这个模块是为 ...
- QTextEdit字符串的高亮显示问题
20130222 鬼猫猫 整理 http://www.cnblogs.com/muyr/ 解决方法的原始地址 http://www.qtcn.org/bbs/read.php?tid=20335 背景 ...
- Jmeter-插件扩展及性能监控插件的安装
需要对http服务进行大数据量的传值测试:看看产品中的http服务,能支持传多少字符:目标值是希望能到10w+: 上次测试中,服务器总是内存满导致服务不响应,因此想增加对服务端的性能监控:查阅了smi ...
- 详解Mybatisplus
详解Mybatisplus MyBatis-Plus(简称 MP)是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发.提高效率而生. 特性: 无侵入**:只 ...
- 如何使用 Vuepress
项目结构 ├─docs │ ├─.vuepress --vuepress相关文件路径 (主要配置) │ │ ├─dist --build 打包生成路径 (自定义) │ │ ├─nav --导航条配置 ...