POJ-1276 Cash machine multiple backpack binary optimization

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.