这个题,先是数组开小了,然后是输出结果用int存不下。
时候超过了1000MS,时间上比较长啊。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<set>
#include<cstdlib>
#include<cstring>
using namespace std;
#define N 1111111
int n;
struct my
{
int id;
long long v;
}
go[N];
int hi[N];
int t[N];
bool cmp1(my a,my b)
{
return a.v<b.v;
}
bool cmp2(my a,my b)
{
return a.id>b.id;
}
void update(int k)
{
k+=2;
while (k<N)
{
t[k]++;
k+=k&-k;
}
}
int get(int k)
{
k+=2;
int sum=0;
while (k)
{
sum+=t[k];
k-=k&-k;
}
return sum;
}
int main()
{
freopen("in.txt","r",stdin);
int i,j,k;
while (cin>>n,n)
{
for (i=0;i<n;i++)
{
scanf("%lld",&go[i].v);
go[i].id=i;
}
sort(go,go+n,cmp1);
k=1;
for (i=0;i<n;i++)
{
go[i].v=k++;
}
sort(go,go+n,cmp2);
memset(t,0,sizeof(t));
long long ans;
ans=0;
for (i=0;i<n;i++)
{
ans+=get(go[i].v);
update(go[i].v);
}
cout<<ans<<endl;
}
return 0;
}
Ultra-QuickSort
Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 27087 | Accepted: 9707 |
Description
![](https://i-blog.csdnimg.cn/blog_migrate/b3530eaa8fa98ef394d3639722240a8c.jpeg)
Ultra-QuickSort produces the output
Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.
Input
The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.
Output
For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.
Sample Input
5 9 1 0 5 4 3 1 2 3 0
Sample Output
6 0
Source