jzoj P1667【coci2011/2012 1】PLES

来源:互联网 发布:网络播放器哪个好用 编辑:程序博客网 时间:2024/06/05 23:51

t题目大意:
在舞会上有N个男孩和N个女孩,每个人都量过了自己的身高。每个男孩只跟女孩跳舞,并且女孩也只跟男孩跳舞。每个人最多只有一个舞伴。男孩或者想和比自己高的女孩跳舞,或者想和比自己低的女孩跳舞,同样的,女孩也是或者想和比自己高的男孩跳舞,或者想和自己低的男孩跳舞。你能决定最多有多少对能在一起跳舞么?

1<=N<=100000 N个数的绝对值在1500到2500的整数
如果是正整数的话,表示这个女孩喜欢和比她高的男孩跳舞,如果是负整数的话,表示这个女孩喜欢和比她低的男孩跳舞,男的同理

题解:
分别把男生女生想要跟低的跳的跟想要跟高的跳的分类讨论,就是用排序随便搞搞。
因为只有2种情况:
想要跟矮的女生跳的高的男生
想要跟矮的男生跳的高的女生
然后排完序后,从最大开始比较这2种情况,贪心随便搞搞。

var     a,b,c,k:array [0..100001] of longint;     d:array [0..100001,1..2] of longint;     l,r,mid,n,i,j,x,a1,b1,c1,k1,ans:longint;procedure qsort(l,r:longint);var     i,j,mid:longint;begin     if l>=r then exit;     i:=l; j:=r;     mid:=d[(l+r) div 2,1];     repeat           while d[i,1]<mid do inc(i);           while d[j,1]>mid do dec(j);           if i<=j then           begin                d[0,1]:=d[i,1];d[i,1]:=d[j,1];d[j,1]:=d[0,1];                d[0,2]:=d[i,2];d[i,2]:=d[j,2];d[j,2]:=d[0,2];                inc(i); dec(j);           end;     until i>j;     qsort(i,r);     qsort(l,j);end;begin     readln(n);     for i:=1 to n do     begin          read(x);          if x<0 then d[i,2]:=-1                 else d[i,2]:=1;          d[i,1]:=abs(x);     end;     readln;     qsort(1,n);     for i:=1 to n do       if d[i,2]=-1 then begin inc(a1); a[a1]:=d[i,1] end                    else begin inc(b1); b[b1]:=d[i,1] end;     for i:=1 to n do     begin          read(x);          if x<0 then d[i,2]:=-1                 else d[i,2]:=1;          d[i,1]:=abs(x);     end;     qsort(1,n);     for i:=1 to n do       if d[i,2]=-1 then begin inc(c1); c[c1]:=d[i,1] end                    else begin inc(k1); k[k1]:=d[i,1] end;     i:=c1; j:=b1;     while (i>=1) and (j>=1) do     begin          if c[i]>b[j]             then begin inc(ans); dec(i); dec(j); end             else if c[i]<=b[j] then dec(j);     end;     writeln(ans);     i:=a1; j:=k1;     while (i>=1) and (j>=1) do     begin          if a[i]>k[j]             then begin inc(ans); dec(i); dec(j); end             else if a[i]<=k[j] then dec(j);     end;     writeln(ans);end.
原创粉丝点击