[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 ...
随机推荐
- bat里如何用相对路径
在bat中直接使用绝对路径没有问题,但是文件传到其他地方时,绝对路径会发生改变,因此想通过使用相对路径来解决. 可以通过在bat获取当前bat所在的目录,然后cd 该目录来解决该问题 在bat前面增加 ...
- Android_时间服务
接着上一节,这次我查看了Android的时间服务,觉得帮助很大,解决了我很多疑问,下面我就自己总结了一下,加深了自己的印象,好记性不如烂笔头,还真讲得很不错,收下吧?看下图如何利用线程更新UI组件 重 ...
- lucene 4.0学习
一:官方文件 http://lucene.apache.org/core/4_0_0/ ps:网上参考文章:http://www.cnblogs.com/xing901022/p/3933675.ht ...
- MVC 弹出提示框
第一种弹框成功后要刷新界面 [HttpPost] public ActionResult Add(Maticsoft.Model.Project.ProjectMoneyPlan model) { m ...
- demo_01 css3中的radius
css属性:border-radius :border:边框:radius:弧度:所以这个属性的意思很明了. 下面实现一个小demo: <!doctype html> <html&g ...
- css写圆角效果
.introTips i{ position: absolute; display: block; top: 8px; right: 8px; width:; height:; font-size:; ...
- php 开启缓冲,页面纯静态化
服务器默认不开启php缓冲区 两种方法开启 1.php.ini out_put_buffer = on 2.ob_start(); 页面纯静态化 file_put_contents()写文件 ob_s ...
- Erlang 开发者的福音:IntelliJ IDEA 的 Erlang 插件
IntelliJ IDEA 的 Erlang 插件,主要特性: 智能编辑器: Erlang 代码补全.语法和错误高亮.代码检查 代码导航:项目和文件结构视图.在文件.模型.函数和用例之间快速跳转 工 ...
- ImportError: No module named pysqlite2 --安装pysqlite
yum install sqlite-devel -y pip install pysqlite 每次使用yum安装额外的包之后都需要重新安装python,否则可能会有各种奇奇怪怪的问题出现 cd P ...
- vi 在行首尾添加字符串
在行首尾添加字符串 用下列命令在文本每行的行首添加字符串“NewString”,请注意空格的存在. :g/^/s//NewString/g :%s/^/NewString/g ...