# poj 3378 Crazy Thairs

These days, Sempr is crazed on one problem named Crazy Thair. Given N (1 ≤ N ≤ 50000) numbers, which  are no more than 109, Crazy Thair is a group of 5 numbers {i, j, k, l, m} satisfying:

1. 1 ≤ i < j < k < l < m  N

2. Ai < Aj < Ak < Al < Am

For example, in the sequence {2, 1, 3, 4, 5, 7, 6},there are four Crazy Thair groups: {1, 3, 4, 5, 6}, {2, 3, 4, 5, 6}, {1, 3, 4, 5, 7} and {2, 3, 4, 5, 7}.

Could you help Sempr to count how many Crazy Thairs in the sequence?

Input contains several test cases. Each test case begins with a line containing a number N, followed by a line containing N numbers.

Output the amount of Crazy Thairs in each sequence.

网上的解题报告有两个解法：

1：Dp+线段树+离散化+高精度

2：树状数组+Dp+离散化+高精度

靠，好复杂，又没有P语言，C语言不会翻，I give up it。然后LZH经过了N天N夜，在 big head 的叽叽咕咕下，在 pig 的乱搞下，AC啦。于是我狠狠的敲了一波标。

讲一下DP：

F[I,j]表示用a[i]结尾的长度为j的序列数目。

F[I,j]= sum(F[k,j-1]) (1<=k<I且a[i]>a[k])

因为读入的数有10^9大，而数列长度只有50000，想到离散化。可离散后，位置发生改变，排个序，在二分来查找。

type
arr=record
x,y:longint;
end;

var
dp:array [0..50001,1..5] of int64;
m,len,n,k:longint;
f:array [0..50001] of longint;
tree:array [0..50001] of arr;
sum:array [0..101] of longint;

procedure qsort(l,r:longint);
var
i,j,key,key1:longint;
temp:arr;
begin
if l>=r then exit;
i:=l; j:=r;
key:=tree[(l+r) shr 1].x;
key1:=tree[(l+r) shr 1].y;
repeat
while (tree[i].x<key) or (tree[i].x=key) and (tree[i].y<key1) do inc(i);
while (tree[j].x>key) or (tree[j].x=key) and (tree[j].y>key1) do dec(j);
if i<=j then
begin
temp:=tree[i]; tree[i]:=tree[j]; tree[j]:=temp;
inc(i);dec(j);
end;
until i>j;
qsort(l,j);
qsort(i,r);
end;

function bit(n:longint):longint;
begin
exit(n and -n);
end;

procedure jf(n:qword);
var
i:longint;
a,b:array [0..100] of longint;
begin
fillchar(a,sizeof(a),0);
fillchar(b,sizeof(b),0);
i:=-1;
while n>0 do
begin
inc(i);
a[i]:=n mod 10;
n:=n div 10;
end;
i:=-1;
while i<100 do
begin
inc(i);
b[i]:=a[i]+sum[i]+b[i];
if b[i]>=10 then
begin
inc(b[i+1]);
b[i]:=b[i] mod 10;
end;
end;
for i:=0 to 99 do
sum[i]:=b[i];
end;

function count(n,j:longint):int64;
var
ans:int64;
begin
ans:=0;
while n>0 do
begin
ans:=ans+dp[n,j];
n:=n-bit(n);
end;
exit(ans);
end;

procedure update(n,j:longint;k:int64);
begin
while n<=m do
begin
dp[n,j]:=dp[n,j]+k;
n:=n+bit(n);
end;
end;

procedure dpp(n:longint);
var
tem:int64;
i,j:longint;
begin
fillchar(dp,sizeof(dp),0);
fillchar(sum,sizeof(sum),0);
len:=1;
for i:=1 to n do
begin
tem:=count(f[i]-1,4);
jf(tem);
for j:=5 downto 2 do
begin
tem:=count(f[i]-1,j-1);
update(f[i],j,tem);
end;
update(f[i],1,1);
end;
len:=100;
while(sum[len]=0) and (len>0) do dec(len);
for i:=len downto 0 do
write(sum[i]);
writeln;
end;

procedure main;
var
i:longint;
begin
while not eof do
begin
m:=n;
for i:=1 to n-1 do
begin
tree[i].y:=i;
end;
tree[n].y:=n;
qsort(1,n);
f[tree[1].y]:=1; k:=0;
for i:=1 to n do
begin
if tree[i].x=tree[i-1].x then f[tree[i].y]:=f[tree[i-1].y] else
begin
f[tree[i].y]:=k+1;
inc(k);
end;
end;
dpp(n);
end;
end;

begin
main;
end.

#### POJ 3378 Crazy Thairs(数据集中+DP+树状数组+高精度)

2014-03-17 21:03:25

#### poj 3378 Crazy Thairs dp

2013-05-04 02:18:06

#### poj 3378 Crazy Thairs 动态规划

2013-10-13 20:47:06

#### Crazy Thairs_poj3378_DP+离散+高精度+树状数组

2016-05-26 21:19:04

#### POJ 3378——Crazy Thairs（树状数组+dp+高精度）数据结构优化的DP

2014-08-29 16:20:53

#### poj 3378 Crazy Thairs 树状数组+高精度+dp

2014-12-27 23:08:44

#### POJ 3378 / UESTC 1460 - Crazy Thairs

2012-07-28 13:10:40

#### [PKU 3378]Crazy Thairs(平衡树)

2009-09-08 12:32:00

#### POJ 3378 - Crazy Thairs 树状数组+dp+离散化+高精度..

2013-10-04 17:33:25

#### 【转】POJ 3378 Crazy Thairs(数据集中+DP+树状数组+高精度)

2017-08-24 11:47:17