CF ROUND946 (DIV. 3)E
Money Buys Happiness
题面翻译
你是一个物理学家。一开始你没有钱,每个月的末尾你会得到 \(x\) 英镑。在第 \(i\) 个月里,你可以付出 \(c_i\) 英镑,获取 \(h_i\) 的幸福。
在任何时刻你都不能欠钱,问你在 \(m\) 个月过后最多能获得多少幸福。
保证 \(\sum h_i \leq 10^5\)。
题目描述
Being a physicist, Charlie likes to plan his life in simple and precise terms.
For the next $ m $ months, starting with no money, Charlie will work hard and earn $ x $ pounds per month. For the $ i $ -th month $ (1 \le i \le m) $ , there'll be a single opportunity of paying cost $ c_i $ pounds to obtain happiness $ h_i $ .
Borrowing is not allowed. Money earned in the $ i $ -th month can only be spent in a later $ j $ -th month ( $ j>i $ ).
Since physicists don't code, help Charlie find the maximum obtainable sum of happiness.
输入格式
The first line of input contains a single integer $ t $ ( $ 1 \le t \le 1000 $ ) — the number of test cases.
The first line of each test case contains two integers, $ m $ and $ x $ ( $ 1 \le m \le 50 $ , $ 1 \le x \le 10^8 $ ) — the total number of months and the monthly salary.
The $ i $ -th of the following $ m $ lines contains two integers, $ c_i $ and $ h_i $ ( $ 0 \le c_i \le 10^8 $ , $ 1 \le h_i \le 10^3 $ ) — the cost and happiness on offer for the $ i $ -th month. Note that some happiness may be free ( $ c_i=0 $ for some $ i $ 's).
It is guaranteed that the sum of $ \sum_i h_i $ over all test cases does not exceed $ 10^5 $ .
输出格式
For each test case, print a single integer, the maximum sum of happiness Charlie could obtain.
样例 #1
样例输入 #1
7
1 10
1 5
2 80
0 10
200 100
3 100
70 100
100 200
150 150
5 8
3 1
5 3
3 4
1 5
5 3
2 5
1 5
2 1
5 3
2 5
2 4
4 1
5 1
3 4
5 2
2 1
1 2
3 5
3 2
3 2
样例输出 #1
0
10
200
15
1
9
9
提示
In the first test case, Charlie only gets paid at the end of the month, so is unable to afford anything.
In the second test case, Charlie obtains the free happiness in the first month.
In the third test case, it's optimal for Charlie to buy happiness in the second month. Even with money left at the end, Charlie could not go back in time to obtain the happiness on offer in the first month.
本题思路
题目保证在h[i]为1e5,即提示要从h上下文章而不是ci(钱数)
钱数ci很大,且因为m=50无法暴搜(没月选或不选2^m种方案数),要DP
但以当前金钱为状态,每种金钱的最大幸福值,这样dp状态转移数量就会过多
这里反过来设fj表示当前幸福值为j时,当前剩余钱最多是多少
fj表示j这个幸福值剩余钱最多是多少
AC_cdoe
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const int inf = 0x3f3f3f3f;
const int N = 2 * 1e5 + 10;
inline int read() {
int f = 1, x = 0;
char ch;
do {
ch = getchar();
if(ch == '-') f = -1;
}while(ch < '0' || ch > '9');
do {
x = x * 10 + ch - '0';
ch = getchar();
}while(ch >= '0' && ch <= '9');
return f * x;
}
int a[N], b[N];
i64 f[N];
int _;
int main()
{
_ = read();
while(_ -- ) {
int n, x;
n = read(), x = read();
int s = 0;
for(int i = 1; i <= n; ++ i) {
a[i] = read(), b[i] = read();
s += b[i];//计算h
}
fill(f, f + s + 1, -1e18);//初始化负无穷
f[0] = 0;
//背包:但这里体积和价值调换(每个状态里存价值),即dp值存的是花费
for(int i = 1; i <= n; ++ i) {
for(int j = s; j >= 0; -- j) {
//要买则f[j-b[i]]幸福值状态当前剩余的钱要>=a[i]才可以买,买完之后钱数即f[j-b[i]]-a[i]
if(j >= b[i] && f[j - b[i]] >= a[i]) f[j] = max(f[j], f[j - b[i]] - a[i]);
f[j] += x;//每个月加的工资
}
}
int ans = 0;
for(int i = 1; i <= s; ++ i)//最后看哪个dp数组>=0
if(f[i] >= 0) ans = i;
printf("%d\n", ans);
}
return 0;
}
本人思路
一眼DP,01背包,设计状态dp[i]:为钱数为i时可以得到的最大价值,倒着做一遍01
因为获得月薪只能第二个月来花,且最后一个月得到的月薪不能花,(处理边界)所以这里从第二个月开始枚举,最后特判一下第一个月是否可以花费 = 0
喜得SF,然后发现dp[i]范围开错,最后输出的应该是超大金钱k···最后还写错甚至没到TLE程度就先WA掉
愚蠢做法
#include <iostream>
using namespace std;
using i64 = long long;
int n, m;
int solve() {
cin >> n >> m;
int w[n] {}, h[n] {};
i64 k = (n - 1) * m + 1;
i64 dp[k] = {0};
for(int i = 0; i < n; ++ i) cin >> w[i] >> h[i];
if(n == 1) {
if(w[0] == 0) cout << h[1] << endl, 0;
else return cout << 0 << endl, 0;
}
for(int i = 1; i < n; ++ i) {
for(int j = k; j >= w[i]; -- j) {
dp[j] = max(dp[j], dp[j - w[i]] + h[i]);
}
}
i64 l = 0;
for(int i = 0; i < k; ++ i) l = max(l, dp[i]);
if(w[0] == 0) l += h[0];
cout << l << endl;
return 0;
}
int _;
int main()
{
cin >> _;
while(_ -- ) {
solve();
}
return 0;
}
CF ROUND946 (DIV. 3)E的更多相关文章
- CF #376 (Div. 2) C. dfs
1.CF #376 (Div. 2) C. Socks dfs 2.题意:给袜子上色,使n天左右脚袜子都同样颜色. 3.总结:一开始用链表存图,一直TLE test 6 (1)如果需 ...
- CF #375 (Div. 2) D. bfs
1.CF #375 (Div. 2) D. Lakes in Berland 2.总结:麻烦的bfs,但其实很水.. 3.题意:n*m的陆地与水泽,水泽在边界表示连通海洋.最后要剩k个湖,总要填掉多 ...
- CF #374 (Div. 2) D. 贪心,优先队列或set
1.CF #374 (Div. 2) D. Maxim and Array 2.总结:按绝对值最小贪心下去即可 3.题意:对n个数进行+x或-x的k次操作,要使操作之后的n个数乘积最小. (1)优 ...
- CF #374 (Div. 2) C. Journey dp
1.CF #374 (Div. 2) C. Journey 2.总结:好题,这一道题,WA,MLE,TLE,RE,各种姿势都来了一遍.. 3.题意:有向无环图,找出第1个点到第n个点的一条路径 ...
- CF #371 (Div. 2) C、map标记
1.CF #371 (Div. 2) C. Sonya and Queries map应用,也可用trie 2.总结:一开始直接用数组遍历,果断T了一发 题意:t个数,奇变1,偶变0,然后与问的 ...
- CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组
题目链接:CF #365 (Div. 2) D - Mishka and Interesting sum 题意:给出n个数和m个询问,(1 ≤ n, m ≤ 1 000 000) ,问在每个区间里所有 ...
- CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组(转)
转载自:http://www.cnblogs.com/icode-girl/p/5744409.html 题目链接:CF #365 (Div. 2) D - Mishka and Interestin ...
- CF#138 div 1 A. Bracket Sequence
[#138 div 1 A. Bracket Sequence] [原题] A. Bracket Sequence time limit per test 2 seconds memory limit ...
- CF 552(div 3) E Two Teams 线段树,模拟链表
题目链接:http://codeforces.com/contest/1154/problem/E 题意:两个人轮流取最大值与旁边k个数,问最后这所有的数分别被谁给取走了 分析:看这道题一点思路都没有 ...
- CF 222 (DIV 1)
A: 我是bfs出一颗树,然后删掉树后面的k个结点. 其实也可以直接bfs出一块连通的s - k个点,其余的.打X就可以了. 很水的题目. /* *************************** ...
随机推荐
- 仿EXCEL插件,智表ZCELL产品V2.2 版本发布,增加获取单元格类型、样式功能,优化键盘事件、数值千分位等功能
详细请移步 智表(ZCELL)官网www.zcell.net 更新说明 这次更新主要应用户要求,增加获取单元格类型.样式功能,优化键盘事件.数值千分位等功能 ,欢迎大家体验使用. 本次版本更新内容如 ...
- live555开发笔记(三):live555创建RTSP服务器源码剖析,创建h264文件rtsp服务器源码深度剖析
前言 对于live555的rtsp服务器有了而基本的了解之后,进一步对示例源码进行剖析,熟悉整个h264文件流媒体的开发步骤. Demo 播放本地文件,多路播放的时候,总是以第 ...
- System.Drawing.Point与System.Windows.Point的异同
在C#中,System.Drawing.Point 和 System.Windows.Point 是两个不同的结构,分别属于不同的命名空间,用于表示二维平面中的点.尽管它们的功能相似,但在使用场景和实 ...
- AssemblyResolve巧解未能加载文件或程序集“Newtonsoft.Json, Version=6.0.0.0的问题
问题:未能加载文件或程序集"Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aee ...
- 判别式 AI 与生成式 AI
本文摘选来自: AI 智能体开发指南 一.背景 为了解决不同的应用场景,在AI的发展过程中,机器学习模型逐步分化为 判别式(Discriminative)和生成式(Generative) 两各技术路线 ...
- 洛谷题解:P12364 [蓝桥杯 2022 省 Python B] 寻找整数
注:可以在两分钟内跑出. 看到这题,暴力枚举跑不出来.如果你有没有充分的数学知识,那又怎么办呢? 减少枚举量 首先,注意到许多余数都是 \(11\),有图为证: 设这个数为 \(n\),则有: \[n ...
- odoo16跨域问题解决办法--适用app端、web端、跨系统接口
Odoo的跨域问题: 由于浏览器的同源策略所引起的.同源策略是一种安全策略,它限制了一个源(协议.域名.端口)的文档或脚本如何与另一个源的资源进行交互. 如果两个源不同,则无法进行跨域交互.因此,如果 ...
- JVM划重点:引用类型、垃圾回收算法和内存划分
一.Java四种引用类型 每种编程语言都有操作内存中元素的方式,例如在 C 和 C++ 里是通过指针,而在 Java 中则是通过"引用"(Reference).在 Java ...
- React-native之Flexbox
本文总结: 我们学到了 React Native 的 Flexbox 布局,它让写样式变得更方便啦! Flexbox 就像一个有弹性的盒子,有主轴和交叉轴(行或列). 在 RN 里写样式要用 Styl ...
- Vue3中Mock数据的简单方案
因为Vue3项目开发中需要用到Mock数据,所以这里记录一种快速Mock数据的方法. 一.安装 首先,你需要安装 axios 和 axios-mock-adapter. npm install axi ...