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. |
InputThe 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. |
OutputYou 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 Input3 3 1 2 3 2 1 3 3 2 1 分析:此题即求每一行的逆序对 找出逆序对最少的那一行编号 求逆序对可以采用归并排序 但另外一种更简单快捷的方法是使用树状数组(二叉索引树) 树状数组用于修改单点值的范围求和 点修改和范围求和均为O(logn) 其修改操作是从前往后修改 其求和操作是从前往前求和 数组表示的意义是根据下标与二倍数的关系的连续和 每次插入一个数 计算已插在其前面的数的数量 利用modify插数 再使用sum求数量 代码如下:
总结: 因为本题频繁使用各函数 所以利用inline可以减少部分时间 |
SOJ 2309: In the Army Now 树状数组求逆序对
最新推荐文章于 2019-06-15 14:01:39 发布