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 ;
}

ural1090 In the Army Now的更多相关文章

  1. poj 3069 Saruman's Army

    Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8477   Accepted: 4317 De ...

  2. poj3069 Saruman's Army

    http://poj.org/problem?id=3069 Saruman the White must lead his army along a straight path from Iseng ...

  3. R2D2 and Droid Army(多棵线段树)

    R2D2 and Droid Army time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. POJ 3069 Saruman's Army(萨鲁曼军)

    POJ 3069 Saruman's Army(萨鲁曼军) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] Saruman ...

  5. 【LCA】CodeForce #326 Div.2 E:Duff in the Army

    C. Duff in the Army Recently Duff has been a soldier in the army. Malek is her commander. Their coun ...

  6. URAL 1774 A - Barber of the Army of Mages 最大流

    A - Barber of the Army of MagesTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/v ...

  7. POJ 3069 Saruman's Army(贪心)

     Saruman's Army Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  8. 编程算法 - 萨鲁曼的军队(Saruman&#39;s Army) 代码(C)

    萨鲁曼的军队(Saruman's Army) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 直线上有N个点, 每个点, 其距离为R以内的区域里 ...

  9. Codeforces 514 D R2D2 and Droid Army(RMQ+二分法)

    An army of n droids is lined up in one row. Each droid is described by m integers a1, a2, ..., am, w ...

随机推荐

  1. 发现在看完objc基本语法之后,还是看Apple文档比较有用。

    现在已经停止找中文资料了,因为很多例子已经过时,运行不出来. 看完objc基本语法以后,Apple的资料也看得懂了. 还是应该跟着Apple的入门指南开始学,今后也应该以Apple的文档为主.

  2. CentOS7 PostgreSQL 主从配置( 一)

    主库配置 pg_hba.conf host replication all 10.2.0.0/0 trust postgresql.conf listen_addresses = '*' max_wa ...

  3. php过滤提交数据 防止sql注入攻击

    规则 1:绝不要信任外部数据或输入 关于 Web 应用程序安全性,必须认识到的第一件事是不应该信任外部数据.外部数据(outside data) 包括不是由程序员在 PHP 代码中直接输入的任何数据. ...

  4. 哈佛大学构建动态网站--第七讲ajax

    Ajax ajax举例: DOM的结构 通过js来修改html页面. Ajax的含义: return false的用途 跨浏览器的ajax 为什么不直接从yahoo获得数据呢? XMLHttpRequ ...

  5. 后台处理excel下载输出流

    前台 <ul class="navtop-right"> <li > <a href="/portal/trip/importExec&qu ...

  6. QQ登录界面

    @property (nonatomic,assign) IBOutlet UITextField *qq; @property (nonatomic,assign) IBOutlet UITextF ...

  7. think in uml 2.1

    业务建模

  8. libevent在windows下使用步骤详解

    一 环境 官方下载地址:http://libevent.org/版本:libevent-2.0.22-stable 二 编译静态库 1 解压把上面下载到libevent-2.0.22-stable.t ...

  9. windows ORA-12560: TNS: 协议适配器错误

    1.first it report ORA-12560: TNS: 协议适配器错误 手工设定环境变量如下: set ORACLE_HOME=d:\app\OAadmin\product\11.2.0\ ...

  10. php多进程实现

    php多进程实现 PHP有一组进程控制函数(编译时需要–enable-pcntl与posix扩展),使得php能在nginx系统中实现跟c一样的创建子进程.使用exec函数执行程序.处理信号等功能. ...