动态规划 LIS最长上升子序列

POJ 2533 Longest Ordered Subsequence

Language: Default
Longest Ordered Subsequence
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 41599   Accepted: 18312

Description

A numeric sequence of  ai is ordered if  a1 <  a2 < ... <  aN. Let the subsequence of the given numeric sequence ( a1a2, ...,  aN) be any sequence ( ai1ai2, ...,  aiK), where 1 <=  i1 <  i2 < ... <  iK <=  N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, e. g., (1, 7), (3, 4, 8) and many others. All longest ordered subsequences are of length 4, e. g., (1, 3, 5, 8).

Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.

Input

The first line of input file contains the length of sequence N. The second line contains the elements of sequence - N integers in the range from 0 to 10000 each, separated by spaces. 1 <= N <= 1000

Output

Output file must contain a single integer - the length of the longest ordered subsequence of the given sequence.

Sample Input

7
1 7 3 5 9 4 8

Sample Output

4

Source

Northeastern Europe 2002, Far-Eastern Subregion

[Submit]   [Go Back]   [Status]   [Discuss]



#include 
#include 
using namespace std;
const int INF = 0x3f3f3f3f;

int main() {
    int n;
    int dp[1010];

    while (~scanf("%d", &n)) {
        fill(dp, dp + n, INF);
        int tmp;
        for (int i = 0; i < n; ++i) {
            scanf("%d", &tmp);
            *lower_bound(dp, dp + n, tmp) = tmp;
        }
        printf("%ld\n", lower_bound(dp, dp + n, INF) - dp);
    }

    return 0;
}

POJ 1631Bridging signals


Language: Default
Bridging signals
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 12264   Accepted: 6694

Description

'Oh no, they've done it again', cries the chief designer at the Waferland chip factory. Once more the routing designers have screwed up completely, making the signals on the chip connecting the ports of two functional blocks cross each other all over the place. At this late stage of the process, it is too expensive to redo the routing. Instead, the engineers have to bridge the signals, using the third dimension, so that no two signals cross. However, bridging is a complicated operation, and thus it is desirable to bridge as few signals as possible. The call for a computer program that finds the maximum number of signals which may be connected on the silicon surface without crossing each other, is imminent. Bearing in mind that there may be thousands of signal ports at the boundary of a functional block, the problem asks quite a lot of the programmer. Are you up to the task? 
动态规划 LIS最长上升子序列_第1张图片

A typical situation is schematically depicted in figure 1. The ports of the two functional blocks are numbered from 1 to p, from top to bottom. The signal mapping is described by a permutation of the numbers 1 to p in the form of a list of p unique numbers in the range 1 to p, in which the i:th number specifies which port on the right side should be connected to the i:th port on the left side.Two signals cross if and only if the straight lines connecting the two ports of each pair do.

Input

On the first line of the input, there is a single positive integer n, telling the number of test scenarios to follow. Each test scenario begins with a line containing a single positive integer p < 40000, the number of ports on the two functional blocks. Then follow p lines, describing the signal mapping:On the i:th line is the port number of the block on the right side which should be connected to the i:th port of the block on the left side.

Output

For each test scenario, output one line containing the maximum number of signals which may be routed on the silicon surface without crossing each other.

Sample Input

4
6
4
2
6
3
1
5
10
2
3
4
5
6
7
8
9
10
1
8
8
7
6
5
4
3
2
1
9
5
8
9
2
3
1
7
4
6

Sample Output

3
9
1
4

Source

Northwestern Europe 2003



#include 
#include 
using namespace std;
const int INF = 0x3f3f3f3f;

int main() {
    int n;
    int dp[40010];

    scanf("%d", &n);
    while (~scanf("%d", &n)) {
        fill(dp, dp + n, INF);
        int tmp;
        for (int i = 0; i < n; ++i) {
            scanf("%d", &tmp);
            *lower_bound(dp, dp + n, tmp) = tmp;
        }
        printf("%ld\n", lower_bound(dp, dp + n, INF) - dp);
    }

    return 0;
}

POJ 1887 Testing the CATCHER 

最长下降子序列,将其倒过来做即可.

Language: Default
Testing the CATCHER
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 16507   Accepted: 6077

Description

A military contractor for the Department of Defense has just completed a series of preliminary tests for a new defensive missile called the CATCHER which is capable of intercepting multiple incoming offensive missiles. The CATCHER is supposed to be a remarkable defensive missile. It can move forward, laterally, and downward at very fast speeds, and it can intercept an offensive missile without being damaged. But it does have one major flaw. Although it can be fired to reach any initial elevation, it has no power to move higher than the last missile that it has intercepted. 

The tests which the contractor completed were computer simulations of battlefield and hostile attack conditions. Since they were only preliminary, the simulations tested only the CATCHER's vertical movement capability. In each simulation, the CATCHER was fired at a sequence of offensive missiles which were incoming at fixed time intervals. The only information available to the CATCHER for each incoming missile was its height at the point it could be intercepted and where it appeared in the sequence of missiles. Each incoming missile for a test run is represented in the sequence only once. 

The result of each test is reported as the sequence of incoming missiles and the total number of those missiles that are intercepted by the CATCHER in that test. 

The General Accounting Office wants to be sure that the simulation test results submitted by the military contractor are attainable, given the constraints of the CATCHER. You must write a program that takes input data representing the pattern of incoming missiles for several different tests and outputs the maximum numbers of missiles that the CATCHER can intercept for those tests. For any incoming missile in a test, the CATCHER is able to intercept it if and only if it satisfies one of these two conditions: 

The incoming missile is the first missile to be intercepted in this test. 
-or- 
The missile was fired after the last missile that was intercepted and it is not higher than the last missile which was intercepted.

Input

The input data for any test consists of a sequence of one or more non-negative integers, all of which are less than or equal to 32,767, representing the heights of the incoming missiles (the test pattern). The last number in each sequence is -1, which signifies the end of data for that particular test and is not considered to represent a missile height. The end of data for the entire input is the number -1 as the first value in a test; it is not considered to be a separate test.

Output

Output for each test consists of a test number (Test #1, Test #2, etc.) and the maximum number of incoming missiles that the CATCHER could possibly intercept for the test. That maximum number appears after an identifying message. There must be at least one blank line between output for successive data sets. 

Note: The number of missiles for any given test is not limited. If your solution is based on an inefficient algorithm, it may not execute in the allotted time. 

Sample Input

389
207
155
300
299
170
158
65
-1
23
34
21
-1
-1

Sample Output

Test #1:
  maximum possible interceptions: 6

Test #2:
  maximum possible interceptions: 2

Source

World Finals 1994

#include 
#include 
#include 
using namespace std;
const int INF = 0x3f3f3f3f;

int main() {
    int n;
    int dp[10010];

    while (scanf("%d", &n), ~n) {
        stack s;
        do {
            s.push(n);
            scanf("%d", &n);
        }while (~n);

        fill(dp, dp + 10010, INF);
        while (!s.empty()) {
            *lower_bound(dp, dp + 10010, s.top()) = s.top();
            s.pop();
        }
        static int cnt = 0;
        printf("Test #%d:\n  maximum possible interceptions:", ++cnt); 
        printf(" %ld\n\n", lower_bound(dp, dp + 10010, INF) - dp);
    }

    return 0;
}


你可能感兴趣的:(动态规划)