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