UVa 10131 Is Bigger Smarter?
来源:互联网 发布:淘宝许可证怎么办 编辑:程序博客网 时间:2024/06/11 17:12
http://acm.tju.edu.cn/toj/showp.php?pid=1216
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 13006000 2100500 20001000 40001100 30006000 20008000 14006000 12002000 1900Sample Output
44597
Note: Special judge problem, you may get "Wrong Answer" when output in wrong format.
Source: Waterloo Local Contest Oct. 7, 1997
注:最长上升子序列
//c++
#include<stdio.h>#define N 1005FILE *f1,*f2;typedef struct node{ long w,s,num;}node;node a[N];long f[N],b[N],q[N],n;int bigger(node x,node y){ if (x.w!=y.w) return (x.w>y.w); else return (x.s<y.s);}void sort(long left,long right){ long i,j; node x,t; i=left;j=right; x=a[(i+j)/2]; while (i<=j) { while (bigger(x,a[i])) i++; while (bigger(a[j],x)) j--; if (i<=j) { t=a[i];a[i]=a[j];a[j]=t; i++;j--; } } if (left<j) sort(left,j); if (i<right) sort(i,right);}int main(){ long i,j,k,max; /*f1=fopen("elephant.in","r"); f2=fopen("elephant.out","w");*/ i=1; while (scanf("%ld%ld",&a[i].w,&a[i].s)==2) a[i].num=i++; n=i-1; sort(1,n); for (i=1;i<=n;i++) {f[i]=1;b[i]=i;} for (i=1;i<=n;i++) for (j=1;j<i;j++) if (a[j].w<a[i].w&&a[j].s>a[i].s&&f[j]+1>f[i]) { f[i]=f[j]+1; b[i]=j; } max=0; for (i=1;i<=n;i++) if (f[i]>max) {max=f[i];k=i;} printf("%ld/n",max); q[1]=a[k].num; i=1; while (b[k]!=k) {q[++i]=a[b[k]].num; k=b[k];} for (i=max;i>=1;i--) printf("%ld/n",q[i]); /*fclose(f1); fclose(f2); */ return 0;}
// Pascal code
program smarter;type ele=record w,s,num:longint; end;var n:longint; e:array[1..1001]of ele; f,path:array[1..1001]of longint;function bigger(x,y:ele):boolean; begin if x.w<y.w then exit(false); if x.w>y.w then exit(true); if x.s<y.s then exit(true); bigger:=false; end;procedure sort(left,right:longint); var i,j:longint; x,t:ele; begin i:=left; j:=right; x:=e[(i+j)div 2]; while (i<=j) do begin while bigger(x,e[i]) do inc(i); while bigger(e[j],x) do dec(j); if i<=j then begin t:=e[i]; e[i]:=e[j]; e[j]:=t; inc(i); dec(j); end; end; if left<j then sort(left,j); if i<right then sort(i,right); end;procedure solve; var i,j,max,p:longint; ans:array[1..1000]of longint; begin for i:=1 to n do f[i]:=1; max:=0; for i:=2 to n do begin for j:=1 to i-1 do if (e[j].w<e[i].w)and(e[j].s>e[i].s)and(f[j]+1>f[i]) then begin f[i]:=f[j]+1; path[i]:=j; end; if f[i]>max then begin p:=i; max:=f[i]; end; end; n:=0; i:=p; while (f[i]>1) do begin inc(n); ans[n]:=e[i].num; i:=path[i]; end; inc(n); ans[n]:=e[i].num; writeln(n); for i:=n downto 1 do writeln(ans[i]); end;begin n:=0; while not eof do begin inc(n); readln(e[n].w, e[n].s); e[n].num:=n; end; sort(1,n); solve; end.
- UVa 10131 Is Bigger Smarter?
- UVa 10131 Is Bigger Smarter?
- UVA 10131 - Is Bigger Smarter
- uva 10131 Is Bigger Smarter?
- uva:10131 - Is Bigger Smarter?
- uva 10131 Is Bigger Smarter?
- UVA 10131 Is Bigger Smarter?
- UVA 10131 Is Bigger Smarter?
- UVa 10131 - Is Bigger Smarter?
- UVa 10131 - Is Bigger Smarter?
- Uva - 10131 - Is Bigger Smarter?
- UVa 10131 - Is Bigger Smarter?
- UVa 10131 - Is Bigger Smarter
- UVa 10131: Is Bigger Smarter?
- UVa:10131 Is Bigger Smarter?
- UVA - 10131 Is Bigger Smarter?
- UVa 10131 - Is Bigger Smarter?
- UVa 10131 - Is Bigger Smarter?
- HibernateTemplate中HibernateCallback的事务
- VC播放声音函数PlaySound和sndPlaySound的用法
- 在ubuntuserver上搭建tftp服务器
- 转:DLL注入大法—之CreateRemoteThread法
- 按钮盒 Button Boxes@GTK+ 2.0 中文教程连载
- UVa 10131 Is Bigger Smarter?
- C#代码与javaScript函数的相互调用
- 转:DLL注入大法—之HOOK注入
- 61条面向对象设计的经验原则
- 工具栏 Toolbar@GTK+ 2.0 中文教程连载
- 今天开博
- 2. 反转单词的两种解决方案
- 转:DLL注入大法—之利用特洛伊DLL进行注入
- 电容yingxuexuan笔记