poj2299 Ultra-QuickSort
2015-11-14 00:19
357 查看
Ultra-QuickSort
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
9 1 0 5 4 ,
Ultra-QuickSort produces the output
0 1 4 5 9 .
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
Sample Output
Source
Waterloo local 2005.02.05
离散化+树状数组求逆序对
(离散化用map会超时…)
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<map>
#define F(i,j,n) for(int i=j;i<=n;i++)
#define D(i,j,n) for(int i=j;i>=n;i--)
#define LL long long
#define MAXN 500005
#define pa pair<int,int>
using namespace std;
int n,b[MAXN],f[MAXN];
LL ans;
struct data
{
int val,pos;
}a[MAXN];
int read()
{
int ret=0,flag=1;char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') flag=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){ret=ret*10+ch-'0';ch=getchar();}
return ret*flag;
}
void add(int k)
{
for(int i=k;i<=n;i+=i&(-i)) f[i]++;
}
LL getsum(int k)
{
LL ret=0;
for(int i=k;i>0;i-=i&(-i)) ret+=f[i];
return ret;
}
bool cmp(data x,data y)
{
return x.val<y.val;
}
int main()
{
n=read();
while (n)
{
memset(f,0,sizeof(f));
ans=0;
F(i,1,n) a[i].val=read(),a[i].pos=i;
sort(a+1,a+n+1,cmp);
F(i,1,n)
{
if (i>1&&a[i].val==a[i-1].val) b[a[i].pos]=b[a[i-1].pos];
else b[a[i].pos]=i;
}
D(i,n,1)
{
if (b[i]>1) ans+=getsum(b[i]-1);
add(b[i]);
}
printf("%lld\n",ans);
n=read();
}
}
Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 50361 | Accepted: 18458 |
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
9 1 0 5 4 ,
Ultra-QuickSort produces the output
0 1 4 5 9 .
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
Waterloo local 2005.02.05
离散化+树状数组求逆序对
(离散化用map会超时…)
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<map>
#define F(i,j,n) for(int i=j;i<=n;i++)
#define D(i,j,n) for(int i=j;i>=n;i--)
#define LL long long
#define MAXN 500005
#define pa pair<int,int>
using namespace std;
int n,b[MAXN],f[MAXN];
LL ans;
struct data
{
int val,pos;
}a[MAXN];
int read()
{
int ret=0,flag=1;char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') flag=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){ret=ret*10+ch-'0';ch=getchar();}
return ret*flag;
}
void add(int k)
{
for(int i=k;i<=n;i+=i&(-i)) f[i]++;
}
LL getsum(int k)
{
LL ret=0;
for(int i=k;i>0;i-=i&(-i)) ret+=f[i];
return ret;
}
bool cmp(data x,data y)
{
return x.val<y.val;
}
int main()
{
n=read();
while (n)
{
memset(f,0,sizeof(f));
ans=0;
F(i,1,n) a[i].val=read(),a[i].pos=i;
sort(a+1,a+n+1,cmp);
F(i,1,n)
{
if (i>1&&a[i].val==a[i-1].val) b[a[i].pos]=b[a[i-1].pos];
else b[a[i].pos]=i;
}
D(i,n,1)
{
if (b[i]>1) ans+=getsum(b[i]-1);
add(b[i]);
}
printf("%lld\n",ans);
n=read();
}
}
相关文章推荐
- POJ - 1986 Distance Queries(离线Tarjan算法)
- 关于UEdiutor文本编辑器。前台展示分文字的问题
- OS X下UNIX环境高级编程(第三版)学习日志-第一章ChapterI,编译apue包与第一个例程
- 禁止uiscrollview垂直方向滚动,只允许水平方向滚动;或只允许垂直方向滚动
- UE4蓝图案例:制作按下键盘F键改变场景中模型的颜色
- iOS 8 Auto Layout界面自动布局系列2-使用Xcode的Interface Builder添加布局约束
- 在T-SQL语句中访问远程数据库(openrowset/opendatasource/openquery)
- IOS的UI基础01
- hdoj 2817 A sequence of numbers【快速幂】
- Codeforces 597C Subsequences 【树状数组优化DP】
- HDU 5493 Queue(线段树)
- iOS开发之UITextView自定义选择内容后的分享菜单
- 在一个Activity中切换多个layout
- UIKit - scrollView缩放、滚动
- android背景设置报错 <item> tag requires a 'drawable' attribute or child tag defining a drawable
- 关于UIView的autoresizingMask属性
- HDU 1711 Number Sequence
- 特征值和特征向量(Eigenvalues and Eigenvectors)
- 每天学一点easyui①
- iOS相机, 相册, 图片库 和 UIAlertController的使用