[HDU POJ] 逆序数
HDU 1394
Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11961 Accepted Submission(s): 7310
For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of the seqence, we will obtain another sequence. There are totally n such sequences as the following:
a1, a2, ..., an-1, an (where m = 0 - the initial seqence)
a2, a3, ..., an, a1 (where m = 1)
a3, a4, ..., an, a1, a2 (where m = 2)
...
an, a1, a2, ..., an-1 (where m = n-1)
You are asked to write a program to find the minimum inversion number out of the above sequences.
1 3 6 9 0 8 5 7 4 2
逆序数、改段求点
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
using namespace std;
#define INF 0x3f3f3f3f
#define N 5010 int n;
int a[N];
int c[N]; int lowbit(int x)
{
return x&(-x);
}
void update(int pos,int val)
{
while(pos>)
{
c[pos]+=val;
pos-=lowbit(pos);
}
}
int query(int pos)
{
int res=;
while(pos<=n)
{
res+=c[pos];
pos+=lowbit(pos);
}
return res;
}
int main()
{
int res;
while(scanf("%d",&n)!=EOF)
{
res=;
memset(c,,sizeof(c));
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
a[i]++;
}
for(int i=;i<=n;i++)
{
update(a[i]-,);
res+=query(a[i]);
}
//printf("逆序数:%d",res);
int Min=INF;
for(int i=;i<=n;i++)
{
res+=n-*a[i]+;
Min=min(res,Min);
}
printf("%d\n",Min);
}
return ;
}
POJ 2299
Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 43824 | Accepted: 15983 |
Description

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
Output
Sample Input
5
9
1
0
5
4
3
1
2
3
0
Sample Output
6
0
Source
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f
#define N 500000 int n;
int a[N];
int b[N];
int c[N]; int lowbit(int x)
{
return x&(-x);
}
void update(int pos,int val)
{
while(pos>)
{
c[pos]+=val;
pos-=lowbit(pos);
}
}
int query(int pos)
{
int res=;
while(pos<=N)
{
res+=c[pos];
pos+=lowbit(pos);
}
return res;
}
int main()
{
while(scanf("%d",&n),n)
{
ll res=;
memset(c,,sizeof(c));
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b,b+n);
for(int i=;i<n;i++)
{
int pos=lower_bound(b,b+n,a[i])-b;
pos++;
update(pos-,);
res+=query(pos);
}
printf("%lld\n",res);
}
return ;
}
[HDU POJ] 逆序数的更多相关文章
- HDU 4944 逆序数对
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911 题意: 给出一个序列,可以相邻的交换k次,求 k 次之后,逆序数对最少是多少: 分析: 可以发现 ...
- HDU 1394 (逆序数) Minimum Inversion Number
原来求逆序数还可以用线段树,涨姿势了. 首先求出原始序列的逆序数,然后递推每一个序列的逆序数. #include <cstdio> #include <cstring> #in ...
- HDU 1394 逆序数 线段树单点跟新 | 暴力
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- hdu 1394 逆序数(线段树)
http://acm.hust.edu.cn/vjudge/problem/15764 http://blog.csdn.net/libin56842/article/details/8531117 ...
- POJ 2299 Ultra-QuickSort 逆序数 树状数组 归并排序 线段树
题目链接:http://poj.org/problem?id=2299 求逆序数的经典题,求逆序数可用树状数组,归并排序,线段树求解,本文给出树状数组,归并排序,线段树的解法. 归并排序: #incl ...
- HDU 4911 (树状数组+逆序数)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4911 题目大意:最多可以交换K次,就最小逆序对数 解题思路: 逆序数定理,当逆序对数大于0时,若ak ...
- 逆序数 POJ 2299 Ultra-QuickSort
题目传送门 /* 题意:就是要求冒泡排序的交换次数. 逆序数:在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序. 一个排列中逆序的总数就称为这个排列的逆 ...
- HDU 1394 Minimum Inversion Number(线段树/树状数组求逆序数)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- poj 1804 (nyoj 117)Brainman : 归并排序求逆序数
点击打开链接 Brainman Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 7810 Accepted: 4261 D ...
随机推荐
- PHP递归
<?php /** * Mr.xml * 处理无限级分类 */ class Category{ /** * [递归一维数组] * @param [type] $cate [传递一个数组$cate ...
- Oracle 关于事物的描述
事物在Oracle中的4种状态: commit--提交 rollback--全部回滚 savepoint name;--定义一个回滚到这里的点:例如:savepoint a; rollback to ...
- Asp.Net细节性问题精萃
1.<%=…%>与<%#… %>的区别: 答:<%=…%>是在程序执行时调用,<%#… %>是在DataBind()方法之后被调用 2.控件接收哪些类型 ...
- C# 实体model验证输出
新建Model实体: [Required(ErrorMessage = @"地址 1 为必填项!")] [StringLength(, ErrorMessage = @" ...
- HttpContext.Current多线程调用
1.在web网站的Global中,进行数据量比较大的初始化工作,而为了使用户在页面上能够及时响应,我们在Global中开启了一个线程执行该函数模块. 不过,在线程中用到HttpContext.Curr ...
- javascript cookie 操作
<html> <head> <meta charset="utf-8"> <title>Javascript cookie</ ...
- 【10】了解Bootstrap栅格系统基础案例(5)
这次我们来说下列排序: 通过使用 .col-md-push-* 和 .col-md-pull-* 类就可以很容易的改变列(column)的顺序. <!DOCTYPE html> <h ...
- 从Python传递JSON到JavaScript
OS: Windows 8.1 with update 关键字:Python 3.4,HTML5,JSON,JavaScript 1.LocalServer.py,启动server,打开网页,传递JS ...
- Kinetic使用注意点--animation
new Animation(func, layers) 参数: func:每一帧都会调用一次此函数.此函数接收一个包含四个元素的参数对象,时间单位均为毫秒. { timeDiff:"上一帧和 ...
- uCGUI动态内存管理
动态内存的堆区 /* 堆区共用体定义 */ typedef union { /* 可以以4字节来访问堆区,也可以以1个字节来访问 */ ]; /* required for proper aligne ...