标签:line poj ima org elements return ever 连续 ini
In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define MAX 500050
typedef long long ll;
int a[MAX];
int c[MAX];
struct node
{
int num;
ll v;
bool operator < (const node &b ) const //重载一下运算符,这里的const可加可不加,对于不同编译器是有区别的
{
return v<b.v;
}
}b[MAX];
int lowbit(int i)
{
return i&(-i);
}
void add(int x,int v)
{
while(x<=MAX)
{
c[x]+=v;
x+=lowbit(x);
}
}
int sum(int x)
{
int res=0;
while(x>0)
{
res+=c[x];
x-=lowbit(x);
}
return res;
}
int main()
{
int n;
while(scanf("%d",&n),n)
{
for(int i=1;i<=n;i++)
{
scanf("%d",&b[i].v);
b[i].num=i;
}
sort(b+1,b+n+1); //值排序
memset(a,0,sizeof(a));
a[b[1].num]=1; //对于最小值当然标最小啦
ll ans=0;
for(int i=2;i<=n;i++)
{
if(b[i].v==b[i-1].v)
a[b[i].num]=a[b[i-1].num];
else
a[b[i].num]=i; // 记录前面比他小的数。
}
memset(c,0,sizeof(c));
for(int i=1;i<=n;i++)
{
add(a[i],1);
ans+=sum(n)-sum(a[i]);
}
printf("%lld\n",ans);
}
}
POJ-2299 Ultra-QuickSort (树状数组,离散化,C++)
标签:line poj ima org elements return ever 连续 ini
原文地址:http://www.cnblogs.com/ygtzds/p/7399712.html