题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1107

题意:中文题诶~

思路:通过题意可以发现对于两点p1(x1, y1),p2(x2, y2), 若x1<x2&&y1>y2则线段p1p2满足要求,那么显然可以先按x值排下序,对应y序列的逆序对数目即为答案;

注意:对于x1==x2&&y1>y2的情况是不满足题意的,可以通过重载排序去除x值相等的情况下的y逆序对;

代码:

 #include <iostream>
#include <algorithm>
#include <stdio.h>
#define ll long long
using namespace std; const int MAXN=5e4+;
struct node{
int first;
int second;
}p[MAXN];
ll ans=; bool cmp(node a, node b){
if(a.first==b.first) return a.second<b.second;
return a.first<b.first;
} void teger_sort(int* a, int* t, int x, int y){
if(y-x>){ //**递归至单个元素为一组
int m=(y+x)>>;
int p=x, q=m, i=x;
teger_sort(a, t, x, m); //***左递归
teger_sort(a, t, m, y); //***右递归
while(p<m||q<y){ //**合并
if(q>=y||(p<m&&a[p]<=a[q])){
t[i++]=a[p++]; //**将左边元素复制到临时数组
}else{
t[i++]=a[q++]; //**将右边元素复制到临时数组
ans+=m-p; //**累计逆序对的数目
}
}
for(int i=x; i<y; i++){ //**将临时数组里已排序的元素还原到原数组
a[i]=t[i];
}
}
} int main(void){
int n, a[MAXN], t[MAXN];
scanf("%d", &n);
for(int i=; i<n; i++){
scanf("%d%d", &p[i].first, &p[i].second);
}
sort(p, p+n, cmp);
for(int i=; i<n; i++){
a[i]=p[i].second;
}
teger_sort(a, t, , n);
printf("%lld\n", ans);
return ;
}

51nod1107(逆序对数&归并排序)的更多相关文章

  1. 求逆序对数总结 & 归并排序

    用归并排序方式 最原始的方法的复杂度是O(n^2). 使用归并排序的方式,可以把复杂度降低到O(nlgn). 设A[1..n]是一个包含N个非负整数的数组.如果在i〈 j的情况下,有A〉A[j],则( ...

  2. 归并排序(归并排序求逆序对数)--16--归并排序--Leetcode面试题51.数组中的逆序对

    面试题51. 数组中的逆序对 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数. 示例 1: 输入: [7,5,6,4] 输出 ...

  3. hdu 4911 求逆序对数+树状数组

    http://acm.hdu.edu.cn/showproblem.php?pid=4911 给定一个序列,有k次机会交换相邻两个位置的数,问说最后序列的逆序对数最少为多少. 实际上每交换一次能且只能 ...

  4. bzoj 3744 Gty的妹子序列 区间逆序对数(在线) 分块

    题目链接 题意 给定\(n\)个数,\(q\)个询问,每次询问\([l,r]\)区间内的逆序对数. 强制在线. 思路 参考:http://www.cnblogs.com/candy99/p/65795 ...

  5. php实现数组中的逆序对(归并排序实现:排序 辅助数组)

    php实现数组中的逆序对(归并排序实现:排序 辅助数组) 一.总结 这题用归并排序  线段树   树状数组 等操作的复杂度应该都是小于n方的 二.php实现数组中的逆序对 题目描述 在数组中的两个数字 ...

  6. P1908 逆序对(归并排序)

    https://www.luogu.com.cn/problem/P1908 归并排序是用来求逆序对的 归并排序的思想就是分治 #include <bits/stdc++.h> using ...

  7. poj 2299 树状数组求逆序对数+离散化

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 54883   Accepted: 20184 ...

  8. 剑指Offer——数组中的逆序对(归并排序的应用)

    蛮力: 遍历数组,对每个元素都往前遍历所有元素,如果有发现比它小的元素,就count++. 最后返回count取模. 结果没问题,但超时哈哈哈,只能过50%.   归并法: 看讨论,知道了这道题的经典 ...

  9. POJ 1804 逆序对数量 / 归并排序

    Brainman Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12175   Accepted: 6147 Descrip ...

随机推荐

  1. Java for LeetCode 124 Binary Tree Maximum Path Sum

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  2. python获取当前的时间

    打印出当前的年月日时分秒 print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))) 2018-09-05 09:39: ...

  3. Android适配API23之后权限的动态申请

    一.权限介绍 对于6.0以下的权限及在安装的时候,根据权限声明产生一个权限列表,用户只有在同意之后才能完成app的安装,造成了我们想要使用某个app,就要默默忍受其一些不必要的权限(比如是个app都要 ...

  4. property 中的strong 与weak

    strong关键字与retain关似,用了它,引用计数自动+1,用实例更能说明一切 @property (nonatomic, strong) NSString *string1; @property ...

  5. 创建blog APP

    声明:此Django分类下的教程是追梦人物所有,地址http://www.jianshu.com/u/f0c09f959299,本人写在此只是为了巩固复习使用 什么是APP呢,Django里的APP其 ...

  6. 国画经典之梅花PSD素材

    国画经典之梅花图片PSD素材,由huiyi8素材网提供. 地址:http://www.huiyi8.com/meihua/​

  7. ps炫光素材

    炫光闪电笔刷,炫光闪电笔刷,雷电笔刷,自然闪电Photoshop笔刷下载ps炫光素材 素材下载:http://www.huiyi8.com/sc/8695.html

  8. BZOJ 1529 [POI2005]ska Piggy banks:并查集

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1529 题意: Byteazar有N个小猪存钱罐. 每个存钱罐只能用钥匙打开或者砸开. By ...

  9. hdu-5818 Joint Stacks(模拟)

    题目链接: Joint Stacks Time Limit: 8000/4000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Othe ...

  10. linux C++ scandir 的使用

    () 头文件 #include <dirent.h> () 函数定义 int scandir(const char *dir,struct dirent **namelist,int (* ...