当前位置:网站首页>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 .
边栏推荐
- Google play index table
- 赛芯电子冲刺科创板:拟募资6.2亿 实控人谭健为美国籍
- [time series database incluxdb] code example for configuring incluxdb+ data visualization and simple operation with C under Windows Environment
- What is XR extended reality and what are the XR cloud streaming platforms
- The difference between intermodulation and intermodulation
- Log4j2 进阶使用
- Solution for IIS failing to load font files (*.woff, *.svg)
- Three development trends of enterprise application viewed from the third technological revolution
- 今晚19:00知识赋能第2期直播丨OpenHarmony智能家居项目之控制面板界面设计
- 招标公告:2022年台州联通Oracle一体机和数据库维保服务项目
猜你喜欢

Policy Center > Misrepresentation
![[machine learning] K-means clustering analysis](/img/5f/3199fbd4ff2129d3e4ea518812c9e9.png)
[machine learning] K-means clustering analysis

备战数学建模34-BP神经网络预测2

24:第三章:开发通行证服务:7:自定义异常(来表征程序中出现的错误);创建GraceExceptionHandler来全局统一处理异常(根据异常信息,构建对应的API统一返回对象的,JSON数据);

优惠券种类那么多,先区分清楚再薅羊毛!

RT thread heap size Setting

Policy Center-User Data

Li Zexiang, a legendary Chinese professor, is making unicorns in batches

ArcMap operation series: 80 plane to latitude and longitude 84

BC1.2 PD协议
随机推荐
深入分析GadgetInspector核心代码
What is the difference between real-time rendering and pre rendering
Reptile (1) - Introduction to basic reptile theory
快照和备份
Carry two load balancing notes and find them in the future
药品管理系统加数据库,一夜做完,加报告
I 用c I 实现“栈”
Policy Center > Deceptive Behavior
Cesium-1.72 learning (add points, lines, cubes, etc.)
[bjdctf2020]the mystery of ip|[ciscn2019 southeast China division]web11|ssti injection
思源笔记:能否提供页面内折叠所有标题的快捷键?
GaussDB创新特性解读:Partial Result Cache,通过缓存中间结果对算子进行加速
MC Instruction Decoder
Mysql8 error: error 1410 (42000): you are not allowed to create a user with grant solution
婴儿认知学习所带来的启发,也许是下一代无监督机器学习的关键
[machine learning] K-means clustering analysis
Is your light on? Before you start to solve a problem, you need to know what the "real problem" is
备战数学建模34-BP神经网络预测2
CVPR 2022丨特斯联AI提出:基于图采样深度度量学习的可泛化行人重识别
[time series database incluxdb] code example for configuring incluxdb+ data visualization and simple operation with C under Windows Environment