Poj 1036 Gangsters

Source: Internet
Author: User

POJ's 2nd questions. It is also a dynamic conversion. dp [I] [j] indicates the maximum value of the I-th moment and the degree to which the door is opened to j.

Of course, the status is quite good. The value of this moment must be related to the previous moment. The connection is the j of the previous moment, that is, the degree of the door of the previous moment, and there are only three possibilities for a time gate. 1: open a unit. 2: close a unit. 3: Same as before. In addition, we need to use a rolling array to do this. Otherwise, my memory may be too large. I have never tried this kind of dynamic transfer equation. Generally, we use a rolling array to save space. It's just a matter of course. The following code is provided.

[Cpp]
# Include <iostream>
# Include <algorithm>
Using namespace std;
Struct node {
Int time;
Int value;
Int silm;
};
Node gangster [110];
Int mark [30010];
Int dp [3] [2, 110];
Bool cmp (node a, node B)
{
If (a. time <B. time)
Return true;
Else return false;
}
Int maxi (int a, int B)
{
If (a> B)
Return;
Else return B;
}
Int main ()
{
Int N, K, T, I, j, w, p, mabi;
Cin> N> K> T;
For (I = 1; I <= N; I ++)
{
Cin> gangster [I]. time;
Mark [gangster [I]. time] = 1;
}
For (I = 1; I <= N; I ++)
Cin> gangster [I]. value;
For (I = 1; I <= N; I ++)
Cin> gangster [I]. silm;
Sort (gangster + 1, gangster + 1 + N, cmp );
Mabi = 0;
For (I = 0; I <= T; I ++)
For (j = 0; j <= I & j <= K; j ++)
{
W = 0;
Dp [I % 2] [j] = 0;
If (mark [I] = 1)
{
For (p = 1; p <= N; p ++)
{
If (gangster [p]. time = I & gangster [p]. silm = j)
W = w + gangster [p]. value;
}
}
If (j = 0)
{
Dp [I % 2] [j] = maxi (dp [1-i % 2] [j], dp [1-i % 2] [j + 1]);
}
Else if (j = K)
{
Dp [I % 2] [j] = maxi (dp [1-i % 2] [j], dp [1-i % 2] [J-1]);
}
Else dp [I % 2] [j] = maxi (dp [1-i % 2] [j], dp [1-i % 2] [J-1]), dp [1-i % 2] [j + 1]);
Dp [I % 2] [j] + = w;
If (dp [I % 2] [j]> mabi)
Mabi = dp [I % 2] [j];
}
Cout <mabi <endl;
Return 0;
}

# Include <iostream>
# Include <algorithm>
Using namespace std;
Struct node {
Int time;
Int value;
Int silm;
};
Node gangster [110];
Int mark [30010];
Int dp [3] [2, 110];
Bool cmp (node a, node B)
{
If (a. time <B. time)
Return true;
Else return false;
}
Int maxi (int a, int B)
{
If (a> B)
Return;
Else return B;
}
Int main ()
{
Int N, K, T, I, j, w, p, mabi;
Cin> N> K> T;
For (I = 1; I <= N; I ++)
{
Cin> gangster [I]. time;
Mark [gangster [I]. time] = 1;
}
For (I = 1; I <= N; I ++)
Cin> gangster [I]. value;
For (I = 1; I <= N; I ++)
Cin> gangster [I]. silm;
Sort (gangster + 1, gangster + 1 + N, cmp );
Mabi = 0;
For (I = 0; I <= T; I ++)
For (j = 0; j <= I & j <= K; j ++)
{
W = 0;
Dp [I % 2] [j] = 0;
If (mark [I] = 1)
{
For (p = 1; p <= N; p ++)
{
If (gangster [p]. time = I & gangster [p]. silm = j)
W = w + gangster [p]. value;
}
}
If (j = 0)
{
Dp [I % 2] [j] = maxi (dp [1-i % 2] [j], dp [1-i % 2] [j + 1]);
}
Else if (j = K)
{
Dp [I % 2] [j] = maxi (dp [1-i % 2] [j], dp [1-i % 2] [J-1]);
}
Else dp [I % 2] [j] = maxi (dp [1-i % 2] [j], dp [1-i % 2] [J-1]), dp [1-i % 2] [j + 1]);
Dp [I % 2] [j] + = w;
If (dp [I % 2] [j]> mabi)
Mabi = dp [I % 2] [j];
}
Cout <mabi <endl;
Return 0;
}
This program should be able to optimize the time complexity, which was previously done. It may be supplemented later.

 

Related Article

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.