当前位置:网站首页>Move bricks (greedy perturbation + 01 backpack)
Move bricks (greedy perturbation + 01 backpack)
2022-07-26 01:48:00 【to cling】
The 13th National Blue Bridge Cup
Problem
Yes n n n Items , The first i i i The volume and value of each item are v i , w i v_i, w_i vi,wi . Now from here n Take some items out of the items , Line up , Make the value of each item greater than or equal to the item in front of it Total volume . Find the maximum value of the items taken out .
Solution
Greedy perturbation is a little metaphysical .. You can do it first 《 King's game 》 The subject , Get to know “ Perturbation ” The process of analyzing a topic .
First , Considering how to ensure that the total volume of an item in front of it is less than the value of the item , There will be no clue . However , If it's difficult, it's the opposite .
therefore , You can deal with the items in front first , Then deal with the following items first .After that, choose items .
After thinking, you can find , The selection order of items can affect the result , direct 01 The backpack seems inappropriate .( I don't quite understand why sorting items will affect 01 It's a backpack dp The process , I hope some big guys can explain it in the comment area , thank [doge])How to sort : Sort according to the sum of the value and volume of the goods from small to large .
The proof method is : Perturbation method .
The following is the proof processUnder the condition that the conditions in the topic are met :
Suppose... Of these items taken out i i i Items and The first i + 1 i + 1 i+1 The volume and value of each item are ( v i , w i v_i, w_i vi,wi)( v i + 1 , w i + 1 v_{i + 1}, w_{i + 1} vi+1,wi+1)
Set before i − 1 i - 1 i−1 The total volume of this article is s u m sum sumAccording to the conditions in the title : s u m + v i ≤ w i + 1 sum + v_i \leq w_{i + 1} sum+vi≤wi+1
In exchange for i i i Items and i + 1 i + 1 i+1 Location of items : Other items and article i + 1 i + 1 i+1 Items still meet the conditions in the title .( Simple thinking and analysis shows )
And the first i Whether the item meets the conditions , There is a relationship :
If after the exchange i i i Items Do not meet the conditions in the topic , Then the total value decreases , That is, the result becomes worse . So there are : s u m + v i + 1 > w i sum + v_{i + 1} > w_i sum+vi+1>wi
Two inequalities can be combined to obtain : w i − v i + 1 < s u m ≤ w i + 1 − v i w_i - v_{i + 1} < sum \leq w_{i + 1} - v_i wi−vi+1<sum≤wi+1−vi
namely w i − v i + 1 < w i + 1 − v i w_i - v_{i + 1} < w_{i + 1} - v_i wi−vi+1<wi+1−vi, It is reduced to : w i + v i < w i + 1 + v i + 1 w_i + v_i < w_{i + 1} + v_{i + 1} wi+vi<wi+1+vi+1It can be seen from the above analysis : When the first i i i The sum of the value and volume of an article Less than The first i + 1 i + 1 i+1 The sum of the value and volume of an item , The result after exchange Variation .
in other words : If all the items were sorted from small to large according to the sum of value and volume ,
After scrambling the order , There are more reverse pairs , The worse the result .
Code
#define ft first
#define sd second
#define pii pair<int, int>
const int N = 2e3 + 5;
pii s[N];
int f[20100];// Maximum volume :20 + 20000
int main()
{
IOS;
int n; cin >> n;
for (int i = 1; i <= n; i++)
cin >> s[i].ft >> s[i].sd;//first Is the volume ,second For value
sort(s + 1, s + n + 1, [&](pii &a, pii &b) {
return a.ft + a.sd < b.ft + b.sd;
});
//dp[i][v]: front i Items , The total volume is v when , The maximum value that can be accommodated
// Select the first i Item , Before request i - 1 The total volume of this article is less than i The value of items
// Select the first i When it comes to items , Maximum volume = s[i].ft + s[i].sd ( front i - 1 The total volume of the item plus i The volume of items v[i])
// Select the first i When it comes to items , Minimum volume = s[i].ft ( There are no objects in front ,i Is the first item )
for (int i = 1; i <= n; i++)
for (int v = s[i].ft + s[i].sd; v >= s[i].ft; v--)
f[v] = max(f[v], f[v - s[i].ft] + s[i].sd);
cout << *max_element(f, f + 20021) << endl;
return 0;
}
Over
边栏推荐
- Digital transformation behind the reshaping growth of catering chain stores
- Typora expiration solution, what if typora can't open
- "Weilai Cup" 2022 Niuke summer multi school training camp 2 g.[link with monotonic subsequence] block structure
- CPU的三种模式
- Practice sharing of monorepo based on yarn1.x
- C语言中的整型数据类型(你真的了解吗)
- 劳驾问一下各位老师 oracle 到pg cdc oracle 那边字段大写 pg 这边小写 同
- How to modify Oracle functions?
- Is it safe to open an account for stock speculation through the online account manager?
- MDK compilation process and arm compilation tool chain
猜你喜欢

Maximum side length of elements and squares less than or equal to the threshold (source: leetcode)

Test questions and answers of the latest Beijing Construction eight (materialman) mock examination in 2022

Leetcode537. Complex multiplication (yes, solved)

After reading this article, you should thoroughly understand how to do interface testing

MDK compilation process and arm compilation tool chain

The sales volume has won the championship repeatedly. Is the secret of Wuling's success only low price?

Big view +500 cases, software teams should improve R & D efficiency in this way

CPU的三种模式

C语言中的整型数据类型(你真的了解吗)

Nodejs builds cloud native microservice applications based on dapr, a quick start guide from 0 to 1
随机推荐
After reading this article, you should thoroughly understand how to do interface testing
IP address of the network
NFT market also began to diversify
The sales volume has won the championship repeatedly. Is the secret of Wuling's success only low price?
SVN版本控制分支、合并功能使用
excel中怎么显示数字/英文时间
Travel (split points and layers)
言语理解-片段阅读的结构剖析练习
Handler message mechanism - FWK layer
Mysql_ Note1
Cross Site Request Forgery (CSRF): impact, examples, and Prevention
CPU的三种模式
"Weilai Cup" 2022 Niuke summer multi school training camp 2 g.[link with monotonic subsequence] block structure
In spark SQL, date is used to display the day of the week according to the year, month and day_ format(date,‘u‘)
华为无线设备WDS配置命令
Recommend a super good UI automation tool: uiautomator2!
【Verilog数字系统设计(夏宇闻)3-----Verilog语法的基本概念1】
01. MySQL transaction isolation level and concurrent database access
pdf. JS introduction
6 + 1 skills of Software Test Engineer