Title: Uva-10280old Wine into New bottles (full backpack + pruning)
The main topic: Now to put the old wine into the new bottle, each bottle has the smallest maximum capacity requirements, and then give you l wine, in the give you n bottles, each bottle of the specification also give you, each kind of bottle supply is unlimited, ask how to arrange these wine to make the remaining wine the least.
Problem-solving ideas: The problem is a complete backpack, but the first was the data scare, 10^9ml, and then there are 100 bottles, the maximum minimum capacity of the bottle is also 4000 or so, directly to the full backpack definitely timed out. Then saw the great God of the topic, there is a law: the maximum minimum capacity of each kind of bottle is continuous, so many times with this bottle, will make the smallest and largest capacity value closer and finally reached the overlapping part. So (k + 1) * min > k * max, so the wine from the back of K * min will be ready to be loaded. This is a very large pruning, and the problem is to consider the volume of duplication, with a vis array to record.
Code:
#include <cstdio> #include <cstring>const int maxn = 1e8 + 5;const int N = 105;const int M = 4505;int DP[MAXN]; int V[n][2];int vis[m];int L, n;void Init () {memset (DP, 0, sizeof (DP)), memset (Vis, 0, sizeof (VIS)); for (int i = 0; i < n; i++) for (int j = v[i][0]; J <= V[i][1] && J <= L; j + +) if (!vis[j]) vis[j] = 1;dp[0] = 1;} int main () {int t;int maxc, minc;scanf ("%d", &t), while (t--) {maxc = -1;minc = m;scanf ("%d%d", &l, &n); l *= 1000;int k = -1;for (int i = 0; i < n; i++) {scanf ("%d%d", &v[i][0], &v[i][1]); if (K < v[i][0] * V[i][0]/ (V[i][1]-v[i][0])) K = v[i][0] * v[i][0]/(v[i][1]-v[i][0]), if (Maxc < v[i][1]) MAXC = v[i][1];if (Minc > v[i][0]) minc = v[i][0];} if (l >= k) {printf ("0\n"),} else {init (); if (l <= maxc && Vis[l]) {printf ("0\n");} else {for (int i = mi nc I <= maxc; i++) for (int j = i; J <= L; j + +) {if (Vis[i] && dp[j-i]) dp[j] = 1;} int i;for (i = l; I >= 0; i--) if (Dp[i]) break;printf ("%d\n", L-i);}} if (t) printf ("\ n");} return 0;}
Uva-10280old Wine into New bottles (full backpack + pruning)