poj 1887 Testing the CATCHER

//我都不敢相信,这是World Finals 1994的第二题,就是简单的LIS做法,只不过需要注意的地方确实很多,需要很细心, 
//而且需要有耐性去读懂题目!这题用二分+DP,0MS过了,数据还是比较弱的了!
//求解LIS的两种方法都写了,第一种的效率明显要比第二种的效率要高,第一种时间复杂度为O(n^2),第二种时间复杂度为O(nlgn)! 
#include <iostream>
#include <memory.h> 
using namespace std;

int height[100000], tmp[100000]; 

int main()
{
    int i, n = 0, temp, tc = 0, ans = 0, left, right, mid;
    bool flag = false; 
    while (cin >> temp){
          if (temp == -1 && flag){
              break; 
          }
          else if (temp == -1 && !flag){
               tc++; 
               tmp[0] = 32768;
               for (i = 0; i < n; i++){
                   if (height[i] < tmp[ans]){
                       tmp[++ans] = height[i]; 
                   }
                   else{
                        left = 1, right = ans;
                        while (left <= right){
                              mid = (left + right) / 2;
                              if (tmp[mid] > height[i]){
                                  left = mid + 1; 
                              }
                              else{
                                   right = mid - 1; 
                              } 
                        } 
                        tmp[left] = height[i]; 
                   } 
               } 
               cout << "Test #" << tc << ":" << endl;
               cout << "  maximum possible interceptions: " << ans << endl << endl; 
               ans = n = 0; 
               memset(tmp, 0, sizeof(tmp)); 
               flag = true; 
          } 
          else{
               height[n] = temp;
               n++; 
               flag = false; 
          } 
    } 
    
    system("pause"); 
} 


/* 
#include <iostream>
#include <memory.h>
using namespace std;

int height[100000], dp[100000];

int main()
{
    int i, j, n = 0, temp, tc = 0, ans = 0;
    bool flag = false; 
    while (cin >> temp){
          if (temp == -1 && flag){
              break; 
          }
          else if (temp == -1 && !flag){
               tc++; 
               for (i = 0; i < 100000; i++)
                    dp[i] = 1;
               for (i = 1; i < n; i++){
                   for (j = 0; j < i; j++){
                       if (height[i] < height[j] && dp[i] <= dp[j]){
                           dp[i] = dp[j] + 1; 
                       } 
                   } 
               } 
               for (i = 0; i < n; i++){
                   if (dp[i] > ans)
                       ans = dp[i]; 
               } 
               cout << "Test #" << tc << ":" << endl;
               cout << "  maximum possible interceptions: " << ans << endl << endl; 
               ans = n = 0; 
               flag = true; 
          } 
          else{
               height[n] = temp;
               n++; 
               flag = false; 
          } 
    } 
    
    system("pause"); 
} 
*/ 

你可能感兴趣的:(poj 1887 Testing the CATCHER)