1090. In the Army Now (Ural 1090 归并排序||树状数组)

1090. 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


题意:k行n列,求出每一列的逆序对,输出逆序对最少的所在的行数。

思路:采用归并排序或者树状数组,这两天写了好几道了,要练熟悉!

代码:

//归并排序
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 10005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

int n,k,sum;
int a[maxn];
int b[maxn];

void Merge(int a[],int l,int mid,int r)
{
    int i,j,k=l;
    FRE(i,l,r) b[i]=a[i];
    i=l;j=mid+1;
    while (i<=mid&&j<=r)
    {
        if (b[i]<=b[j])
            a[k++]=b[i++];
        else{
            sum+=(mid-i+1);
            a[k++]=b[j++];
        }
    }
    while (i<=mid) a[k++]=b[i++];
    while (j<=r) a[k++]=b[j++];
}

void Merge_sort(int a[],int l,int r)
{
    if (l>=r) return ;
    int mid=(l+r)>>1;
    Merge_sort(a,l,mid);
    Merge_sort(a,mid+1,r);
    Merge(a,l,mid,r);
}

int main()
{
    int i,j;
    while (~sff(n,k))
    {
        int maxx=0;
        int ans=1;
        FRE(i,1,k)
        {
            sum=0;
            FRE(j,1,n)
                sf(a[j]);
            Merge_sort(a,1,n);
            if (sum>maxx)
            {
                maxx=sum;
                ans=i;
            }
        }
        pf("%d\n",ans);
    }
    return 0;
}


//树状数组
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 10005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

struct Node
{
    int val,pos;
}node[maxn];

int n,k,ans,s;
int bit[maxn];

int cmp(Node a,Node b)
{
    return a.val>b.val;
}

int sum(int i)
{
    int s=0;
    while (i>0)
    {
        s+=bit[i];
        i-=i&-i;
    }
    return s;
}

void add(int i,int x)
{
    while (i<=n)
    {
        bit[i]+=x;
        i+=i&-i;
    }
}

int main()
{
    int i,j;
    while (~sff(n,k))
    {
        ans=1;
        int maxx=-1;
        FRE(i,1,k)
        {
            s=0;
            mem(bit,0); //一定要记住初始化
            FRE(j,1,n)
            {
                sf(node[j].val);
                node[j].pos=j;
            }
            sort(node+1,node+n+1,cmp);
//            DBG;
            FRE(j,1,n)
            {
//                DBG;
                s+=sum(node[j].pos-1);
                add(node[j].pos,1);
            }
            if (s>maxx)
            {
                maxx=s;
                ans=i;
            }
//            DBG;
        }
        pf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用代码解决这个问题The program committee of the school programming contests, which are often held at the Ural State University, is a big, joyful, and united team. In fact, they are so united that the time spent together at the university is not enough for them, so they often visit each other at their homes. In addition, they are quite athletic and like walking. Once the guardian of the traditions of the sports programming at the Ural State University decided that the members of the program committee spent too much time walking from home to home. They could have spent that time inventing and preparing new problems instead. To prove that, he wanted to calculate the average distance that the members of the program committee walked when they visited each other. The guardian took a map of Yekaterinburg, marked the houses of all the members of the program committee there, and wrote down their coordinates. However, there were so many coordinates that he wasn't able to solve that problem and asked for your help. The city of Yekaterinburg is a rectangle with the sides parallel to the coordinate axes. All the streets stretch from east to west or from north to south through the whole city, from one end to the other. The house of each member of the program committee is located strictly at the intersection of two orthogonal streets. It is known that all the members of the program committee walk only along the streets, because it is more pleasant to walk on sidewalks than on small courtyard paths. Of course, when walking from one house to another, they always choose the shortest way. All the members of the program committee visit each other equally often. Input The first line contains the number n of members of the program committee (2 ≤ n ≤ 105). The i-th of the following n lines contains space-separated coordinates xi, yi of the house of the i-th member of the program committee (1 ≤ xi, yi ≤ 106). All coordinates are integers. Output Output the average distance, rounded down to an integer, that a member of the program committee walks from his house to the house of his colleague.
最新发布
05-26

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值