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的更多相关文章

  1. CF #376 (Div. 2) C. dfs

    1.CF #376 (Div. 2)    C. Socks       dfs 2.题意:给袜子上色,使n天左右脚袜子都同样颜色. 3.总结:一开始用链表存图,一直TLE test 6 (1)如果需 ...

  2. CF #375 (Div. 2) D. bfs

    1.CF #375 (Div. 2)  D. Lakes in Berland 2.总结:麻烦的bfs,但其实很水.. 3.题意:n*m的陆地与水泽,水泽在边界表示连通海洋.最后要剩k个湖,总要填掉多 ...

  3. CF #374 (Div. 2) D. 贪心,优先队列或set

    1.CF #374 (Div. 2)   D. Maxim and Array 2.总结:按绝对值最小贪心下去即可 3.题意:对n个数进行+x或-x的k次操作,要使操作之后的n个数乘积最小. (1)优 ...

  4. CF #374 (Div. 2) C. Journey dp

    1.CF #374 (Div. 2)    C.  Journey 2.总结:好题,这一道题,WA,MLE,TLE,RE,各种姿势都来了一遍.. 3.题意:有向无环图,找出第1个点到第n个点的一条路径 ...

  5. CF #371 (Div. 2) C、map标记

    1.CF #371 (Div. 2)   C. Sonya and Queries  map应用,也可用trie 2.总结:一开始直接用数组遍历,果断T了一发 题意:t个数,奇变1,偶变0,然后与问的 ...

  6. 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) ,问在每个区间里所有 ...

  7. 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 ...

  8. CF#138 div 1 A. Bracket Sequence

    [#138 div 1 A. Bracket Sequence] [原题] A. Bracket Sequence time limit per test 2 seconds memory limit ...

  9. CF 552(div 3) E Two Teams 线段树,模拟链表

    题目链接:http://codeforces.com/contest/1154/problem/E 题意:两个人轮流取最大值与旁边k个数,问最后这所有的数分别被谁给取走了 分析:看这道题一点思路都没有 ...

  10. CF 222 (DIV 1)

    A: 我是bfs出一颗树,然后删掉树后面的k个结点. 其实也可以直接bfs出一块连通的s - k个点,其余的.打X就可以了. 很水的题目. /* *************************** ...

随机推荐

  1. SQL 强化练习 (十)

    这一周都被客户搞得很惨.... 项目主流程不推进, 尽搞一些无关紧要的事情, 什么界面 ui 美化, 增加什么按钮进度条, 模糊查询...各种乱七八糟的需求, 挡都挡不住呀... 真的是把我当全栈使了 ...

  2. SQL 强化练习 (六)

    本以为学会了Python 就已经天下无敌, 果然, 我还是太傻太天真了. 业务中几乎就没有用 Python 来直接连接数据库进行操作, 当然我是说数据这块哈. 哎, 难受, 还是用的 sql 这种方式 ...

  3. CUDA原子操作

    这节主要涉及到一个多线程情况下存在的数据竞争问题 -- 多个线程同时访问共享数据时,由于没有正确的同步机制,导致数据出现不一致的情况. C/C++ 多线程中,可以通过互斥锁(mutex).原子操作(a ...

  4. 经典webshell流量特征

    开门见山,不说废话 判断条件 是否符合通信的特征 请求加密的数据和响应包加密的类型一致 是否一直向同一个url路径发送大量符合特征的请求,并且具有同样加密的响应包 一 .蚁剑 特征为带有以下的特殊字段 ...

  5. Vue3源码解析--收集的依赖是什么?怎么收集的?什么时候收集的?

    从Vue开始较大范围在前端应用开始,关于Vue一些基础知识的讨论和面试问题就在开发圈子里基本上就跟前几年的股票和基金一样,楼下摆摊卖酱香饼的阿姨都能说上几句那种.找过前端开发工作或者正在找开发工作的前 ...

  6. Java基于线程池和AQS模拟高并发

    概述   <手写高并发下线程安全的单例模式>主要介绍使用枚举类实现JAVA单例模式,以及在高并发环境下验证此单例模式是线程安全的.本文借助ReentrantLock.CountDownLa ...

  7. CSP-S 2020模拟训练题1-信友队T4 二维码

    题意简述 有一个初始全白的\(n*m\)大小的二维网格,每次可以选择一行或一列染全白或全黑.问可以通过任意次该操作得到多少不同的网格.答案对998244353取模. 分析 不难发现,无论怎么染色,都不 ...

  8. kubernetes网络组件calico详解

    一.Calico介绍 Calico是一种容器之间互通的网络方案,在虚拟化平台中,比如OpenStack.Docker等都需要实现workloads之间互连,但同时也需要对容器做隔离控制,就像在Inte ...

  9. Centos7.x根分区扩容

    背景说明 我们在部署好的系统中,随着数据的不断增加, 发现根分区频繁出现满载问题,这种情况下,我们需要对根分区进行扩容. 方案说明 • 使用空闲磁盘扩容到根分区 • 使用空闲的分区扩容到根分区 • 使 ...

  10. .NET 10 支持Linux 的Shebang(Hashbang)

    .NET 10 Preview 5 带来的C# 文件脚本化运行,在 Linux/Unix 系统中通过 #!/usr/bin/dotnet run 支持 Shebang(Hashbang) 的详细说明: ...