HDU 4911 Inversion 解题报告(逆序数)
来源:互联网 发布:手机淘宝客服怎么设置 编辑:程序博客网 时间:2024/05/29 14:35
Inversion
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 261 Accepted Submission(s): 112
Problem Description
bobo has a sequence a1,a2,…,an. He is allowed to swap twoadjacent 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
Author
Xiaoxu Guo (ftiasch)
Source
2014 Multi-University Training Contest 5
解题报告:求逆序数inv,输出max(inv-k, 0)即可。这里用线段树求逆序数。代码如下:
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>#include <queue>#include <vector>#include <map>#include <set>#include <string>#include <iomanip>using namespace std;#define ff(i, n) for(int i=0;i<(n);i++)#define fff(i, n, m) for(int i=(n);i<=(m);i++)#define dff(i, n, m) for(int i=(n);i>=(m);i--)#define bit(n) (1LL<<(n))typedef long long LL;typedef unsigned long long ULL;const LL inf=1e15;void work();int main(){#ifdef ACM freopen("input.in", "r", stdin);#endif // ACM work();}/***************************************************/#define lson l, m, pos<<1#define rson m+1, r, pos<<1|1#define defm int m = (l+r)/2;const int maxn = 111111;int num[111111];int yy[111111];int n, k;LL sum[maxn << 2];void updateFather(int pos){ sum[pos] = sum[pos<<1] + sum[pos<<1|1];}void update(int p, int l, int r, int pos){ if(l == r) { sum[pos]++; return; } defm; if(p <= m) update(p, lson); else update(p, rson); updateFather(pos);}LL query(int L, int R, int l, int r, int pos){ if(L<=l && r<=R) return sum[pos]; defm; return (L<=m?query(L, R, lson):0) + (m<R?query(L, R, rson):0);}void work(){ while(scanf("%d%d", &n, &k) == 2) { ff(i, n) scanf("%d", num + i); memcpy(yy, num, sizeof(num)); sort(yy, yy+n); int tot = unique(yy, yy+n) - yy; LL ans = 0; memset(sum, 0, sizeof(sum)); ff(i, n) { int pos = lower_bound(yy, yy+tot, num[i]) - yy; ans += query(pos+1, tot, 0, tot, 1); update(pos, 0, tot, 1); } cout << max(ans - k, 0LL) << endl; }}
0 0
- HDU 4911 Inversion 解题报告(逆序数)
- hdu 4911 Inversion(求逆序数)
- hdu 4911 Inversion (逆序数变换)
- HDU 4911 Inversion 求逆序数对
- HDU 4911 Inversion(逆序对-BIT)
- HDU 4911 Inversion(求逆序对)
- HDU 4911 Inversion 树状数组求逆序数对
- hdu 4911 Inversion 树状数组求逆序数对
- hdu 4911 Inversion 多校第五场 求逆序数
- HDU 4911 Inversion(归并排序求逆序数)
- HDU 4911 Inversion【归并排序求逆序数】
- HDU 1394 Minimum Inversion Number(线段树,逆序数)
- HDU 1394 Minimum Inversion Number (求逆序数)
- 【树状数组(逆序数)】hdu 1394 Minimum Inversion Number
- HDU 1394 Minimum Inversion Number(逆序数)
- hdu 1394 Minimum Inversion Number(逆序数)
- HDU 1394 Minimum Inversion Number(线段树+逆序数)
- HDU 1394 Minimum Inversion Number(求最小逆序数)
- 百钱买百鸡,代码的优化
- leetcode Clone Graph
- hdu 4909 String dp+哈希
- Insertion Sort List 链表插入排序
- [Eage、Eage S系列] 预装Win 8的机型更换Win7及Windows XP如何设置
- HDU 4911 Inversion 解题报告(逆序数)
- 广度优先搜索(算法导论第22章-基本的图算法)
- 常用的APT命令参数
- 动态二维数组
- bzoj1207: [HNOI2004]打鼹鼠
- java的System.getProperty()方法可以获取的值
- Black Box——优先队列
- SetROP2 (hdc, R2_NOT) 函数功能测试
- 51单片机实现scanf和printf函数