Pages

Showing posts with label LIS. Show all posts
Showing posts with label LIS. Show all posts

Saturday, 13 July 2013

UVA - 231 - Testing the CATCHER

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        int i=1;
        while(true){
            int n=Integer.parseInt(br.readLine());
            if(n<0)
                break;
            if(i>1)
                sb.append("\n");
            Vector<Integer> vec=new Vector<Integer>();
            vec.add(n);
            while(true){
                n=Integer.parseInt(br.readLine());
                if(n<0)
                    break;
                vec.add(n);
            }
            sb.append("Test #").append(i).append(":\n");
            sb.append("  maximum possible interceptions: ").append(LDS(vec)).append("\n");
            i++;
        }
        System.out.print(sb);
    }

    static int LDS(Vector<Integer> vec){
        int[]arr=new int[vec.size()];
        arr[0]=1;
        int maxL=0;
        for(int i=1;i<arr.length;i++){
          int maX=0;
          for(int j=0;j<i;j++){
            if(vec.get(j)>vec.get(i) &&arr[j]>maX){
              maX=arr[j];
            }
          }
          arr[i]=maX+1;
          if(arr[i]>maxL){
              maxL=arr[i];
          }
        }
        return maxL;
    }
}

Friday, 11 January 2013

UVA - 481 - What Goes Up

#include <vector>
#include <cstdio>

using namespace std;

void LIS(vector<int> &input, vector<int> &lis){
    vector<int> temp(input.size());
    lis.push_back(0);
    int s, e;
    for (size_t i = 1; i < input.size(); i++){
        if (input[lis.back()] < input[i]){
            temp[i] = lis.back();
            lis.push_back(i);
            continue;
        }
        s = 0;
        e = lis.size()-1;
        while (s < e){
            int m = (s + e) / 2;
            if (input[lis[m]] < input[i])
                s=m+1;
            else
                e=m;
        }
        if (input[i] < input[lis[s]]){
            if (s > 0)
                temp[i] = lis[s-1];
            lis[s] = i;
        }
    }
    for (s = lis.size(), e = lis.back(); s--; e = temp[e])
        temp[s] = e;
}


int main()
{
    int x;
    vector<int> seq;
    while(scanf("%d",&x)==1){
        if(x==22)
            break;
        seq.push_back(x);
    }
    vector<int> lis;
    LIS(seq, lis);
    printf("%d\n-\n", lis.size());
    for (unsigned int i = 0; i < lis.size(); i++)
        printf("%d\n", seq[lis[i]]);
    return 0;
}