浙江工商大学15年校赛I题 Inversion 【归并排序求逆序对】
Inversion
Time Limit
1s
Memory Limit
131072KB
Judge Program
Standard
Ratio(Solve/Submit)
15.00%(3/20)
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. 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). Output:
For each tests:
A single integer denotes the minimum number of inversions. Sample Input:
3 1
2 2 1
3 0
2 2 1
Sample Output:
1
2
在14年的多校联合训练上写过这道题目
求的是逆序对,数据有10^5大小,用O(n^2)的算法肯定会TLE,那么就自然而然想到了用归并排序(算法导论上也提过这个知识点)
但是这道题需要注意的几点就是,如果ans比交换的次数少那么要输出0,ans可能超出int范围所以需要用long long 类型存储
我们知道,求逆序对最典型的方法就是树状数组,但是还有一种方法就是Merge_sort(),即归并排序。
实际上归并排序的交换次数就是这个数组的逆序对个数,为什么呢?
我们可以这样考虑:
归并排序是将数列a[l,h]分成两半a[l,mid]和a[mid+1,h]分别进行归并排序,然后再将这两半合并起来。
在合并的过程中(设l<=i<=mid,mid+1<=j<=h),当a[i]<=a[j]时,并不产生逆序数;当a[i]>a[j]时,在
前半部分中比a[i]大的数都比a[j]大,将a[j]放在a[i]前面的话,逆序数要加上mid+1-i。因此,可以在归并
排序中的合并过程中计算逆序数.
Source Code:
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0) using namespace std; typedef long long ll ;
typedef unsigned long long ull ;
typedef unsigned int uint ;
typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e- ;
const int N = ;
const int M = * ;
const ll P = 10000000097ll ;
const int MAXN = ;
const int INF = 0x3f3f3f3f ;
const int offset = ; int a[],tmp[];
int n;
ll ans; void Merge (int l,int m,int r) {
int i = l;
int j = m + ;
int k = l;
while (i <= m && j <= r) {
if (a[i] > a[j]) {
tmp[k++] = a[j++];
ans += m - i + ;
} else {
tmp[k++] = a[i++];
}
}
while (i <= m) tmp[k++] = a[i++];
while (j <= r) tmp[k++] = a[j++];
for (int i = l; i <= r; ++i)
a[i] = tmp[i];
} void Merge_sort (int l,int r) {
if (l < r) {
int m = (l + r) >> ;
Merge_sort (l,m);
Merge_sort (m+,r);
Merge (l,m,r);
}
} int main() {
std::ios::sync_with_stdio(false);
int i, j, t, k, u, c, v, p, numCase = ; while (cin >> n >> k) {
for (i = ; i < n; ++i) {
cin >> a[i];
}
ans = ;
Merge_sort(, n - );
if (ans - k < ) {
cout << << endl;
} else {
cout << ans - k << endl;
}
} return ;
}
浙江工商大学15年校赛I题 Inversion 【归并排序求逆序对】的更多相关文章
- 浙江工商大学15年校赛E题 无邪的飞行棋 【经典背包】
无邪的飞行棋 Time Limit 1s Memory Limit 64KB Judge Program Standard Ratio(Solve/Submit) 15.38%(4/26) Descr ...
- 浙江工商大学15年校赛C题 我删我删,删删删 【简单模拟】
Description: 有一个大整数.不超过1000位.假设有N位.我想删掉其中的任意S个数字.使得删除S位后,剩下位组成的数是最小的. Input: 有多组数据数据,每组数据为两行.第一行是一个大 ...
- 2014 HDU多校弟五场A题 【归并排序求逆序对】
这题是2Y,第一次WA贡献给了没有long long 的答案QAQ 题意不难理解,解题方法不难. 先用归并排序求出原串中逆序对的个数然后拿来减去k即可,如果答案小于0,则取0 学习了归并排序求逆序对的 ...
- [USACO17FEB] Why Did the Cow Cross the Road I P (树状数组求逆序对 易错题)
题目大意:给你两个序列,可以序列进行若干次旋转操作(两个都可以转),对两个序列相同权值的地方连边,求最少的交点数 记录某个值在第一个序列的位置,再记录第二个序列中某个值 在第一个序列出现的位置 ,求逆 ...
- 浙江大学2015年校赛F题 ZOJ 3865 Superbot BFS 搜索
不知道为什么比赛的时候一直想着用DFS 来写 一直想剪枝结果还是TLE = = 这题数据量不大,又是问最优解,那么一般来说是用 BFS 来写 int commandi[4] = {1, 2, 3, 4 ...
- 哈尔滨工程大学ACM预热赛 补题
链接:https://ac.nowcoder.com/acm/contest/554/A来源:牛客网 小虎刚刚上了幼儿园,老师让他做一个家庭作业:首先画3个格子,第二行有2个格子,第三行有1个格子. ...
- 浙江大学2015年校赛B题 ZOJ 3861 Valid Pattern Lock
这道题目是队友写的,貌似是用暴力枚举出来. 题意:给出一组数,要求这组数在解锁的界面可能的滑动序列. 思路:按照是否能够直接到达建图,如1可以直接到2,但是1不能直接到3,因为中间必须经过一个2. 要 ...
- 哈尔滨工程大学ACM预热赛 G题 A hard problem(数位dp)
链接:https://ac.nowcoder.com/acm/contest/554/G Now we have a function f(x): int f ( int x ) { if ( ...
- Skiing 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛H题(拓扑序求有向图最长路)
参考博客(感谢博主):http://blog.csdn.net/yo_bc/article/details/77917288 题意: 给定一个有向无环图,求该图的最长路. 思路: 由于是有向无环图,所 ...
随机推荐
- appium获取app应用的package和 activity。---新手总结(大牛勿喷,新手互相交流)
从网上搜索的方法: 如下: 1.查看源码 2.日志法a.启动待测apkb.开启日志输出:adb logcat>D:/log.txtc.关闭日志输出:ctrl+cd.查看日志直接搜索 :Displ ...
- yield return in C#
Yield has two great uses It helps to provide custom iteration with out creating temp collections. It ...
- 为什么要用on()而不直接使用click
为什么要用on()而不直接使用clickhttp://stackoverflow.com/questions/10082031/why-use-jquery-on-instead-of-clickht ...
- 自定义Chrome 背景色 (FF需要User Styles插件)
Chrome有个自定义背景色的文件 Custom.css 默认里面什么字都没写 html, body {background-color: #e0dcc0!important;} 这个颜色 ...
- php文件链接数据库基本代码
<?php $conn=@mysql_connect("localhost","root",""); if($conn==null) ...
- BZOJ 3385: [Usaco2004 Nov]Lake Counting 数池塘
题目 3385: [Usaco2004 Nov]Lake Counting 数池塘 Time Limit: 1 Sec Memory Limit: 128 MB Description 农夫 ...
- DBV-00111: OCI failure (3722) (ORA-01002: fetch out of sequence解决
在使用DBV检测segment的时候出现 DBV-00111: OCI failure (3722) (ORA-01002: fetch out of sequence)错误: 在寻找原因过程中发现相 ...
- EF使用时异常:对一个或多个实体的验证失败。有关详细信息
//最顶级异常中是不提示具体哪个字段验证失败,必须到详细异常类型中查看 try { //EF操作 } catch (System.Data.Entity.Validation.DbEntityVali ...
- Querying Microsoft SQL Server 2012 读书笔记:查询和管理XML数据 1 -使用FOR XML返回XML结果集
XML 介绍 <CustomersOrders> <Customer custid="1" companyname="Customer NRZBB&qu ...
- BZOJ 3675: [Apio2014]序列分割( dp + 斜率优化 )
WA了一版... 切点确定的话, 顺序是不会影响结果的..所以可以dp dp(i, k) = max(dp(j, k-1) + (sumn - sumi) * (sumi - sumj)) 然后斜率优 ...