[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 ...
随机推荐
- Poj 1001 / OpenJudge 2951 Exponentiation
1.链接地址: http://poj.org/problem?id=1001 http://bailian.openjudge.cn/practice/2951 2.题目: Exponentiatio ...
- Android NFC标签 开发深度解析 触碰的艺术
有几天没有更新博客了,不过本篇却准备了许久,希望能带给每一位开发者最简单高效的学习方式.废话到此为止,下面开始正文. NFC(Near Field Communication,近场通信)是一种数据传输 ...
- MYSQL Error 2006HY000:MySQL server has gone away的解决方案
MySQL server has gone away有几种情况. 1.应用程序(比如PHP)长时间的执行批量的MYSQL语句. 最常见的就是采集或者新旧数据转化. 解决方案: 在my.cnf文件中添加 ...
- hdu 5626 Clarke and points 数学推理
Clarke and points Problem Description The Manhattan Distance between point A(XA,YA) and B(XB,YB) i ...
- Win10 IIS以及ASP.NET 4.0配置问题日志
问题日志 升级到Win10并安装了VS2015后,原有ASP.NET 4.0项目在本机的IIS部署出现问题. 安装IIS: 在[控制面板.程序.启用或关闭Windows功能.Internet Info ...
- Codeforces Round #360 div2
Problem_A(CodeForces 688A): 题意: 有d天, n个人.如果这n个人同时出现, 那么你就赢不了他们所有的人, 除此之外, 你可以赢他们所有到场的人. 到场人数为0也算赢. 现 ...
- 获取PDF页数
下载pdfbox这个包,这俩个方法都可以: PDDocument doc = PDDocument.load("e://aa.pdf"); System.out.println(d ...
- css3 旋转出现动画
@-moz-keyframes daf{ 0% { -moz-transform: rotate(-360deg) scale(0.2); -webkit-transform: rotate(-360 ...
- 百度:在O(1)空间复杂度范围内对一个数组中前后连段有序数组进行归并排序
一.题目理解 题目:数组al[0,mid-1]和al[mid,num-1]是各自有序的,对数组al[0,num-1]的两个子有序段进行merge,得到al[0,num-1]整体有序.要求空间复杂度为O ...
- WCF 服务器已拒绝客户端凭据
将 WCF 服务器和客户端分别部署到不同机器上,可能会触发如下异常. 未处理 System.ServiceModel.Security.SecurityNegotiationException M ...