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]); }