[DP] [01] [洛谷] P1510 味精填海
基础01背包DP
考察对背包决策过程的理解
#pragma GCC optimize(2)
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <vector>
#include <fstream>
#include <list>
#include <iomanip>
#include <numeric>
using namespace std;
typedef long long ll;
const int MAXN = 1e6 + 10;
ll inf = -1;
ll v, n, c;
bool flag = false;
int dp[MAXN] = {0};
int volume[MAXN];
int value[MAXN];
ll ans = inf;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>v>>n>>c;
for(int i = 1; i <= n; i++)
{
cin>>value[i]>>volume[i];
}
for(int i = 1; i <= n; i++)
{
for(int j = c; j >= volume[i]; j--)
{
dp[j] = max(dp[j], dp[j - volume[i]] + value[i]);
if(dp[j] >= v)
{
ans = max(ans, c - j);
flag = true;
}
}
}
flag ? cout<<ans<<endl : cout<<"Impossible"<<endl ;
return 0;
}