UVA 10131 Is Bigger Smarter?(DP最长上升子序列)

Description

UVA 10131  Is Bigger Smarter?(DP最长上升子序列)
 

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 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
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn=;
int d[maxn],pre[maxn],n; struct node
{
int x,y,id;
}p[maxn]; bool mycomp(node a,node b)
{
if(a.x!=b.x) return a.x<b.x;
return a.y>b.y;
} void printf_ans(int n,int i)
{
if(n==) return ;
int u=pre[i];
printf_ans(n-,u);
printf("%d\n",p[i].id);
} bool judge(int j,int i)
{
if(p[j].x<p[i].x&&p[j].y>p[i].y)
return true;
return false;
} int main()
{
int i,j;n=;
while(~scanf("%d%d",&p[n].x,&p[n].y))
{
p[n].id=n;n++;
}
n--;
sort(p+,p+n+,mycomp);
memset(pre,-,sizeof(pre));
memset(d,,sizeof(d));
p[].x=-;p[].y=;
for(i=;i<=n;i++)
{
for(j=;j<i;j++)
{
if(judge(j,i) && d[i]<d[j]+)
{
d[i]=d[j]+;pre[i]=j;
}
}
}
int ansm,ansi;
for(i=;i<=n;i++)
{
if(ansm<d[i])
{
ansm=d[i];ansi=i;
}
}
printf("%d\n",ansm);
printf_ans(ansm,ansi);
return ;
}
上一篇:leetcode-006 detect cycle


下一篇:c++ 如何获取系统时间 - zjnig711的信息仓库 - 博客频道 - CSDN.NET