Title Link: https://cn.vjudge.net/problem/POJ-1276
Test instructions
Too lazy to write their own to watch, sleepy hurriedly write this back to the dorm to sleep, tomorrow morning also to test.
Ideas
Binary optimization of multiple backpacks.
The idea is to split n items into log (m) items, which allows these items to be combined out of a 1~n original item, which is used in 01 backpacks.
Submission Process
WA |
I don't understand num-=k. |
AC |
|
Code
#include <cstdio> #include <cstring> #include <algorithm>using namespace Std;const int maxn=1000+20, MAXM=10+20, Maxc=1e5+20;int CASH[MAXM], NUM[MAXN], DP[MAXC], total, N;char str[500];void compknap (int. cost, int weight) { for (int i=cost; i<=total; i++) Dp[i]=max (Dp[i], dp[i-weight]+cost);} void Zeroknap (int cost, int. weight) {for (int i=total; i>=cost; i--) Dp[i]=max (Dp[i], dp[i-weight]+cost);} int main (void) {while (scanf ("%d%d", &total, &n) ==2) {for (int i=0; i<n; i++) scanf ("%d%d" , &num[i], &cash[i]); memset (DP, 0, sizeof (DP)); for (int i=0; i<n; i++) {if (num[i]*cash[i]>=total) {Compknap (Cash[i], cash[i]); }else{int k=1; for (int k=1; k<num[i]; k*=2) {zeroknap (cash[i]*k, cash[i]*k); Num[i]-=k; } zeroknap (Cash[i]*num[i], cash[i]*num[i]); }} printf ("%d\n", Dp[total]); } return 0;}
| Time
Memory |
Length |
Lang |
submitted |
47ms |
552kB |
1063 |
C++ |
2018-08-10 09:24:56 |
POJ-1276 Cash machine multiple backpack binary optimization