"Bzoj" "3163" "HEOI2013" Eden's new backpack problem

Source: Internet
Author: User

Multi-pack/thought problem

Repeatedly ask, each time from all items omitted one piece, ask the maximum benefit ...

This question I use zyf of a "violent" approach, is first preprocessed out G1[i][j] said 1~i item spent the maximum value of j dollars, G2[i][j] said I~n item spent the maximum value of j dollars (here I will all the item number added 1, reason ...) Because I didn't +1,wa at first. Qaq)

And then when asked, $ans=max_{j=0}^{e}\{ans,g1[d-1][j]+g2[d+1][e-j]\}$

Because the number is 0~n-1, so here $d-1$ this item will be out of bounds! I don't know why I've had it on CH--but I'm determined to WA when I'm on the Bzoj, so I've labeled all my Items + 1 ...

In fact, the complexity of this approach is O (QE), up to $10^8$, as for the reason for not tle ... I guess it's because the people who make the questions think that the e are all set to a lot (like 990~1000?). May make some more violent practices (are you this not violence?) ) Run over ... So e is random. So it is not easy to get to the upper bound = = (But the total time is not to 1000ms is also very strange ...) )

1 /**************************************************************2 problem:31633 User:tunix4 language:c++5 result:accepted6 time:816 Ms7 memory:9252 KB8 ****************************************************************/9  Ten //huce #6 A One#include <vector> A#include <cstdio> -#include <cstdlib> -#include <cstring> the#include <iostream> -#include <algorithm> - #defineRep (i,n) for (int i=0;i<n;++i) - #defineF (i,j,n) for (int i=j;i<=n;++i) + #defineD (i,j,n) for (int i=j;i>=n;--i) - using namespacestd; +   A intGetint () { at     intv=0, sign=1;CharCh=GetChar (); -      while(ch<'0'|| Ch>'9') {if(ch=='-') sign=-1; Ch=GetChar ();} -      while(ch>='0'&&ch<='9') {v=v*Ten+ch-'0'; Ch=GetChar ();} -     returnv*Sign ; - } -typedefLong LongLL; in Const intn=1010, inf=~0u>>2; - /*******************tamplate********************/ to intN,m,a[n],b[n],c[n],g1[n][n],g2[n][n]; + intMain () { - #ifndef Online_judge theFreopen ("a.in","R", stdin); * //freopen ("Output.txt", "w", stdout); $ #endifPanax Notoginsengn=getint (); -F (I,1, n) {a[i]=getint (); B[i]=getint (); c[i]=getint ();} theF (I,1, n) F (k,0, C[i]) F (J,k*a[i], +) +G1[i][j]=max (g1[i][j],g1[i-1][j-k*a[i]]+k*b[i]); A           theD (I,n,1) F (k,0, C[i]) F (J,k*a[i], +) +G2[i][j]=max (g2[i][j],g2[i+1][j-k*a[i]]+k*b[i]); -m=getint (); $      while(m--){ $         intD=getint () +1, E=getint (), ans=0; -F (J,0, e) Ans=max (ans,g1[d-1][j]+g2[d+1][e-j]); -printf"%d\n", ans); the     } -     return 0;Wuyi}
View Code 3163: [Heoi2013]eden's new backpack problem time limit:10 Sec Memory limit:256 MB
submit:206 solved:142
[Submit] [Status] [Discuss] Description

"Send a letter without an address, such emotions have a distance, you put who song, is how the heart calm, can say to me listen." ”
Lost memories of Eden always want to try to recall the past, but always can only clearly remember the feeling of missing, but can not recall her voice. In memory, she always likes to give Eden a riddle: On the night of Valentine's Day, when two people stroll in the busy downtown, looking at the delicate and exquisite dolls in the gift shop, she had a whim and asked Eden such a question: There are n dolls, each doll has the corresponding value, price, Each doll can be bought for a limited time, in the case of the price m fixed, how to choose which dolls and how many to buy each doll, in order to make the total price of the selected doll not more than M, and the value and maximum. As we all know, this is a classic multi-knapsack problem, Eden quickly solves it, but she seems to be a bit upset because her problem has been quickly resolved, so she wants to make it harder: multiple inquiries, every inquiry will give a new total price, and will remove a doll (that the doll cannot be selected), Ask the answer to the multi-pack at this point (that is, the problem described in the previous paragraph).
Eden is puzzled, but Eden doesn't want to be stumped, can you help him?

Input


The first line is a number n, indicating that there are N dolls, the doll numbering starting from 0
The second line starts with the next n lines, three numbers per line AI, Bi, c I, respectively, to buy a first I doll to be
The price to be paid, the value earned, and the number of times the purchase is limited to the first doll.
The next behavior, Q, indicates the number of queries.
Next Q line, two number di per line. EI indicates which doll is removed from each inquiry (note that the doll is numbered from 0) and the new total price of the inquiry. (Removal of the operation is not retained, that is, different inquiries independent of each other)

Output


Output Q line, line I output for the answer to the first question.

Sample Input5
2 3 4
1 2 1
4 1 2
2 1 1
3 2 3
5
1 10
2 7
3 4
4 8
0 5


Sample Output13
11
6
12
4


HINT



Altogether five kinds of dolls, the price value and the number of purchase limit are (2,3,4), (1,2,1), (4,1,2), (2,1,1), (3,2,3). Five inquiries, taking the first inquiry as an example. The first question is the removal of the doll numbered 1 and the maximum value that can be obtained when the money is 10, then the remaining doll is (2,3,4), (4,1,2), (2,1,1), (3,2,3), if the doll numbered 0 is bought 4 (ie all bought), then the doll numbered 3 buys a , the total value of the 10 yuan is spent, and the aggregate is 13. Can prove that there is no better solution. Pay attention to buying some kind of doll not necessarily buy light.


100. Data meet 1≤n≤1000, 1≤q≤3*105, 1≤a



I, Bi, c i≤100, 0≤d i < n, 0≤ei≤1000.






Source [Submit] [Status] [Discuss]

"Bzoj" "3163" "HEOI2013" Eden's new backpack problem

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.