uva10131,

# Question 1: Is Bigger Smarter?

## The Problem

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.
The input will consist of data for a bunch of elephants, one elephant per line, terminated by the end-of-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.
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 nintegers are aa,..., a[n] then it must be the case that
```   W[a] < W[a] < ... < W[a[n]]
```
and
```   S[a] > S[a] > ... > 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```

Solution:
Using dynamic programming. dp[i] = max{dp[j] | 0<=j < i};
Using last[i] to keep the parent of ith element;

## Source Code:

//Sun Apr 10 00:26:15 CDT 2011
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

class Elephant {
public:
int ID;
int weight;
int IQ;
Elephant(int id, int w, int iq) {
this->ID = id;
this->weight = w;
this->IQ = iq;
}
static bool cmp(const Elephant &A, const Elephant &B) {
if (A.weight < B.weight)
return true;
if (A.weight == B.weight && A.IQ > B.IQ)
return true;
return false;
}
};

vector<int> LongestIncreasingSubsequence(vector<Elephant> v) {
int N = v.size();
vector<int> dp;
vector<int> last;
for (int i = 0; i < N; i++) {
dp.push_back(1);
last.push_back(i);
}
int mmax = 0;
int index = -1;
for (int i = 0; i < N; i++) {
for (int j = i - 1; j >= 0; j--) {
if (v[j].weight < v[i].weight && v[j].IQ > v[i].IQ) {
if (dp[i] <= dp[j]) {
dp[i] = dp[j] + 1;
last[i] = j;
if (dp[i] > mmax) {
mmax = dp[i];
index = i;
}
}
}
}
}
//  cout << mmax << "," << index << endl;
//  for (int i = 0; i < N; i++)
//      cout << v[i].ID << ":" << dp[i] << ", " << last[i] << endl;
vector<int> ret;
ret.push_back(v[index].ID);
do {
index = last[index];
ret.push_back(v[index].ID);
} while (index != last[index]);
reverse(ret.begin(), ret.end());
return ret;
}

void print(vector<int> v) {
int N = v.size();
cout << N << endl;
for (int i = 0; i < N; i++)
cout << v[i] << endl;
}

int main(int argc, char* argv[]) {
//freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
vector<Elephant> v;
int count = 0;
int w, iq;
while (cin >> w >> iq) {
v.push_back(Elephant(++count, w, iq));
}
sort(v.begin(), v.end(), Elephant::cmp);
//  for (int i = 0; i < v.size(); i++)
//      cout << v[i].ID << ": " << v[i].weight << ", " << v[i].IQ << endl;
print(LongestIncreasingSubsequence(v));
//fclose(stdin);
//fclose(stdout);
return 0;
}