题目链接: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. (十)Python装饰器

    装饰器:本质就是函数,功能是为其他函数添加附加功能. 两个原则: 1.不修改被修饰函数的源代码 2.不修改被修饰函数的调用方式 一个栗子 def test(): res = 0 for i in ra ...

  2. 同一个网段内所有服务器virtual_router_id设置相同的后果

    /var/log/messages中一直报的错 one or more VIP associated with VRID mismatch actual MASTER advert bogus VRR ...

  3. Nginx(四):http服务器静态文件查找的实现

    上一篇nginx的文章中,我们理解了整个http正向代理的运行流程原理,主要就是事件机制接入,header解析,body解析,然后遍历各种checker,直到处理成功为止. 我们以访问一个普通文件为例 ...

  4. 【Oracle】win7安装报错

    在WIN7上安装oracle 10g时,提示如下信息: 正在检查操作系统要求... 要求的结果: 5.0,5.1,5.2,6.0 之一 实际结果: 6.1 检查完成.此次检查的总体结果为: 失败 &l ...

  5. LeetCode349. 两个数组的交集

    题目 给定两个数组,编写一个函数来计算它们的交集. 分析 数组元素值可以很大,所以不适合直接开数组进行哈希,这里要学习另一种哈希方式:集合 集合有三种,区别见下面代码随想录的Carl大佬的表格,总结的 ...

  6. CTFshow萌新赛-密码学签到

    查看密码信息 猜测为base家族 存在"^"符号,所以应该是在base64以上 使用base85解密 成功拿到flag

  7. ctfshow—web—web5

    打开靶机,代码审计 附上代码 <?php error_reporting(0); ?> <html lang="zh-CN"> <head> & ...

  8. fileinput模块用法

    fileinput模块功能: 提供拼接一个或多个文本文件的功能,可以通过使用for循环来读取一个或多个文本文件的所有行,从而进行逐行处理(如进行显示.替换.添加行号等). 其功能类似于linux命令的 ...

  9. ADB 基本命令

    ADB很强大,记住一些ADB命令有助于提高工作效率. 获取序列号: adb get-serialno 查看连接计算机的设备: adb devices 重启机器: adb reboot 重启到bootl ...

  10. 浅析Redis与IO多路复用器原理

    为什么Redis使用多路复用I/O Redis 是跑在单线程中的,所有的操作都是按照顺序线性执行的,但是由于读写操作等待用户输入或输出都是阻塞的,所以 I/O 操作在一般情况下往往不能直接返回,这会导 ...