In the Army Now

Time limit: 1.0 second
Memory limit: 64 MB
The sergeant ordered that all the recruits stand in rows. The recruits have formed K rows with Npeople 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 output
3 3
1 2 3
2 1 3
3 2 1
3

分析:一行中前面数比后面数大的数的总个数,很明显的树状数组;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=1e4+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t,a[maxn],ma,now,ans;
void add(int x,int y)
{
for(int i=x;i<=n;i+=(i&(-i)))
a[i]+=y;
}
int get(int x)
{
int res=;
for(int i=x;i;i-=(i&(-i)))
res+=a[i];
return res;
}
int main()
{
int i,j;
ans=;
scanf("%d%d",&n,&k);
rep(i,,k)
{
memset(a,,sizeof a);
now=;
rep(j,,n)
{
scanf("%d",&t);
add(t,);
now+=get(n)-get(t);
}
if(ma<now)ma=now,ans=i;
}
printf("%d\n",ans);
//system("Pause");
return ;
}

最新文章

  1. jquery实现on/off开关按钮
  2. oracle行转列,decode 等用法
  3. Node 进阶:express 默认日志组件 morgan 从入门使用到源码剖析
  4. 【转载】关于Embedded Linux启动的经典问题
  5. Shell 总结
  6. 50道经典的JAVA编程题 (6-10)
  7. 实用Yii扩展
  8. Android与JS混编(js调用android相机)
  9. Zabbix3.0 客户端搭建
  10. MACOS关闭指定端口
  11. 关于top命令的使用
  12. Vue慕课网音乐项目随手记--node代理及数据抓取
  13. 性能优化4--Bitmap内存优化
  14. mysql数据导入导出与数据表优化
  15. CS50.3
  16. THUWC2019爆零记
  17. 查询删除安装rpm -qa | grep -i wrapname
  18. 爬楼梯 &#183; Climbing Stairs
  19. poj1204 Word Puzzles
  20. linux文本分析利器awk

热门文章

  1. find unique values in an array
  2. 算法:1!+(1!+3!)+(1!+3!+5!) + ( 1! + 3! + 5! + 7! + 9!)+....+(1!+3!+5!+ ... + m!)
  3. wpa_supplicant_8_ti hostapd wpa_supplicant TI 官方的wpa_supplicant hostapd 移植到linux
  4. FTP 服务器
  5. eclipse无法导入已有android项目
  6. tab奇偶行颜色交替+插件
  7. JSON文件存入MySQL数据库
  8. Git学习 -- 远程仓库
  9. HDU - 1083 Courses /POJ - 1469
  10. HDU 3552 I can do it!