Test instructions: There are n gangsters to enter a restaurant, the restaurant has a door, the initial size of the door is 0, the door will increase by 1 per second or decrease by 1 or unchanged, but the range in [0,k], and then each gangster has to arrive at the time of the restaurant ti, each person has a wealth value pi and their body value si, Only when the gangster arrives at the restaurant is the size of the door and Si equal before he can enter the restaurant, the restaurant will increase Pi, ask the restaurant to increase the total pi value is how much.
The F[i] indicates that the former I can allow the restaurant to increase the total pi value is how much, first the gangsters to the restaurant's time sequencing, and then compare between gangsters to reach the difference and the difference in size to determine if the former into the latter can also enter, f[i] = max (F[i], f[j] + pi).
#include <stdio.h>#include <string.h>#include <math.h>#include <algorithm>using namespace STD;Const intN = the;structGan {intT, p, s;} Gan[n];intN, K, T, F[n];BOOLCMP (gan A, gan b) {returnA.T < b.t;}intMain () {intCAsscanf("%d", &cas); while(cas--) {scanf("%d%d%d", &n, &k, &t); for(inti =1; I <= N; i++)scanf("%d", &gan[i].t); for(inti =1; I <= N; i++)scanf("%d", &GAN[I].P); for(inti =1; I <= N; i++)scanf("%d", &GAN[I].S); Sort (gan +1, gan +1+ N, CMP);memset(F,-1,sizeof(f)); f[0] =0; for(inti =1; I <= N; i++) { for(intj =0; J < I; J + +) {if(F[j]! =-1) {//If J can't go in, he can't judge if I can enter . inttemp = gan[i].t-gan[j].t;intTemp2 =fabs(GAN[I].S-GAN[J].S);if(temp >= temp2) f[i] = max (F[i], f[j] + GAN[I].P); } } }intres =0; for(inti =1; I <= N; i++) res = max (res, f[i]);printf("%d\n", res);if(CAS)printf("\ n"); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVA 672 (DP)