HDU 4911 Inversion(归并排序求逆序数)
来源:互联网 发布:企业工商信息数据库 编辑:程序博客网 时间:2024/05/22 02:01
归并排序求逆序数,然后ans-k与0取一个最大值就可以了。
也可以用树状数组做,比赛的时候可能姿势不对,树状数组wa了、、
Inversion
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 578 Accepted Submission(s): 249
Problem Description
bobo has a sequence a1,a2,…,an. He is allowed to swap two adjacent numbers for no more than k times.
Find the minimum number of inversions after his swaps.
Note: The number of inversions is the number of pair (i,j) where 1≤i<j≤n and ai>aj.
Find the minimum number of inversions after his swaps.
Note: The number of inversions is the number of pair (i,j) where 1≤i<j≤n and ai>aj.
Input
The input consists of several tests. For each tests:
The first line contains 2 integers n,k (1≤n≤105,0≤k≤109). The second line contains n integers a1,a2,…,an (0≤ai≤109).
The first line contains 2 integers n,k (1≤n≤105,0≤k≤109). The second line contains n integers a1,a2,…,an (0≤ai≤109).
Output
For each tests:
A single integer denotes the minimum number of inversions.
A single integer denotes the minimum number of inversions.
Sample Input
3 12 2 13 02 2 1
Sample Output
12
#include <algorithm>#include <iostream>#include <stdlib.h>#include <string.h>#include <iomanip>#include <stdio.h>#include <string>#include <queue>#include <cmath>#include <stack>#include <map>#include <set>#define eps 1e-12///#define M 1000100#define LL __int64///#define LL long long///#define INF 0x7ffffff#define INF 0x3f3f3f3f#define PI 3.1415926535898#define zero(x) ((fabs(x)<eps)?0:x)using namespace std;const int maxn = 10010000;int num[maxn];LL ans;int tmp[maxn];void Sell(int l, int mid, int r){ int i = l; int j = mid+1; int k = 0; while(i <= mid && j <= r) { if(num[i] > num[j]) { tmp[k++] = num[j++]; ans += mid-i+1; } else tmp[k++] = num[i++]; } while(i <= mid) tmp[k++] = num[i++]; while(j <= r) tmp[k++] = num[j++]; for(int i = 0; i < k; i++) num[i+l] = tmp[i];}void Sort(int l, int r){ if(l < r) { int mid = (l+r)/2; Sort(l, mid); Sort(mid+1, r); Sell(l, mid, r); }}int main(){ int n, k; while(cin >>n>>k) { ans = 0; for(int i = 0;i < n; i++) scanf("%d",&num[i]); Sort(0, n-1); cout<<max(0LL, ans-k)<<endl; } return 0;}
0 1
- HDU 4911 Inversion(归并排序求逆序数)
- HDU 4911 Inversion【归并排序求逆序数】
- A - Inversion 归并排序求逆序数
- HDU 4911 Inversion (归并排序求逆序对)
- Inversion (hdu 4911 树状数组 || 归并排序 求逆序对)
- HDU 4911 Inversion(归并排序求逆序对)
- hdu 4911 归并排序求逆序数
- hdu 4911 归并排序求逆序数
- HDU 4911 Inversion(归并求逆序对)
- hdu 1394 Minimum Inversion Number 求逆序数(树状数组/归并排序/暴力)
- HDU1394-Minimum Inversion Number-归并排序求最小逆序数
- hdu 1394 Minimum Inversion Number 归并求逆序数
- hdu 4911 Inversion(求逆序数)
- HDU 4911 Inversion 求逆序数对
- 归并排序求逆序数——HDU 4911
- hdu 4911 Inversion(归并排序求逆序对数)2014多校训练第5场
- [hdu]1394 Minimum Inversion Number -- 暴力求逆序、树状数组求逆序、线段树求逆序、归并排序求逆序
- 利用归并求逆序数 Inversion
- 网店系统/电子商务开源软件
- sort 与 qsort
- Snmpv1 v2报文格式详细介绍
- hdu 4911 Inversion 多校第五场 求逆序数
- iOS_20_微博骨架搭建
- HDU 4911 Inversion(归并排序求逆序数)
- REUSE_ALV_FIELDCATALOG_MERGE的用法
- 链表
- 驴子圈资讯:携程多元化投资打造未来竞争优势
- ASP.net MVC 基于角色的权限控制系统的实现
- UVA 1626 - Brackets sequence
- 课程复习——数据库系统概论(第4版 王珊 萨师煊 高等教育出版社)—— 第十一章 并发控制
- Makefile基础练习
- oracle11g中impdp导入dump文件