UVa 10131 Is Bigger Smarter? (LDS+数据结构排序)

Is Bigger Smarter?

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Submit Status

Description

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

题意:大象真的越大越聪明吗,找出体重递增IQ递减的最长大象序列。

分析:很明显的最长递降子序列,先把体重排序,体重一样的按iq递减排,这样就能出来了。

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 //antiew[i]为编号i个大象的体重排名; ew是体重第i名的大象的编号;w,s是大象的体重,智商;
 5 int w[1005],s[1005],n=0,ew[1005],antiew[1005],loest[1005],p[1005];
 6 //int f[1005][1005];
 7 int cmp(const void* x,const void* y)
 8 {
 9     if(  w[*(int*)x] != w[*(int*)y]  )
10         return w[*(int*)x] - w[*(int*)y];
11     else
12         return s[*(int*)y] - s[*(int*)x] ;
13 }
14
15 void print(int i)
16 {
17     if(i == -1) return;
18     print(p[i]);
19     printf("%d\n",ew[i] + 1);
20     //printf("%d : %d   %d\n",ew[i] + 1,w[ew[i]],s[ew[i]]);
21 }
22
23 int main()
24 {
25     #ifdef LOCAL
26     freopen("..\\data.in","r",stdin);
27     #endif // LOCAL
28     //读入大象的体重w  智商s  共n头大象
29     while(scanf("%d%d",w+n,s+n) == 2)
30         n++;
31     //ew是大象体重增加的
32     for(int i=0;i<n;i++)
33         ew[i] = i;
34     qsort(ew,n,sizeof(int),cmp);
35     for(int i=0;i<n;i++)
36         antiew[ew[i]] = i;
37     memset(p,-1,sizeof(p));
38     memset(loest,0,sizeof(loest));
39
40     loest[0] = 1;
41     for(int i=1;i<n;i++)
42     {
43         //maxl是前i个的大象IQ的严格下降降子序列长度的最大值
44         int maxl=0;
45         for(int j=i-1;j>=0;j--) if( s[ew[i]] < s[ew[j]] && w[ew[i]] > w[ew[j]] && maxl < loest[j] )
46         {
47             maxl = loest[j];
48             p[i] = j;
49         }
50         if(p[i] == -1)
51             loest[i] = 1;
52         else
53             loest[i] = maxl + 1;
54     }
55
56     int maxip=0;
57     for(int i=0;i<n;i++) if(loest[maxip] < loest[i])
58         maxip = i;
59     printf("%d\n",loest[maxip]);
60
61     print(maxip);
62     /*
63     for(int i=0;i<n;i++)
64         printf("编号:%4d  体重:%4d   智商:%4d  体重排名:%4d\n",i + 1,w[i],s[i],antiew[i]+1);
65     for(int i=0;i<n;i++)
66         printf("体重排名:%4d  体重:%4d   智商:%4d  编号:%4d\n",i+1,w[ew[i]],s[ew[i]],ew[i]+1);
67     */
68     return 0;
69 }
时间: 2024-10-13 15:04:18

UVa 10131 Is Bigger Smarter? (LDS+数据结构排序)的相关文章

UVA 10131 Is Bigger Smarter? 【严格单调递增子序列】

题目:UVA 10131 Is Bigger Smarter 题意:给出大象的身高和体重,求身高递增且体重递减的最长序列,都是严格的,并打印序列. 分析:就是先对身高按自增排序,然后求一个单调递减子序列,严格单调的,所以加一句判断,然后打印序列,用一个数组保存就好了 开始想的是先预处理掉重复的,提交wa了,这样不行,因为你不知道体重是最高的还是最低的,可能开始留高的好,后面低的比较好.所以..... AC代码: #include<iostream> #include<cstdio>

uva 10131 Is Bigger Smarter? (DAG)

uva 10131 Is Bigger Smarter? 题目大意:当一只大象的体重大于另一只的体重,且智商小于另一只的智商,该大象便可以"嵌套"另一只大象.问,最长的嵌套方式.(答案不唯一) 解题思路:DAG. #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> using namespace std; struct ELE{ int w

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

Description 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 in

Uva 10131 Is Bigger Smarter? (LIS,打印路径)

option=com_onlinejudge&Itemid=8&page=show_problem&problem=1072">链接:UVa 10131 题意:给定若干大象的体重及智商值.求满足大象体重严格递增,智商严格递减的序列的最大个数. 并打印随意一组取得最大值的序列的大象编号 分析:这个是LIS的应用,仅仅只是推断条件有两个,能够先对大象的体重排序,可是要打印路径. 那就必须得回溯求路径.能够直接逆序循环求,当然递归也是一个好的选择 #include<

UVA 10131 Is Bigger Smarter?(DP)

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 t

UVa 10131 - Is Bigger Smarter?

题目:有人认为大象的体重和智力有一定的正相关性,现在给你一些数据,找到一个最长的反例序列. 分析:dp,LIS,醉倒上升子序列.对W排序求出S的最大下降子序列即可,存储路径前驱,dfs输出. 说明:先读到EOF再处理. #include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath

uva 10131 Is Bigger Smarter? dag 最长路 加路径还原

#include <cstdio> #include <iostream> #include <algorithm> #include <queue> #include <stack> #include <climits> #include <cstring> #include <cmath> #include <map> #include <set> #define INF 10000

UVA - 10131Is Bigger Smarter?(DAG上的DP)

题目:UVA - 10131Is Bigger Smarter? (DAG) 题目大意:给出一群大象的体重和IQ.要求挑选最多的大象,组成一个序列.严格的体重递增,IQ递减的序列.输出最多的大象数目和这些大象的序列(当中一种就能够). 解题思路:DAG上的DP.和之前的一篇相似.uva437 - The Tower of Babylon(DAG上的DP).就是将每两仅仅大象满足上面的序列要求的形成一条有向边. 之后就是DAG上的DP.然后再路径输出. 代码: #include <cstdio>

UVA - 10905 - Children&#39;s Game (简单排序)

UVA - 10905 Children's Game Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description 4thIIUCInter-University Programming Contest, 2005 A Children's Game Input: standard input Output: standard output Problems