BZOJ4247: Hanging Ornaments

Source: Internet
Author: User

Description
Joi has n a pendant on the phone, numbered 1 ... N. Joi can put some of them on the phone.
Joi's ornaments are somewhat different-some of them have hooks that can hang other pendants. Each pendant is either hung directly on the phone or hung on the hook of the other pendant. There are up to 1 hooks hanging directly on the phone.
In addition, each pendant has a joy value that is obtained during installation and is represented by an integer. If Joi hates a pendant, the joy of the pendant is a negative number.
Joi wants to maximize the sum of the joy values of all ornaments. Note that you do not have to hang all the hooks on the pendant, and you can do it without hanging it.
Input
The first line, an integer n, represents the number of ornaments.
Next N lines, line I (1<=i<=n) has two space-delimited integer ai and bi, which indicates that the pendant i has an AI hook, which will get the joy value of bi after installation.
Output
outputs a single integer representing the maximum number of attached ornaments on the phone
Sample Input
5
0 4
2-2 br> 1-1
0 1
0 3
Sample Output
5
HINT
put the pendant 2 directly on the phone, then the pendant 1 and hang Ornaments 5 are hung on the pendant 2 of the two hooks, you can get the maximum joy value 4-2+3=5.
1<=n<=2000
0<=ai<=n (1<=i<=n)
-10^6<=bi<=10^6 (1<=i<=n)
Source
JOI 2013~2014 Spring Training Sports 4 by POPOQQQ

01 Backpack, just a little extra one-dimensional means that there are still several hooks available, i.e. F[i][j] f[i][j] represents the maximum value that can be obtained by the first I I-I items and the remaining J-J Hooks. Note that the initial value is to be assigned a minimum value (instead of 0). To order all items according to the number of hooks from large to small, because it is possible to first put the hook into a negative and then add back.
Equation: F[i][j]=max (F[i−1][max (j−s[i].v,0) +1]+s[i].w,f[i−1][j]) F[i][j]=max (F[i-1][max (j-s[i].v,0) +1]+s[i].w,f[ I-1][J]);
Initial: The minimum value (f[0][1]=0 f[0][1]=0) that cannot be taken.
Target: F[N][0..N] The maximum value in the maximum F[N][0..N].

 #include <cstdio> #include <algorithm> #define INT_MIN-2100000000 using
namespace Std;
int n,f[2100][2100]; struct Data{int v,w;}
S[2100];
    void read (int &x) {char t=getchar (); X=0;int f=1;
    while ((t<48) or (t>57)) {if (t== '-') F=-1;t=getchar ();}
    while ((t>=48) and (t<=57)) {X=x*10+t-48;t=getchar ();}
X*=f;
    } BOOL CMP (data X,data y) {return x.v>y.v;} int main () {read (n);
    for (int i=0;i<=n;++i) f[0][i]=f[i][n+1]=int_min;
    for (int i=1;i<=n;++i) read (S[I].V), read (S[I].W);
    Sort (s+1,s+n+1,cmp);
    f[0][1]=0; for (int i=1;i<=n;++i) for (int j=0;j<=n;++j) F[i][j]=max (F[i-1][max (j-s[i].v,0) +1]+s[i].w,f[i-1][j]
    );
    int ans=int_min;
    for (int i=0;i<=n;++i) Ans=max (Ans,f[n][i]);
    printf ("%d", ans);
return 0; }

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.