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. 在 .NET 4 中使用托管可扩展性框架构建可组合的应用程序

    https://msdn.microsoft.com/zh-cn/library/ff576068.aspx http://blogs.msdn.com/b/nblumhardt/archive/20 ...

  2. jenkins配置角色访问

    本文将介绍如何配置jenkins,使其可以支持基于角色的项目权限管理. 由于jenkins默认的权限管理体系不支持用户组或角色的配置,因此需要安装第三发插件来支持角色的配置,本文将使用Role Str ...

  3. fdisk添加磁盘

    1.            通过Fdisk查看系统分区详细信息: Fdisk –l   详解: [root@jetsenLin ~]# fdisk -l Disk /dev/sda: 10.7 GB, ...

  4. Struts中的数据处理的三种方式

    Struts中的数据处理的三种方式: public class DataAction extends ActionSupport{ @Override public String execute() ...

  5. 事务(JDBC、Hibernate、Spring)

    如果不用spring管理事务,我们自己写代码来操作事务.那么这个代码怎么写要看底层怎么访问数据库了. 当采用原生JDBC访问数据库时,操作事务需要使用java.sql.Connection的API.开 ...

  6. Unity3D项目优化(转)

    前言: 刚开始写这篇文章的时候选了一个很土的题目...<Unity3D优化全解析>.因为这是一篇临时起意才写的文章,而且陈述的都是既有的事实,因而给自己“文(dou)学(bi)”加工留下的 ...

  7. 拖拽加点ui吧

    有一段时间没写东西了,真的是静下心来不容易的事情. 把之前的代码再翻看下,想想要加点ui什么的. 2d  塔防ui之前 我看过,但放到3d上做,其实难度是很大的. 不过,这不算什么,关键是 合理的 布 ...

  8. java 文件字节输出流

    Example10_5.java import java.io.*; public class Example10_5 { public static void main(String args[]) ...

  9. HDU 5719 Arrange

    根据条件,某些位置的数字就可以确定了.确定过程中如果有冲突,则无解. 如果B中出现了递增,C中出现了递减,则无解. 对于每一个未确定的a[i],ans需要更新,ans=ans*((c[i]-b[i]+ ...

  10. sphinx multi valued filter

    publn_date is multi-valued <?php ini_set('memory_limit', '-1'); ini_set('max_execution_time', '10 ...