codevs.1688 求逆序对
来源:互联网 发布:淘宝如何设为公益宝贝 编辑:程序博客网 时间:2024/05/21 11:02
题目描述 Description
给定一个序列a1,a2,…,an,如果存在i<j并且ai>aj,那么我们称之为逆序对,求逆序对的数目
数据范围:N<=105。Ai<=105。时间限制为1s。
输入描述 Input Description
第一行为n,表示序列长度,接下来的n行,第i+1行表示序列中的第i个数。
输出描述 Output Description
所有逆序对总数.
样例输入 Sample Input
4
3
2
3
2
样例输出 Sample Output
3
数据范围及提示 Data Size & Hint
直接归并排序求逆序数
代码如下:
#include<cstdio>#include<iostream>#include<string>#include<algorithm>#include<iomanip>using namespace std;const int maxn = 100050;long long int ans=0;void merge_sort(long long *a,int x,int y,long long *t);int main(){ //freopen("Text.txt","r",stdin); int n,x; long long a[maxn],t[maxn]; cin>>n; for(int i=1;i<=n;i++){ cin>>x; a[i]=x; } merge_sort(a,1,n,t); /*for(int i=1;i<=n;i++) cout<<t[i]<<ends; cout<<endl;*/ cout<<ans<<endl; return 0;}void merge_sort(long long *a,int x,int y,long long *t){ if(x==y)return ; int m=(x+y)/2; int p=x,q=m+1,i=x; merge_sort(a,p,m,t); merge_sort(a,q,y,t); while(p<=m&&q<=y){ if(a[p]>a[q]){ ans+=m-p+1; t[i++]=a[q++]; } else t[i++]=a[p++]; } while(p<=m)t[i++]=a[p++]; while(q<=y)t[i++]=a[q++]; for(i=x;i<=y;i++) a[i]=t[i];}
阅读全文
0 0
- Codevs 1688 求逆序对
- codevs.1688 求逆序对
- codevs 1688 逆序对
- 归并排序求逆序对 CODEVS 1688 && NOIP 2013 火柴排队
- 【CS 1688】求逆序对(归并排序)
- 求逆序对
- 求逆序对
- 求逆序对
- wikioi1688 求逆序对
- POJ2299 求逆序对
- 归并求逆序对
- 求逆序对
- 求逆序对
- 【a703】求逆序对
- CodeVS1688 求逆序对
- LintCode 求逆序对
- 求逆序对做法
- 求逆序对
- CC1310笔记 TI CCS安装旧版本的Complier
- 习题3.12 另类循环队列
- 解决Maven不能下载“com.springsource.net.sf.cglib”jar
- 一个扫描ip的脚本
- 软件测试知识学习路线
- codevs.1688 求逆序对
- React Js 与 Native 之间的通信
- codeforces 263A(Beautiful Matrix) Java
- ubuntu下安装nginx时依赖库zlib,pcre,openssl安装方法
- 5-4 是否同一棵二叉搜索树
- php获取textarea的值作为邮箱内容发出去不换行的问题处理,即回车换行的方法
- Opencv学习之图像的矩
- Fragment中测量宽度、高度, Caused by: java.lang.IllegalArgumentException: width and height must be > 0
- less中使用calc