Algorithm: UVA 672 gangsters (DP)

Source: Internet
Author: User
Tags abs

The main effect of the topic:

There is a hotel door will change the size of the range is "0,k", the door per second size can be larger than 1, can be reduced by 1, can also be unchanged.

Now there are n individuals, their size is Si, everyone in ti always want to enter the hotel, only at ti moment the size of the hotel door and this person's size is equal to the size of the man, this person can enter.

Each person has a value pi, when someone enters the hotel, the hotel will increase the PI value.

In [0,t] this period (0 seconds when the hotel's door size status is 0), so that some people enter the hotel, make the total pi value maximum.

Ideas:

At the beginning, two people at the same time could not enter simultaneously, the result WA for a long period. In fact, if the same time, the size of two people are the same, the state of the door is the same, then two people can enter at the same time.

Method 1:

First of all, according to the time t from small to large sort.

First of all, we know that in t seconds, if you want from state I to J, only ABS (I-J) <= T is feasible

F[I][J] Indicates: The first person, the maximum value of the gate state of J.

Initialize f[0] [1..K] =-1, f[0][0] = 0;

F[I][J] = max{f[i-1][k]+p[i], when f[i-1][k]!=-1 && state k->j feasible}

If J equals S[i], then f[i][j] plus p[i].

Method 1 Code: Degree of Complexity O (n*k*k)

/**========================================== * is a solution for ACM/ICPC problem * * @source: uva-672 Gangster S * @type: DP * @author: Shuangde * @blog: blog.csdn.net/shuangde800 * @email: zengshuangde@gmail.com *=================== ========================*/#include <iostream> #include <cstdio> #include <algorithm> #include <
    
vector> #include <queue> #include <cmath> #include <cstring> using namespace std;
typedef long long Int64;
const int INF = 0X3F3F3F3F;
    
Const double PI = ACOs (-1.0);
    
const int MAXN = 110;
    
int n, K, T; struct node{int T, p, s; bool operator < (const node& RHS) const{return T < RHS.T;}
    
ARR[MAXN];
    
int F[MAXN][MAXN];
    
int main () {int ncase;
    
scanf ("%d", &ncase);
arr[0].t = ARR[0].P = Arr[0].s = 0;
    
while (ncase--) {scanf ("%d%d%d", &n, &k, &t);
    
for (int i=1; i<=n; ++i) scanf ("%d", &arr[i].t); for (int i=1; i<=n; ++i) scanf ("%d", &amP;ARR[I].P);
    
for (int i=1; i<=n; ++i) scanf ("%d", &ARR[I].S);
    
Sort (arr+1, arr+1+n);
Memset (F,-1, sizeof (f));
    
F[0][0] = 0;
int ans = 0; for (int i=1; i<=n; ++i) {for (int j=0; j<=k; ++j) {for (int k=0; k<=k; ++k) if (f[i-1][k]!=-1 && abs (K-J) ; = arr[i].t-arr[i-1].t) {if (J==arr[i].s && j) {F[i][j] = max (f[i][j], F[I-1][K]+ARR[I].P);
else{F[i][j] = max (F[i][j], f[i-1][k]); ans = max (ans, f[i][j));
} printf ("%d\n", ans);
if (ncase) puts ("");}
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.