http://poj.org/problem?id=2299
我因为忘了估计总数的值有可能超过int,而出错
一下是我的代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int sizen=505000;
struct Ele
{
int value;
int order;
int cnt;
}Q[sizen];
struct ele
{
int left;
int right;
int num;
}p[4*sizen];
void build(int L,int R,int step)
{
p[step].num=0;
p[step].left=L;
p[step].right=R;
if(L!=R)
{
int Mid=(L+R)>>1;
build(L,Mid,2*step);
build(Mid+1,R,2*step+1);
}
}
void update(int x,int step)
{
if(p[step].left==p[step].right)
p[step].num=1;
else
{
int Mid=(p[step].left+p[step].right)>>1;
if(Mid>=x)
update(x,2*step);
else
update(x,2*step+1);
p[step].num=p[2*step].num+p[2*step+1].num;
}
}
int get(int x,int y,int step)
{
if(p[step].left==x&&y==p[step].right)
return p[step].num;
else
{
int Mid=(p[step].left+p[step].right)>>1;
if(Mid>=y)
return get(x,y,2*step);
else
if(Mid+1<=x)
return get(x,y,2*step+1);
else
return get(x,Mid,2*step)+get(Mid+1,y,2*step+1);
}
}
bool cmp(Ele x,Ele y)
{
return x.value<y.value;
}
bool cmp1(Ele x,Ele y)
{
return x.order<y.order;
}
int main()
{
int n;
int i;
int t;
while(scanf("%d",&n),n)
{
for(i=1;i<=n;i++)
{
scanf("%d",&Q[i].value);
Q[i].order=i;
}
sort(Q+1,Q+1+n,cmp);
Q[1].cnt=1;
t=1;
for(i=2;i<=n;i++)
if(Q[i-1].value!=Q[i].value)
Q[i].cnt=++t;
else
Q[i].cnt=t;
sort(Q+1,Q+1+n,cmp1);
build(1,t,1);
__int64 sum=0;
for(i=1;i<=n;i++)
{
if(Q[i].cnt<t)
sum+=(get(Q[i].cnt+1,t,1));
update(Q[i].cnt,1);
}
printf("%I64d\n",sum);
}
return 0;
}
进行压缩还有一种方法
用一个结构体记录值和第几位;
然后安值进行排序,把第几位的值赋给数组a[第几位]++;
然后进行a[],遍历;
<span class="sh_function" style="font-weight: bold;">scanf</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">(</span><span class="sh_string" style="color: red; font-family: monospace;">"%I64d"</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">,&</span>c<span class="sh_symbol" style="color: rgb(139, 0, 0);">[</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">]);</span>
a<span class="sh_symbol" style="color: rgb(139, 0, 0);">[</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">].</span>num<span class="sh_symbol" style="color: rgb(139, 0, 0);">=</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span>
a<span class="sh_symbol" style="color: rgb(139, 0, 0);">[</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">].</span>x<span class="sh_symbol" style="color: rgb(139, 0, 0);">=</span>c<span class="sh_symbol" style="color: rgb(139, 0, 0);">[</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">];</span>
<span class="sh_cbracket" style="color: red;">}</span>
<span class="sh_function" style="font-weight: bold;">sort</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">(</span>a<span class="sh_number" style="color: purple;">+1</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">,</span>a<span class="sh_symbol" style="color: rgb(139, 0, 0);">+</span>n<span class="sh_number" style="color: purple;">+1</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">,</span>cmp<span class="sh_symbol" style="color: rgb(139, 0, 0);">);</span>
<span class="sh_type" style="color: rgb(0, 100, 0);">int</span> num<span class="sh_symbol" style="color: rgb(139, 0, 0);">=</span><span class="sh_number" style="color: purple;">1</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span>
<span class="sh_type" style="color: rgb(0, 100, 0);">__int64</span> temp<span class="sh_symbol" style="color: rgb(139, 0, 0);">=</span>a<span class="sh_symbol" style="color: rgb(139, 0, 0);">[</span><span class="sh_number" style="color: purple;">1</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">].</span>x<span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span>
<span class="sh_keyword" style="color: blue; font-weight: bold;">for</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">(</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">=</span><span class="sh_number" style="color: purple;">1</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);"><=</span>n<span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">++)</span>
<span class="sh_cbracket" style="color: red;">{</span>
<span class="sh_keyword" style="color: blue; font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">(</span>a<span class="sh_symbol" style="color: rgb(139, 0, 0);">[</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">].</span>x<span class="sh_symbol" style="color: rgb(139, 0, 0);">!=</span>temp<span class="sh_symbol" style="color: rgb(139, 0, 0);">)</span>
<span class="sh_cbracket" style="color: red;">{</span>
temp<span class="sh_symbol" style="color: rgb(139, 0, 0);">=</span>a<span class="sh_symbol" style="color: rgb(139, 0, 0);">[</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">].</span>x<span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span>
num<span class="sh_symbol" style="color: rgb(139, 0, 0);">++;</span>
<span class="sh_cbracket" style="color: red;">}</span>
c<span class="sh_symbol" style="color: rgb(139, 0, 0);">[</span>a<span class="sh_symbol" style="color: rgb(139, 0, 0);">[</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">].</span>num<span class="sh_symbol" style="color: rgb(139, 0, 0);">]=</span>num<span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span>
<span class="sh_cbracket" style="color: red;">}</span>
<span class="sh_function" style="font-weight: bold;">build</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">(</span><span class="sh_number" style="color: purple;">1</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">,</span>num<span class="sh_symbol" style="color: rgb(139, 0, 0);">,</span><span class="sh_number" style="color: purple;">1</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">);</span>
<span class="sh_type" style="color: rgb(0, 100, 0);">__int64</span> sum<span class="sh_symbol" style="color: rgb(139, 0, 0);">=</span><span class="sh_number" style="color: purple;">0</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span>
<span class="sh_keyword" style="color: blue; font-weight: bold;">for</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">(</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">=</span><span class="sh_number" style="color: purple;">1</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);"><=</span>n<span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">++)</span>
<span class="sh_cbracket" style="color: red;">{</span>
<span class="sh_keyword" style="color: blue; font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">(</span>c<span class="sh_symbol" style="color: rgb(139, 0, 0);">[</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">]+</span><span class="sh_number" style="color: purple;">1</span><span class="sh_symbol" style="color: rgb(139, 0, 0);"><=</span>num<span class="sh_symbol" style="color: rgb(139, 0, 0);">)</span>
<span class="sh_cbracket" style="color: red;">{</span>
sum<span class="sh_symbol" style="color: rgb(139, 0, 0);">+=</span><span class="sh_function" style="font-weight: bold;">get</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">(</span>c<span class="sh_symbol" style="color: rgb(139, 0, 0);">[</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">]+</span><span class="sh_number" style="color: purple;">1</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">,</span>num<span class="sh_symbol" style="color: rgb(139, 0, 0);">,</span><span class="sh_number" style="color: purple;">1</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">);</span>
<span class="sh_comment" style="color: rgb(165, 42, 42); font-style: italic;">//printf("%d\n",get(c[i]));</span>
<span class="sh_cbracket" style="color: red;">}</span>