Cqbzoj 3325 Stealing Dragons

Source: Internet
Author: User

Title Description
Yi Master again in stealing Dragons, he has n skills, each skill can only be used once, consume different mana value, causing different damage. Tai Lung has a total of x points of blood, Yi Master is also only the M-point magic value.
Since the enemy has risen, at least the Qi point Mana is required to prepare for the dragon before using the skill of the first I.
Can you steal the Big dragon, if not, the maximum damage caused by the output.
Input
"Input Format"
First line three integers n,m,x;
Second to n+1 rows of three integers per line w,d,q, the table consumes, at least how much mana left to use, damage.
Output
"Output Format"
If it can be stolen, the output "Q" and the blood volume of the Great Dragon
Otherwise the output "B", the rear line output maximum damage.
Sample input
2 4 1
10 15 10
5 10 5
Sample output
B
0

First what big dragon blood quantity not to pull, this is a DP knapsack question, or one-dimensional, but have a problem:

"At least how much mana left": This limit is actually the essence of the problem,
This limitation has the aftereffect of the problem,
Why.
Think about it, set the last item selected as X, each item consumes pi, at least Qi
So at least the required backpack capacity m=p1+p2+...+pn+ (QX-PX);
And x can be changed, then the QX-PX may also be smaller, the order of different items may be the answer, may also explode 0 ...
So we must sort.
According to the previous example:
Mmin=p1+p2+...+pn+min (QX-PX)
So the last selection qx-px the smallest items, so from (q-p) to small sort.
But stop, 01 knapsack problem is backward, so should from small to big row.
AC Code:

 #include <cstdio> #include <algorithm> using namespace std;
    int Getint () {int ans=0,flag=0;
    char c;
        while (1) {C=getchar ();
            if (c<= ' 9 ' &&c>= ' 0 ') {ans=ans*10+c-' 0 ';
        flag=1;
    } else if (flag) return ans;
    }} struct node{int w,d,q;
    BOOL operator < (const node next) const{return (Q-W) < (NEXT.Q-NEXT.W);
}}t[12001];
int n,m,x,f[100001];
    int main () {N=getint (), M=getint (), X=getint ();
        for (int i=1;i<=n;i++) {t[i].w=getint (), T[i].q=getint (), T[i].d=getint (); if (t[i].q>m| |
    t[i].w>m) I--, n--;
    } sort (t+1,t+1+n);
        for (int. i=1;i<=n;i++) {for (int j=m;j>=t[i].q;j--) F[j]=max (F[J],F[J-T[I].W]+T[I].D);
    if (f[m]>=x) {printf ("q\n%d", x); return 0;}
} printf ("b\n%d", F[m]); }

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.