SPOJ:Another Version of Inversion(二维数组的逆序对)
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(二维数组的逆序对)的更多相关文章
- Another Version of Inversion 二维树状数组求逆序对
Another Version of Inversion 题意:只有2种走路方式,往右或者往下,求先走到一个大的数,在走到小的数的这种方式有多少.也就是说求出关于这个2维矩阵的逆序数. 题解:二维数组 ...
- Java的二维数组的应用及杨辉三角的编写
(1) 编写一个程序,生成一个10*10的二维随机整数数组,并将该数组的每行最大值保存于一个一维数组中,将每列平均值保存于另外一个一维数组中并分别输出. (2) 编程输出杨辉三角的前10行. 找出一个 ...
- php对二维数组进行相关操作(排序、转换、去空白等)
php对二维数组进行相关操作(排序.转换.去空白等) 投稿:lijiao 字体:[增加 减小] 类型:转载 时间:2015-11-04 这篇文章主要介绍了php对二维数组进行相关操作,包括php对 ...
- Python学习笔记_二维数组的查找判断
在进行数据处理的工作中,有时只是通过一维的list和有一个Key,一个value组成的字典,仍无法满足使用,比如,有三列.或四列,个数由不太多. 举一个现实应用场景:学号.姓名.手机号,可以再加元素 ...
- 二维数组中的查找(C++和Python实现)
(说明:本博客中的题目.题目详细说明及参考代码均摘自 “何海涛<剑指Offer:名企面试官精讲典型编程题>2012年”) 题目 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列 ...
- Python之二维数组N*N顺时针旋转90度
需求:把一个二维数组顺时针旋转90度,现实数据的替换. 比如把4*4的二维数组顺时针旋转90度 原始数据是一个嵌套列表:[['A', 'B', 'C', 'D'], ['A', 'B', 'C', ' ...
- 二维数组中的查找 - Java版 -简单二分查找 -<<剑指Offer>> -水题
如题 (总结) -认真读题, 还WA了一次, https://www.nowcoder.com/practice/abc3fe2ce8e146608e868a70efebf62e?tpId=13&am ...
- 实验----Java的二维数组的应用及杨辉三角的编写
(1) 编写一个程序,生成一个10*10的二维随机整数数组,并将该数组的每行最大值保存于一个一维数组中,将每列平均值保存于另外一个一维数组中并分别输出. (2) 编程输出杨辉三角的前10行. 找出一个 ...
- Java二维数组转成稀疏sparsearray数组
稀疏数组 基本介绍 当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组. 稀疏数组的处理方法是: 记录数组一共有几行几列,有多少个不同的值 把具有不同值的元素的行列及值记 ...
随机推荐
- centos 关于防火墙的命令
CentOS7默认的防火墙不是iptables,而是firewalle. 安装iptable iptable-service #先检查是否安装了iptables service iptables st ...
- HDU - 5974 A Simple Math Problem (数论 GCD)
题目描述: Given two positive integers a and b,find suitable X and Y to meet the conditions: X+Y=a Least ...
- 【Todo】UDP P2P打洞原理
参考以下两篇文章: https://my.oschina.net/ososchina/blog/369206 http://m.blog.csdn.net/article/details?id=666 ...
- 第三讲_图像特征与描述Image Feature Descriptor
第三讲_图像特征与描述Image Feature Descriptor 概要 特征提取方法 直方图 对图片数据/特征分布的一种统计:对不同量进行直方图统计:可以表示灰度,颜色,梯度,边缘,形状,纹理, ...
- Answer's Question about pointer
When you create a new pointer, this will be in heap until you delete it. So what you said is sort o ...
- weexapp 开发流程(二)框架搭建
1.创建 入口文件 src / entry.js /** * 入口文件 */ import App from './App.vue' import router from './router' // ...
- AngularJs概述
- unity常见问题之20题
1:天空盒有接缝怎么解决? 答:在贴图导入设置里设置Wrap Mode为"Clamp". 2: DDS格式怎么不显示? 答:Unity不支持DDS格式,Unity会将除DDS外的其 ...
- Hibernate 配置C3P0 连接池
第一步在hibernate.cfg.xml配置 <!-- 连接池 --> <property name="hibernate.connection.provider_cla ...
- [游戏]L4D求生之路官方比赛地图修补完好说明
游戏模式:L4D求生之路4356(1.0.2.1)药抗比赛模式 更新日期:2015.06.04 -----毫不留情01----- 1.开局补给手枪 -----毫不留情02----- 1.开局补给手枪 ...