10131 - Is Bigger Smarter?
Time limit: 3.000 seconds
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 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
题目大意:
输入多行数据,每行数据包括两个正整数W[i]和S[i],范围为[1,10000],分别代表一个大象的体重和IQ,输入至文件末尾,最多不超过1000个大象。从中选取n个大象构成一个序列,使得W[i]递增且S[i]递减(不可相等)。求满足条件的最长大象序列长度为多少,并输出任意一组大象编号的序列(编号按输入顺序)。
解题思路:
首先将所有大象按体重W[i]升序排列,这样该问题就转化为了最长上升子序列LIS(Longest Increasing Subsequence)问题。
定义状态d(i),代表前i个大象中以大象i作为序列结尾的最长上升子序列的长度。
状态转移方程:d(i) = max{ d(k)+1 | 1<=k<i, W[k]<W[i], S[k]>S[i] }
计算出所有状态后,再逆序枚举状态数组d[],加条件判断,打印出序列。
#include <cstdio>
#include <algorithm>
#define max(a,b) (a>b?a:b)
using std::sort;
const int N=1010;
int d[N];//状态
int n=1;
struct _ele{
int i;//编号
int w;//weight
int s;//IQ
bool operator<(const _ele A)const{
return w<A.w;
}
}ele[N];
int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d%d",&ele[n].w,&ele[n].s)) ele[n].i=n,++n;
sort(ele+1,ele+n);
//for(int i=1;i<n;++i) printf("%d: %d %d\n",i,ele[i].w,ele[i].s);
for(int i=1;i<n;++i) d[i]=1;//init
for(int j=2;j<n;++j){//以j结尾
for(int i=1;i<j;++i){//枚举前j-1个状态
if(ele[i].w<ele[j].w && ele[i].s>ele[j].s){
d[j]=max(d[i]+1, d[j]);
}
}
}
//for(int i=1;i<n;++i) printf("%d ",d[i]);printf("\n");
int max_v=-1,max_i;
for(int i=1;i<n;++i) if(max_v<d[i]){
max_v=d[i],max_i=i;
}
printf("%d\n",max_v);
int res[N],i_r=0;
int pre_s=-1,pre_w=10010,pre_d=max_v+1;
for(int i=max_i;i>0;--i){
if(ele[i].s>pre_s && ele[i].w<pre_w && d[i]==pre_d-1){
pre_s=ele[i].s;
pre_w=ele[i].w;
pre_d-=1;
res[i_r++]=i;
}
}
//for(int i=i_r-1;i>=0;--i) printf("%d\n",res[i]);
for(int i=i_r-1;i>=0;--i) printf("%d\n",ele[res[i]].i);
return 0;
}