[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
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
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 ...
随机推荐
- jsp--文本框正则表达式
1.文本框只能输入数字代码(小数点也不能输入) <input onkeyup="this.value=this.value.replace(/\D/g,'')" onafte ...
- [算法] get_lucky_price price
int get_lucky_price(int price, const vector & number) 题意大概是给你一个数price,比如1000,然后有unlucky_num,有{1, ...
- Java异常(1)
一.要达到的效果 如果出现错误而是某些操作没有完成,程序应该: (1)返回到一种安全状态,并能够让用户执行一些其他的命令. (2)允许用户保存所有操作的结果,并以适当的方式终止程序. 异常处理的任 ...
- Android - 安装 windows 7 安装 Android SDK 的时候出现的问题!(Connection to https://dl-ssl.google.com refused)
Android - 安装 windows 7 安装 Android SDK 的时候出现的问题! 首先看到 Connection to https://dl-ssl.google.com refuse ...
- jquery 在页面中按回车 响应 事件
为了用户方便我们往往会在用户回车之后做一些事,比如登陆的时候,填完表单过后,我们习惯性的会直接按回车,当然要处理这个,jquery是很简单的,我们来看看怎么做吧. $(document).ready( ...
- struct timespec 和 struct timeval
time()提供了秒级的精确度 . 1.头文件 <time.h> 2.函数原型 time_t time(time_t * timer) 函数返回从TC1970-1-1 0:0:0开始到现在 ...
- jQuery的基本信息。以及入门Demo
javascript目前最流行的框架jQuery,它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器.jQuery的作者的核心理念就是write less,do more(写得更少,做得更多).他 ...
- 通过xib文件创建和连接UIView
使用Xcode写程序,时间长了,对于界面上的元素,很多时候不想手写代码进行构建,但是有些UIView里面的子控件,又不能通过xib直接进行构造,要是总使用UIViewController来结合xib进 ...
- poj 2828 Buy Tickets (线段树(排队插入后输出序列))
http://poj.org/problem?id=2828 Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissio ...
- 数据结构---顺序表(C++)
顺序表 是用一段地址连续的存储单元依次存储线性表的数据元素. 通常用一维数组来实现 基本操作: 初始化 销毁 求长 按位查找 按值查找 插入元素 删除位置i的元素 判空操作 遍历操作 示例代码: // ...