当前位置:网站首页>Acwing - the collection of pet elves - (multidimensional 01 Backpack + positive and reverse order + two forms of DP for the answer)
Acwing - the collection of pet elves - (multidimensional 01 Backpack + positive and reverse order + two forms of DP for the answer)
2022-07-05 07:45:00 【Lovely and beautiful girl】
The question :
There is n A pet , Catch them to a Physical strength and b Spirit ball of . Now you have m physical strength ,k An elf ball . Ask how many pets you can catch at most , Then when there are most pets , How much physical strength is left at most .
reflection :
Obviously, this can also optimize the dimension of enumerating items . Then triple cycle is ok . For enumeration h When , Reverse order and positive order are the same , Because the last layer m It's in reverse order , therefore h Even positive order , He also uses the upper layer every time , Not on this floor . If there is 4 Layer circulation , equally , Just one layer in reverse order . For those who require the most pets , Highest physical strength . Then just enumerate directly , Find out more pets , If the same, then look at the least physical use . although dp The definition of , I can't directly ask how much physical strength I can have left , But you can ask how much physical strength you used, right .
The second way , It's the gold coin title of the robot competition before . For example, the classic backpack volume and value , Just exchange the power of value and volume , Let what you ask be what you maintain , The result is known , Finally, enumerate and judge dp Whether the status inside meets the given limit is ok . So this question can define the state dp[i][j], Not more than i HP , Captured j A pet , At least how many elf balls should be used . Then finally enumerate all the States , See if < The number of ELF balls , If you can, you can update the answer . The same is true dp[i][j] Defined as , No more than i Spirit ball of , Captured j A pet , How much physical strength should be used at least . Then do the same .
How to determine the state
Namely :
1. Put the required answer , Become dp The answer , This is the most common way .
2. Put the required answer , Put it in dp In the defined state , and dp The answer is a known condition .
For how to define the state , It depends on the scope of each limitation , See how many cycles you can use at least , Try to choose the one with the best time complexity . But we must ensure that there is an optimal substructure to deal with .
Another thing worth noting is , Generally, the first dimension enumeration items are optimized ,dp The state of is the value of the upper layer every time , So don't consider not choosing i The emptying of this item . Actually, I have considered . But if it is not optimized , Then you have to go , Otherwise, there will be less updates . It must be from 1 To m All have to enumerate , Can't fall .
Not more than j Spirit ball and not super k Physical strength , How many pets can you catch at most .
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define db double
#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 = 1e9;
const int N = 2e5+10,M = 2010;
int T,n,m,k,h;
PII va[110];
int dp[1010][510];
signed main()
{
IOS;
cin>>m>>h>>n;
for(int i=1;i<=n;i++) cin>>va[i].fi>>va[i].se;
for(int i=1;i<=n;i++)
{
for(int j=m;j>=1;j--)
{
for(int k=1;k<=h;k++)
{
if(j>=va[i].fi&&k>=va[i].se)
dp[j][k] = max(dp[j][k],dp[j-va[i].fi][k-va[i].se]+1);
}
}
}
int maxn = 0,sum = 0;
for(int k=0;k<h;k++)
{
if(maxn<dp[m][k])
{
maxn = dp[m][k];
sum = h-k;
}
else if(maxn==dp[m][k]) sum = max(sum,h-k);
}
cout<<maxn<<" "<<sum<<"\n";
return 0;
}
Not more than j Physical strength and grasp k Our pet , At least how many elf balls to use .
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define db double
#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 = 1e9;
const int N = 2e5+10,M = 2010;
int T,n,m,k,h;
PII va[110];
int dp[510][110];
signed main()
{
IOS;
cin>>m>>h>>n;
for(int i=1;i<=n;i++) cin>>va[i].fi>>va[i].se;
mem(dp,0x3f);
for(int i=0;i<=h;i++) dp[i][0] = 0;
for(int i=1;i<=n;i++)
{
for(int j=h;j>=1;j--)
{
for(int k=1;k<=n;k++)
{
if(j>=va[i].se)
dp[j][k] = min(dp[j][k],dp[j-va[i].se][k-1]+va[i].fi);
}
}
}
int maxn = -inf,sum = 0;
for(int i=0;i<h;i++)
{
for(int j=0;j<=n;j++)
{
if(dp[i][j]<=m)
{
if(maxn<j)
{
maxn = j;
sum = h-i;
}
else if(maxn==j) sum = max(sum,h-i);
}
}
}
cout<<maxn<<" "<<sum<<"\n";
return 0;
}
Not more than j The elf ball and caught k Our pet , How much physical strength should we use at least .
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define db double
#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 = 1e9;
const int N = 2e5+10,M = 2010;
int T,n,m,k,h;
PII va[110];
int dp[1010][110];
signed main()
{
IOS;
cin>>m>>h>>n;
for(int i=1;i<=n;i++) cin>>va[i].fi>>va[i].se;
mem(dp,0x3f);
for(int i=0;i<=m;i++) dp[i][0] = 0;
for(int i=1;i<=n;i++)
{
for(int j=m;j>=1;j--)
{
for(int k=1;k<=n;k++)
{
if(j>=va[i].fi)
dp[j][k] = min(dp[j][k],dp[j-va[i].fi][k-1]+va[i].se);
}
}
}
int maxn = -inf,sum = 0;
for(int i=0;i<=m;i++)
{
for(int j=0;j<=n;j++)
{
if(dp[i][j]<h)
{
if(maxn<j)
{
maxn = j;
sum = h-dp[i][j];
}
else if(maxn==j) sum = max(sum,h-dp[i][j]);
}
}
}
cout<<maxn<<" "<<sum<<"\n";
return 0;
}
Robot gold coin topic :
reflection :
If you follow the normal way of thinking, it is dp[i][j] Before use i Card roll , No more than j Time for , How many gold coins can you get at most . But such a double cycle must have timed out . But the scope of finding gold coins is very small . therefore dp[i][j] Defined as before i Card roll , Get no more than j Gold coins , At least how much time . Finally, just enumerate the answer of gold coin , On the premise that the time does not exceed the conditions .
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 = 2e5+10,M = 2010;
int T,n,m,k;
PII va[N];
int dp[1010][30010];
signed main()
{
IOS;
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>va[i].fi;
for(int i=1;i<=n;i++) cin>>va[i].se;
int R = n*30;
mem(dp,0x3f);
for(int i=0;i<=n;i++) dp[i][0] = 0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=R;j++)
{
dp[i][j] = min(dp[i][j],dp[i-1][j]); // Note that this is a double cycle. You can't throw it away , If you optimize a single loop , Then itself is already on the next level , So don't write anymore , But it needs to be updated here . It must be from 1 To R, Cannot from va[i].se Start , Because there will be less .
if(j>=va[i].se)
dp[i][j] = min(dp[i][j],dp[i-1][j-va[i].se]+va[i].fi);
}
}
int maxn = 0;
for(int i=1;i<=R;i++)
{
if(dp[n][i]<=m)
maxn = max(maxn,i);
}
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 = 2e5+10,M = 2010;
int T,n,m,k;
PII va[N];
int dp[30010];
signed main()
{
IOS;
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>va[i].fi;
for(int i=1;i<=n;i++) cin>>va[i].se;
int R = n*30;
mem(dp,0x3f);
dp[0] = 0;
for(int i=1;i<=n;i++)
{
for(int j=R;j>=va[i].se;j--) // because dp Itself is the upper layer , So here from va[i].se It's okay at first .
dp[j] = min(dp[j],dp[j-va[i].se]+va[i].fi);
}
int maxn = 0;
for(int i=1;i<=R;i++)
{
if(dp[i]<=m)
maxn = max(maxn,i);
}
cout<<maxn;
return 0;
}
summary :
Think more , Understand the essence of Algorithm .
边栏推荐
- Typecho adds Baidu collection (automatic API submission plug-in and crawler protocol)
- Calibre garbled
- selenium 元素定位
- Threads and processes
- Global and Chinese market of digital shore durometer 2022-2028: Research Report on technology, participants, trends, market size and share
- I 用c l 栈与队列的相互实现
- Application of ultra pure water particle counter in electronic semiconductors
- Readme, self study record
- STM32 knowledge points
- CADD课程学习(5)-- 构建靶点已知的化合结构(ChemDraw)
猜你喜欢
Altium Designer 19.1.18 - 清除测量距离产生的信息
Miracast技术详解(一):Wi-Fi Display
Package ‘*****‘ has no installation candidate
Detailed explanation of miracast Technology (I): Wi Fi display
QT small case "addition calculator"
What is Bezier curve? How to draw third-order Bezier curve with canvas?
MySQL - storage engine
Significance and requirements of semiconductor particle control
Basic series of SHEL script (III) for while loop
Oracle-触发器和程序包
随机推荐
Opendrive arc drawing script
Opendrive record
Cygwin installation
Function and usage of function pointer
Using C language to realize IIC driver in STM32 development
Application of ultra pure water particle counter in electronic semiconductors
Calibre garbled
[MySQL] database knowledge record
When jupyter notebook is encountered, erroe appears in the name and is not output after running, but an empty line of code is added downward, and [] is empty
NSIS search folder
软件设计师:03-数据库系统
Eclipse project recompile, clear cache
Linked list (establishment, deletion, insertion and printing of one-way linked list)
TCP and UDP
611. Number of effective triangles
MySql——存储引擎
UNIX commands often used in work
Nombre - 1. Création de tableaux
Global and Chinese market of blackbody calibration source 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets for medical oxygen machines 2022-2028: Research Report on technology, participants, trends, market size and share