P1020 missile interceptors O (nlogn) practice

Title Description

A country in order to defend against enemy missile attacks, to develop a missile interception system. But such a missile interceptor system has a flaw: Although it's the first rounds to reach any height, but each shell and should not be higher than before the situation got height. One day, the radar picked up incoming enemy missiles. As the system is still in beta, so only a system, and therefore may not intercept all the missiles.
Enter the missile turn flying height (height of the radar data given is \ (\ le 50000 \) is a positive integer), calculate how much the system up to intercept the missile, if you want to intercept all missiles minimum number of sets to be equipped with such missiles interception system.

Input Format

1 line, a plurality of integer (number \ (\ 100000 Le \) )

Output Format

2 lines of an integer, the first number indicates how much the system up to intercept missiles, the second number indicates if you want to intercept all missiles minimum number of sets to be equipped with such a missile interception system.

The first question is not seeking a liter longest sequence.
The second question is, according to Dilworth Theorem seeking a rise in the longest sequence

Each task we can \ (n ^ 2 \) to complete.
On this basis, we can optimize it to the first sub-problems, for example, with \ (f [i] \) represents \ (dp \) is \ (i \) the maximum height. Then we update \ (dp [x] \) when, just need to find the biggest \ (f [i] \) , then \ (dp [x] = i
+ 1 \) Why? If there are considered two of the missile subscripts a, b (both before i), if a is not greater than b, and \ (DP [a]> DP [B] \) , then relatively speaking a, b for the \ (dp [i] \) is not contributing.
Further, we found that by observing \ (F \) array of values is monotonic, so we can find the two points we need \ (f [i] \)

code show as below:

#include<bits/stdc++.h>
using namespace std;

int dp1[100010],dp2[100010],f[100010];
int a[100010];
int main(){
    int tot = 0;
    while(scanf("%d",&a[++tot]) == 1);
        f[0] = max(f[0],a[tot]);
    --tot;
    for(int i = 1; i <= tot; ++i){
        int l = 0,r = tot;
        while(l < r){
            int mid = (l + r + 1) >> 1;
            if(f[mid] >= a[i]) l = mid;
            else r = mid - 1;
        }
        dp1[i] = l + 1;
        f[dp1[i]] = max(f[dp1[i]],a[i]);
    }
    memset(f,0x3f,sizeof(f));
    for(int i = 1; i <= tot; ++i){
        int l = 0,r = tot;
        while(l < r){
            int mid = (l + r + 1) >> 1;
            if(f[mid] < a[i]) l = mid;
            else r = mid - 1;
        }
        dp2[i] = l + 1;
        f[dp2[i]] = min(f[dp2[i]],a[i]);    
    }
    int ans1 = 0,ans2 = 0;
    for(int i = 1; i <= tot; ++i){
        ans1 = max(ans1,dp1[i]);
        ans2 = max(ans2,dp2[i]);
    }
    cout << ans1 << endl << ans2 << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/FoxC/p/11351110.html