A. Laptops

题意:给两个数列,问是否存在i,j,是的a[i]<a[j] 而 b[i]>b[j]

题解:先把一维排序,就是问是否存在逆序对。我写了个树状数组。。。貌似从a小到大扫一遍保存遇到的b的最大值即可

代码:

 1 var s,a,b,c:array[0..150000] of longint;
 2     i,n,m,tot:longint;
 3 procedure sort(l,r:longint);
 4  var i,j,x,y:longint;
 5  begin
 6  i:=l;j:=r;x:=a[(i+j)>>1];
 7  repeat
 8   while a[i]<x do inc(i);
 9   while a[j]>x do dec(j);
10   if i<=j then
11    begin
12    y:=a[i];a[i]:=a[j];a[j]:=y;
13    y:=b[i];b[i]:=b[j];b[j]:=y;
14    inc(i);dec(j);
15    end;
16  until i>j;
17  if i<r then sort(i,r);
18  if j>l then sort(l,j);
19  end;
20 procedure init;
21  begin
22  readln(n);
23  for i:=1 to n do readln(a[i],b[i]);
24  sort(1,n);
25  tot:=0;
26  i:=0;tot:=0;
27  for i:=1 to n do
28   begin
29   if (i=1) or (a[i]<>a[i-1]) then
30    begin
31    inc(tot);c[tot]:=b[i];
32    end;
33   if b[i]>c[tot] then c[tot]:=b[i];
34   end;
35  end;
36 procedure add(x:longint);
37  begin
38  while x<=n do
39   begin
40   inc(s[x]);
41   inc(x,x and (-x));
42   end;
43  end;
44 function sum(x:longint):longint;
45  var t:longint;
46  begin
47  t:=0;
48  while x>0 do
49   begin
50   inc(t,s[x]);
51   dec(x,x and (-x));
52   end;
53  exit(t);
54  end;
55 
56 function check:boolean;
57  begin
58  fillchar(s,sizeof(s),0);
59  for i:=tot downto 1 do
60    begin
61    if sum(c[i]-1)>0 then exit(true);
62    add(c[i]);
63    end;
64  exit(false);
65  end;
66 begin
67   init;
68   if check then writeln('Happy Alex') else writeln('Poor Alex');
69 end.
View Code

相关文章: