UVa 10131 Is Bigger Smarter?

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 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900
Sample Output
4
4
5
9
7

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 1005
FILE *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.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值