当前位置:网站首页>2020 Blue Bridge Cup group B - move bricks - (greedy sorting +01 backpack)
2020 Blue Bridge Cup group B - move bricks - (greedy sorting +01 backpack)
2022-06-30 16:38:00 【Lovely and beautiful girl】
The question :
It's for you. n A brick , Each brick has a weight and value , Now let's put some bricks together , For each brick , The weight of all the bricks on him cannot exceed his own value . Ask you what is the maximum total value of the bricks built up .
reflection :
When the game was played, I felt that it was not easy , In fact, just think more . The first thing that comes to mind dp, But how to guarantee for each brick , The total weight on him is less than or equal to his value , How to maintain this . Actually draw a picture on paper , Think about how you can deal with the bricks first , Then dispose of the bricks below , Bit by bit , Because if you deal with the bricks below first , How do you choose , To ensure that the following are met . So first consider the above , Each brick from top to bottom can be processed linearly . Definition dp[i][j] Yes, before use i A brick , At this time, the weight is j The greatest value when . But how do you enumerate the order of bricks ? In fact, this is to sort , I have done this before , Like the king's game . This question can make i on top ,j Hereunder , We let wi+sum<vj,wj+sum>vi, Also is to let i When it's up there j You can choose , Give Way i Down there ,j No choice . So convert this formula , You can put sum lose ,sum It's all the weight above . And the formula is wi<vj,vi<wj. If you want to add two, you can get wi+vi<wj+vj. So just sort by this . Then for maintenance, the weight on each brick is less than its value , Actually in dp It is enough to add a judgment condition when . And then it's actually 01 knapsack , Added a greedy sort , And one more judgment condition . It can be optimized to 1 Dimensional .
Code :
2D version :
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define db double
#define int long long
#define PII pair<int,int >
#define mem(a,b) memset(a,b,sizeof(a))
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const int mod = 1e9+7,inf = 1e18;
const int N = 1e5+10,M = 2010;
int T,n,m,k;
PII va[N];
int dp[1005][20005];
bool cmp(PII A,PII B)
{
return A.fi+A.se<B.fi+B.se;
}
signed main()
{
IOS;
cin>>n;
for(int i=1;i<=n;i++) cin>>va[i].fi>>va[i].se;
sort(va+1,va+1+n,cmp);
m = n*20; // The weight is these at most
for(int i=1;i<=n;i++)
{
for(int j=0;j<=m;j++)
{
dp[i][j] = max(dp[i][j],dp[i-1][j]); // If this brick is not selected
if(va[i].se>=j-va[i].fi&&j>=va[i].fi) dp[i][j] = max(dp[i][j],dp[i-1][j-va[i].fi]+va[i].se); // If elected , Make sure the value is greater than the total weight above .
}
}
int maxn = 0;
for(int j=0;j<=m;j++) maxn = max(maxn,dp[n][j]);
cout<<maxn;
return 0;
}
One dimensional version :
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define db double
#define int long long
#define PII pair<int,int >
#define mem(a,b) memset(a,b,sizeof(a))
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const int mod = 1e9+7,inf = 1e18;
const int N = 1e5+10,M = 2010;
int T,n,m,k;
PII va[N];
int dp[20005];
bool cmp(PII A,PII B)
{
return A.fi+A.se<B.fi+B.se;
}
signed main()
{
IOS;
cin>>n;
for(int i=1;i<=n;i++) cin>>va[i].fi>>va[i].se;
sort(va+1,va+1+n,cmp);
m = n*20;
for(int i=1;i<=n;i++)
{
for(int j=m;j>=0;j--)
{
if(va[i].se>=j-va[i].fi&&j>=va[i].fi)
dp[j] = max(dp[j],dp[j-va[i].fi]+va[i].se);
}
}
int maxn = 0;
for(int j=0;j<=m;j++) maxn = max(maxn,dp[j]);
cout<<maxn;
return 0;
}
summary :
Think more , It's not hard , This is what I used to do in upc The original question I have done can be said .
边栏推荐
- [algorithm] after summarizing the four linked lists, I brushed two interview questions
- 备战数学建模36-时间序列模型2
- Types of waveguides
- 附加:(还没写,别看~~~)CorsFilter过滤器;
- CloudXR如何推动XR的未来发展
- 快照和备份
- MySQL8.0开启远程连接权限的方法步骤
- ArcMap operation series: 80 plane to latitude and longitude 84
- 电子烟强制性国家标准GB 41700-2022发布 2022年10月1日起实施
- [unity ugui] scrollrect dynamically scales the grid size and automatically locates the middle grid
猜你喜欢

Policy Center > Malware > Malware

How cloudxr promotes the future development of XR

MC Instruction Decoder

Google play index table

婴儿认知学习所带来的启发,也许是下一代无监督机器学习的关键

Finally understand science! 200 pictures to appreciate the peak of human wisdom

The new tea drinks are "dead and alive", but the suppliers are "full of pots and bowls"?

快照和备份

构建适合组织的云原生可观测性能力

RT thread heap size setting
随机推荐
从第三次技术革命看企业应用三大开发趋势
Unsupported major. minor version 52.0
详解Go语言中for循环,break和continue的使用
招标公告:2022年台州联通Oracle一体机和数据库维保服务项目
荣盛生物冲刺科创板:拟募资12.5亿 年营收2.6亿
Distributed machine learning: model average Ma and elastic average easgd (pyspark)
The difference between intermodulation and intermodulation
topic: Privacy, Deception and Device Abuse
halcon知识:区域专题【07】
Unsupported major.minor version 52.0
RT-Thread 堆区大小设置
What is XR extended reality and what are the XR cloud streaming platforms
Bidding announcement: Tianjin housing provident fund management center database all-in-one machine and database software project (budget: 6.45 million)
Table responsive layout tips for super nice
【机器学习】K-means聚类分析
Cloud XR, how to help industrial upgrading
Policy Center > Misrepresentation
Go zero micro Service Practice Series (VIII. How to handle tens of thousands of order requests per second)
360数科、蚂蚁集团等入选中国信通院“业务安全推进计划”成员单位
IIS无法加载字体文件(*.woff,*.svg)的解决办法