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,或者为同一个值的数组时,可以使用稀疏数组来保存该数组. 稀疏数组的处理方法是: 记录数组一共有几行几列,有多少个不同的值 把具有不同值的元素的行列及值记 ...
随机推荐
- Codeforces 552E Vanya and Brackets(枚举 + 表达式计算)
题目链接 Vanya and Brackets 题目大意是给出一个只由1-9的数.乘号和加号组成的表达式,若要在这个表达式中加上一对括号,求加上括号的表达式的最大值. 我们发现,左括号的位置肯定是最左 ...
- Chrome常用URL命令(伪URL)
在Chrome地址栏输入chrome://chrome-urls/可以看到所有的Chrome支持的伪RUL 1.chrome://accessibility/ 可达性分析,默认是关闭的,点击acces ...
- gdb源码安装,指定使用的python版本
gdb调试python的时候,需要根据不同的python版本2.6.2.7.3.x安装相应的gdb: 如何指定关联的python版本? 下面gdb源码,解压后,进入目录: ./configure -h ...
- mysql修改删除列,删除有外键依赖的列
–重命名表rename table t_softwareport to software_port; –建立外键alter table software_port add constraint fk_ ...
- 微信小程序 项目实战(一)生命周期 配置服务器信息 splash启动页
步骤一:小程序 生命周期 //app.js App({ onLaunch: function () { //当小程序初始化完成时,会触发onLaunch(全局只触发一次) }, onShow: fun ...
- PHP读取excel(4)
这一小节内容主要是PHPExcel读取少量excel数据,具体代码如下: <?php //数据较少的时候,一次性读取出来放到数组里 header("Content-Type:text/ ...
- eclipse新建android项目出现非常多错误
如图所看到的: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvSGFycnlXZWFzbGV5/font/5a6L5L2T/fontsize/400/fil ...
- Django-权限信息初始化
数据库 from django.db import models class Menu(models.Model): """ 菜单组: """ ...
- 两个月刷完Leetcode前400题经验总结
更新:气死了,挂个傻逼: 每次做个分享.组织个活动,就会有一些傻逼冒泡生怕别人不知道他是傻逼,气死我了!自己好好看看非法集资的概念,我办这个活动,一分钱都没收,入群99元是督促大家完成刷题任务,最后完 ...
- 1449: [JSOI2009]球队收益
1449: [JSOI2009]球队收益 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 757 Solved: 437[Submit][Status][ ...