Algorithm


problem Link ; https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=1072

Some people think that the bigger an elephant is, the smarter it is. To disprove this, you want to take the data on a collection of elephants and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the IQ’s are decreasing. Input The input will consist of data for a bunch of elephants, one elephant per line, terminated by the endof-file. The data for a particular elephant will consist of a pair of integers: the first representing its size in kilograms and the second representing its IQ in hundredths of IQ points. Both integers are between 1 and 10000. The data will contain information for at most 1000 elephants. Two elephants may have the same weight, the same IQ, or even the same weight and IQ. Output Say that the numbers on the i-th data line are W[i] and S[i]. Your program should output a sequence of lines of data; the first line should contain a number n; the remaining n lines should each contain a single positive integer (each one representing an elephant). If these n integers are a[1], a[2],..., a[n] then it must be the case that W[a[1]] < W[a[2]] < ... < W[a[n]] and S[a[1]] > S[a[2]] > ... > S[a[n]] In order for the answer to be correct, n should be as large as possible. All inequalities are strict: weights must be strictly increasing, and IQs must be strictly decreasing. There may be many correct outputs for a given input, your program only needs to find one.

Sample Input 6008 1300 6000 2100 500 2000 1000 4000 1100 3000 6000 2000 8000 1400 6000 1200 2000 1900

Sample Output 4 4 5 9 7

Code Examples

#1 Code Example with C Programming

Code - C Programming

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

int main()
{
    int w,s;
    int i=1;
    vector < tuple<int,int,int>> elephants;
    while(scanf("%d %d",&w,&s)!=EOF){
        elephants.push_back(make_tuple(w,s,i++));
    }
    auto cmp = [](const tuple < int,int,int>& a,const tuple<int,int,int>& b){
        if(get<0>(a) != get<0>(b)) return get<0>(a) < get<0>(b);
        return get<1>(a) > get<1>(b);
    };
    sort(elephants.begin(),elephants.end(),cmp);

    vector<int> dp;
    vector<int> parent;
    int bestIdx = 0, best = 0;
    for(int i=0;i < elephants.size();i++){
        int prev = -1, longest=0;
        for(int j=0;j < i;j++){
            if(get<0>(elephants[i]) > get<0>(elephants[j]) &&
               get<1>(elephants[i]) < get<1>(elephants[j]) &&
               dp[j] > longest){
                longest = dp[j];
                prev = j;
            }
        }
        parent.push_back(prev);
        dp.push_back(longest+1);
        if(dp.back() > best){
            best = dp.back();
            bestIdx = i;
        }
    }
    stack < int> res;
    while(bestIdx != -1){
        res.push(get<2>(elephants[bestIdx]));
        bestIdx = parent[bestIdx];
    }
    printf("%d\n",best);
    while(!res.empty()) printf("%d\n",res.top()), res.pop();
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900

Output

x
+
cmd
4
4
5
9
7
Advertisements

Demonstration


UVA Online Judge solution - 10131-Is Bigger Smarter? - UVA Online Judge solution in C,C++,Java

Previous
UVA Online Judge solution - 10130 - SuperSale - UVA Online Judge solution in C,C++,Java
Next
UVA Online Judge solution - 10132 - File Fragmentation - UVA Online Judge solution in C,C++,Java