标签:des blog http io ar os sp for strong
| Time Limit: 7000MS | Memory Limit: 65536K | |
| Total Submissions: 43339 | Accepted: 15798 |
Description
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 Input
Output
Sample Input
5 9 1 0 5 4 3 1 2 3 0
Sample Output
6 0
自从高二以来好像只用线段树没有写过树状数组……随便拿个求逆序对的题练练手
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=0,f=1;char ch=getchar();
while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
return x*f;
}
struct dat{
int a,rnk;
}a[500010];
int d[500010];
int c[500010];
bool operator <(const dat &a,const dat &b)
{return a.a<b.a;}
int n,cnt;
LL ans;
inline int lowbit(int x){return x&(-x);}
inline int change(int x)
{
for (int i=x;i<=n;i+=lowbit(i))
c[i]++;
}
inline int query(int x)
{
int sum=0;
for (int i=x;i;i-=lowbit(i))
sum+=c[i];
return sum;
}
int main()
{
while (scanf("%d",&n)&&n)
{
for (int i=1;i<=n;i++)
a[i].a=read(),a[i].rnk=i;
sort(a+1,a+n+1);
cnt=ans=0;
a[0].a=-1;
for (int i=1;i<=n;i++)
{
if (a[i].a!=a[i-1].a)cnt++;
d[a[i].rnk]=cnt;
}
memset(c,0,sizeof(c));
for (int i=n;i>=1;i--)
{
ans+=query(d[i]);
change(d[i]);
}
printf("%lld\n",ans);
}
}
标签:des blog http io ar os sp for strong
原文地址:http://www.cnblogs.com/zhber/p/4138428.html