1090. In the Army now (Ural 1090 Merge Sort | | Tree-like array)

Source: Internet
Author: User
Tags acos cmath

1090. In the Army nowtime limit:1.0 second
Memory limit:64 MB
The sergeant ordered that all the recruits stand in rows. The recruits has formed KRows with NPeople in each, but failed to stand according to their height. The right-stand in a row is as Following:the first soldier must be the highest, the second must being the second high EST and so on; The last soldier in a row must is the shortest. In order to teach the "people" to form rows, the sergeant ordered, the recruits jump as many times as There is recruits before him in the he row who is shorter than he. Note that there is no recruits of the same height. The sergeant wants to find which of the rows would jump the greatest total number of times in order to send this row to wor K in the kitchen. Help the sergeant-find this row. Inputthe first line of the input contains, positive integers Nand K(2≤ N≤10000, 1≤ K≤20). The following KLines contain NIntegers each. The recruits in each row is 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 was the number of the first recruit in a row and so on. Therefore a recruit jumps as many times as there is numbers which is greater than his number on the line before this num ber. Outputyou should output the number of the row in which the total amount of jumps are the greatest. If There is several rows with the maximal total amount of jumps you should output the minimal of their numbers. Sample
input Output
3 31 2 32 1 33 2 1
3


Test instructions: K row n column, to find out the reverse of each column, the output reverse of the minimum number of rows.

Idea: Use merge sort or tree-like array, these two days wrote several ways, want to practice familiar!

Code:

Merge sort # 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 RS On 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--) #defin E 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, s Izeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d", &a, &b) #define SFFF (A,B,C) scan F ("%d%d", &a, &b, &c) #define PF printf#define DBG pf ("hi\n") typedef long Long ll;using NA Mespace 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&LT;=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;}


Tree-like array # 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 RS On 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--) #defin E 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, s Izeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d", &a, &b) #define SFFF (A,B,C) scan F ("%d%d", &a, &b, &c) #define PF printf#define DBG pf ("hi\n") typedef long Long ll;using NA Mespace 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);                Be sure to remember to initialize 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;}


1090. In the Army now (Ural 1090 Merge Sort | | Tree-like array)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.