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,或者为同一个值的数组时,可以使用稀疏数组来保存该数组. 稀疏数组的处理方法是: 记录数组一共有几行几列,有多少个不同的值 把具有不同值的元素的行列及值记 ...
随机推荐
- AC日记——[网络流24题]方格取数问题 cogs 734
734. [网络流24题] 方格取数问题 ★★☆ 输入文件:grid.in 输出文件:grid.out 简单对比时间限制:1 s 内存限制:128 MB «问题描述: 在一个有m*n ...
- T3138 栈练习2 codevs
http://codevs.cn/problem/3138/ 题目描述 Description 给定一个栈(初始为空,元素类型为整数,且小于等于100),只有两个操作:入栈和出栈.先给出这些操作,请输 ...
- Python基础语法07--面向对象+正则表达式
Python 面向对象 Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的.本章节我们将详细介绍Python的面向对象编程. 如果你以前没有接触过 ...
- 转: memcached Java客户端spymemcached的一致性Hash算法
转自:http://colobu.com/2015/04/13/consistent-hash-algorithm-in-java-memcached-client/ memcached Java客户 ...
- 转:一个android开发者独立开发社交app全过程
http://www.cnblogs.com/linguanh/p/5683069.html
- NYOJ 1067 Compress String(区间dp)
Compress String 时间限制:2000 ms | 内存限制:65535 KB 难度:3 描写叙述 One day,a beautiful girl ask LYH to help he ...
- const *char p和char const *p
const *char p和char const *p,const char*p的区别 char*const p——p必须初始化,且不能指向别处,即p是指针常量: char const*p——p指向的 ...
- Tomcat配置,Myeclipse破解和各种设置
转自:http://www.cnblogs.com/tyjsjl/archive/2006/11/14/2156111.html 根据tomcat来配置eclipse和MyEclipse结合使用起来, ...
- Android GUI系统学习1:Gralloc
Gralloc模块是从Android Eclair(android 2.1)開始增加的一个HAL模块,Gralloc的含义为是Graphics Alloc(图形分配).他对上为libui提供服务,为其 ...
- Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! 排序,贪心
C. Hacker, pack your bags! It's well known that the best way to distract from something is to do ...