SOJ 2309: In the Army Now 树状数组求逆序对

原创 2014年10月01日 11:24:48
Time Limit:1000ms Memory Limit:32768KB

Description

The sergeant ordered that all the recruits stand in rows. The recruits have
formed K rows with N people in each, but failed to stand according to their
height. The right way to stand in a row is as following: the first soldier must
be the highest, the second must be the second highest and so on; the last soldier
in a row must be the shortest. In order to teach the young people how to form
rows, the sergeant ordered that each of the recruits jump as many times as there
are recruits before him in his row who are shorter than he. Note that there are
no two recruits of the same height.
The sergeant wants to find which of the rows will jump the greatest total number
of times in order to send this row to work in the kitchen. Help the sergeant to
find this row.

Input

The first line of the input contains two positive integers N and K (2 ≤ N ≤
10000, 1 ≤ K ≤ 20). The following K lines contain N integers each. The recruits
in each row are numbered according to their height (1 — the highest, N — the
shortest). Each line shows the order in which the recruits stand in the
corresponding row. The first integer in a line is the number of the first recruit
in a row and so on. Therefore a recruit jumps as many times as there are numbers
which are greater than his number in the line before this number.

Output

You should output the number of the row in which the total amount of jumps is the
greatest. If there are several rows with the maximal total amount of jumps you
should output the minimal of their numbers.

Sample Input

3 3
1 2 3
2 1 3
3 2 1
 
分析:此题即求每一行的逆序对 找出逆序对最少的那一行编号
求逆序对可以采用归并排序 但另外一种更简单快捷的方法是使用树状数组(二叉索引树)
树状数组用于修改单点值的范围求和
点修改和范围求和均为O(logn)
其修改操作是从前往后修改
其求和操作是从前往前求和
数组表示的意义是根据下标与二倍数的关系的连续和
每次插入一个数 计算已插在其前面的数的数量
利用modify插数 再使用sum求数量
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const int maxN=10000;
const int maxK=20;
const int inf=0xcfcfcfcf;



int C[maxN+5];

int n,k;


inline int sum(int x)
{
	int ret=0;
	while(x>0)
	{
		ret+=C[x];
		x-=x&(-x);
	}
	return ret;
}

inline void modify(int x,int d)
{
	while(x<=n)
	{
		C[x]+=d;
		x+=x&(-x);
	}
}


int main()
{
	int i,j,num;
	int MaxSum,Mindex,temp;
	while(scanf("%d%d",&n,&k)==2)
	{	

		MaxSum=inf;
		Mindex=1;

		for (j=1;j<=k;j++)
		{
		
			memset(C,0,sizeof(C));
			num=0;
			for (i=0;i<n;i++)
			{
				scanf("%d",&temp);	
				modify(temp,1);
				num+=i-sum(temp);
			}
	
			if (num>MaxSum)
			{
				MaxSum=num;
				Mindex=j;
			}

		}
		printf("%d\n",Mindex);
	}



	return 0;
}

 
总结:
因为本题频繁使用各函数 所以利用inline可以减少部分时间
 

URAL - In the Army Now(归并排序求逆序数对)

点击打开题目链接 1090. In the Army Now Time limit: 1.0 second Memory limit: 64 MB The serg...
  • l1832876815
  • l1832876815
  • 2017年11月30日 21:37
  • 33

SOJ 题目分类

-----------------------------最优化问题-------------------------------------  ----------------------常规动态...
  • lh842824672
  • lh842824672
  • 2013年09月25日 11:43
  • 1448

POJ 1836 Alignment(DP max(最长上升子序列 + 最长下降子序列))

Alignment Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14486   Acc...
  • yeguxin
  • yeguxin
  • 2015年08月10日 08:53
  • 705

SOJ 射击游戏递推解法

题目描述 小明很喜欢玩射击游戏。这周末,他完成了数据结构作业之后,又来到了射击娱乐场。他从老板那租了一把步枪和装有N发子弹的弹夹。注意:所有的子弹都从枪口上膛。在射击的过程中,小明每次都有两...
  • mo_hui_tou
  • mo_hui_tou
  • 2018年01月05日 16:46
  • 74

soj 1034 Forest_求树的深度和宽度

求树的深度和宽度
  • neng18
  • neng18
  • 2014年05月02日 20:00
  • 858

【poj 2892】Tunnel Warfare 中文题意&题解&代码

Tunnel WarfareTime Limit: 1000MS Memory Limit: 131072K Total Submissions: 7577 Accepted: 3...
  • WilliamCode
  • WilliamCode
  • 2016年04月06日 10:12
  • 201

1poj1836(和合唱队列一样)

http://poj.org/problem?id=1836 Alignment Time Limit: 1000MS Memory Limit: 30000K Total Submis...
  • lanjiangzhou
  • lanjiangzhou
  • 2013年05月30日 10:55
  • 1191

记录AndroidStudio一些简单操作

一、取消android studio与svn、git的关联(git、svn等事先要配置好)      1.在日常android开发中,我们可能会碰到这么一个场景:将本地的代码share到svn的时...
  • dengyaoning
  • dengyaoning
  • 2016年03月10日 09:54
  • 979

Android Studio 教程(干货)

常见设置 a)在Setting中,修改主题、修改工程目录的字体大小。  b)在Setting中,显示行号:  c)设置注释模板,File–>Other Setting –> ...
  • gaojinshan
  • gaojinshan
  • 2015年12月08日 14:24
  • 3919

开启GitHub模式,now!

(原文地址:) 开启GitHub模式,now! 最近看到了一篇文章,该文章的作者将自己连续177天在github上commit东西的经历和经验分享了出来,我觉得这样挺有意思,所以我...
  • karotte
  • karotte
  • 2014年06月16日 00:43
  • 607
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:SOJ 2309: In the Army Now 树状数组求逆序对
举报原因:
原因补充:

(最多只允许输入30个字)