DCE Coders admins are way much geekier than they actually seem! Kartik has been following that tradition lately. How? Well, he took the inversion count problem to a whole new level!

Sounds pretty normal to you, huh? Wanna challenge him? Try solving his version of inversion count then!

You are given a 2-d array of integers. You need to find out the inversion count of that array. A pair of integers in the 2-d array counts as an inversion pair (A,B) if and only if:

  • There exists a valid path from top-left corner (0,0) to bottom right corner (r,c) such that A and B integers lie on that path.
  • A occurs before B on that path.
  • And, A > B.

A valid path is defined as a path that can be traversed from top-left corner (0,0) to bottom-right corner (r,c) by moving only in right or downwards direction, without moving out of the grid.

Are you geekier than Kartik?

Constraints:

0 < R,C <= 300

0 < Ai <= 10^5, where Ai stands for an integer in the array.

Input

First line contains space separated 2 integers, R and C, denoting the number of rows and columns.

Next R lines contain C space separated integers representing the 2-d array

Output

Output the number of inversion pairs as described in the problem statement.

Example

Input:

4 4

3 4 2 5

1 7 11 16

8 9 6 12

10 13 15 14

Output:

10

题意:就是二维平面上,如果(i,j)左方,上方,左上方的数大于a[i][j],则称其为一堆逆序对,求逆序数。

思路:因为自己之前写的数状数组求逆序对都是离散化然后倒序累加,这个方法在这里不适用。 正确的打开方式是:按从大到小的顺序加入数状数组,然后统计比自己先加入(即比自己大或者等于),而且X坐标和Y坐标小于等于自己的数的个数(保证了反向)。同时注意处理大小相同的数!

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
struct in{
int a,x,y;
}s[maxn*maxn];
bool cmp(in w,in v){
if(w.a==v.a) {
if(w.x==v.x) return w.y>v.y;
return w.x>v.x; //这里保证了两个数相同的时候,在树状数组里没有包含关系。
}
return w.a>v.a;
}
int sum[maxn][maxn],N,M;
void add(int x,int y){
for(int i=x;i<=N;i+=(-i)&i)
for(int j=y;j<=M;j+=(-j)&j)
sum[i][j]++;
}
long long query(int x,int y){
long long res=;
for(int i=x;i;i-=(-i)&i)
for(int j=y;j;j-=(-j)&j)
res+=sum[i][j];
return res;
}
int main()
{
int i,j,cnt=; long long ans=;
scanf("%d%d",&N,&M);
for(i=;i<=N;i++)
for(j=;j<=M;j++){
scanf("%d",&s[++cnt].a);
s[cnt].x=i; s[cnt].y=j;
}
sort(s+,s+cnt+,cmp);
for(i=;i<=cnt;i++){
ans+=query(s[i].x,s[i].y);
add(s[i].x,s[i].y);
}
printf("%lld\n",ans);
return ;
}

感谢Wxk给的思路! 不然我一直用之前的方法做,几乎不会想出来。

SPOJ:Another Version of Inversion(二维数组的逆序对)的更多相关文章

  1. Another Version of Inversion 二维树状数组求逆序对

    Another Version of Inversion 题意:只有2种走路方式,往右或者往下,求先走到一个大的数,在走到小的数的这种方式有多少.也就是说求出关于这个2维矩阵的逆序数. 题解:二维数组 ...

  2. Java的二维数组的应用及杨辉三角的编写

    (1) 编写一个程序,生成一个10*10的二维随机整数数组,并将该数组的每行最大值保存于一个一维数组中,将每列平均值保存于另外一个一维数组中并分别输出. (2) 编程输出杨辉三角的前10行. 找出一个 ...

  3. php对二维数组进行相关操作(排序、转换、去空白等)

    php对二维数组进行相关操作(排序.转换.去空白等) 投稿:lijiao 字体:[增加 减小] 类型:转载 时间:2015-11-04   这篇文章主要介绍了php对二维数组进行相关操作,包括php对 ...

  4. Python学习笔记_二维数组的查找判断

    在进行数据处理的工作中,有时只是通过一维的list和有一个Key,一个value组成的字典,仍无法满足使用,比如,有三列.或四列,个数由不太多. 举一个现实应用场景:学号.姓名.手机号,可以再加元素 ...

  5. 二维数组中的查找(C++和Python实现)

    (说明:本博客中的题目.题目详细说明及参考代码均摘自 “何海涛<剑指Offer:名企面试官精讲典型编程题>2012年”) 题目 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列 ...

  6. Python之二维数组N*N顺时针旋转90度

    需求:把一个二维数组顺时针旋转90度,现实数据的替换. 比如把4*4的二维数组顺时针旋转90度 原始数据是一个嵌套列表:[['A', 'B', 'C', 'D'], ['A', 'B', 'C', ' ...

  7. 二维数组中的查找 - Java版 -简单二分查找 -<<剑指Offer>> -水题

    如题 (总结) -认真读题, 还WA了一次, https://www.nowcoder.com/practice/abc3fe2ce8e146608e868a70efebf62e?tpId=13&am ...

  8. 实验----Java的二维数组的应用及杨辉三角的编写

    (1) 编写一个程序,生成一个10*10的二维随机整数数组,并将该数组的每行最大值保存于一个一维数组中,将每列平均值保存于另外一个一维数组中并分别输出. (2) 编程输出杨辉三角的前10行. 找出一个 ...

  9. Java二维数组转成稀疏sparsearray数组

    稀疏数组 基本介绍 当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组. 稀疏数组的处理方法是: 记录数组一共有几行几列,有多少个不同的值 把具有不同值的元素的行列及值记 ...

随机推荐

  1. AC日记——香甜的黄油 codevs 2038

    2038 香甜的黄油 USACO  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description 农夫Jo ...

  2. 属性font-family:Font property font-family does not have generic default

    以前定义字体都是用的常用的字体,也没注意过会有这个提示,昨天在写界面的时候重新定义了一个本地没有的字体,发现会有提示. W3C的文档: font-family:<family-name>, ...

  3. OHIFViewer meteor build 问题

    D:\Viewers-master\OHIFViewer>meteor build --directory d:/h2zViewerC:\Users\h2z\AppData\Local\.met ...

  4. 【Todo】【转载】Scala中Array, List, Tuple的区别

    参考了这篇文章: https://my.oschina.net/u/1034176/blog/512314 1. 在Scala 2.7中,Array.List都不能混合类型,只有Tuple可以:而在S ...

  5. 区间重合判断[poj2808 校门外的树]

    题目:http://bailian.openjudge.cn/practice/2808/ 参考了文章,重写了代码:http://www.cnblogs.com/youxin/p/3266617.ht ...

  6. centos 複製時顯示進度的指令 pv

    Pipe Viewer 的简称pv:意思是通过管道显示数据处理进度的信息.这些信息包括已经耗费的时间,完成的百分比(通过进度条显示),当前的速度,全部传输的数据,以及估计剩余的时间. yum inst ...

  7. [WASM] Create a New Rust/Webpack Project using the rust-webpack Template

    Previous to this post, we set up our own Rust/wasm project from scratch. The Rust/wasm team ships a ...

  8. Android新技术学习——阿里巴巴免Root无侵入AOP框架Dexposed

    阿里巴巴无线事业部近期开源的Android平台下的无侵入运行期AOP框架Dexposed,该框架基于AOP思想,支持经典的AOP使用场景.可应用于日志记录,性能统计,安全控制.事务处理.异常处理等方面 ...

  9. redux 及 相关插件 项目实战

    目录结构 +-- app | +-- actions | +-- index.js | +-- components | +-- content.js | +-- footer.js | +-- se ...

  10. 嵌入式程序员应知道的0x10个C语言Tips

    [1].[代码] [C/C++]代码 跳至 [1] ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 ...