Codeforces 514D R2D2 and Droid Army

Source: Internet
Author: User

D. R2D2 and Droid Army

An army ofNDroids is lined on one row. Each droid was described bymIntegers a1,? a 2,?...,? a m , where ai is the number of details of theI-th type in this droid ' s mechanism. R2-D2 wants to destroy the sequence of consecutive droids of maximum length. He hasmWeapons, theI-th weapon can affect all the droids in the army by destroying one detail of theI-th type (if the droid doesn ' t has the details of this type, nothing happens to it).

A Droid is considered to being destroyed when the all of it details are destroyed. R2-D2 can make at the most k shots. How many shots from the weapon of what type should r2-d2 do to destroy the sequence of consecutive droids of maximum Len Gth?

Input

The first line contains three integers n,? m,? k (1?≤? n? ≤?105 ,1?≤? m? ≤?5,0?≤? k. ≤?109 )-the number of droids, the number of detail types and the number of available shots, respectively.

NextNLines follow describing the droids. Each line containsmIntegers a1,? a 2,?...,? a m (0?≤? a i? ≤?108 ), where ai is the number of details of theI-th type for the respective robot.

Output

Print m space-separated integers, where the I-th number is the number of shots from the weapon of the i-th Type the robot should make to destroy the subsequence of consecutive of the droids length.

If there is multiple optimal solutions, print any of them.

It is not a necessary to make exactly K shots, the number of shots can was less.

Sample Test (s) input
5 2 44 01 22 10 21 3
Output
2 2
Input
3 2 41 21 32 2
Output
1 3
Note

In the first test the second, third and fourth droids would be destroyed.

In the second test the first and second droids'll be destroyed.

Test instructions: There are n robots, each robot has m logo (used to express the attribute), R2D2 This weapon can hit k times, every dozen, all the corresponding properties of the robot will be reduced by 1, ask at least a few times, can make the continuous robot properties all become 0 of the length of the largest, the number of shots per property output.

Idea: It is obvious that the problem is to use a line segment tree to record the maximum value of each attribute in a continuous region (that is, in this interval to all the properties of the robot into 0 required to hit the number of times), in order to find the longest continuous region, we need to use two points, to avoid timeouts.

The code is as follows:

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace STD; #define L (x) (x<<1) #define R (x) (x<<1|1) #define MID (x, y) (x+y) >>1const int Maxn=1e5+10;int A[MAXN ][6], ans[6], Temp[6];__int64 k;int m,n;struct Node{int left, right, val[6];} Rob[4*maxn];void Build (int l, int r, int rt) {Rob[rt].left=l;rob[rt].right=r;if (l==r) {for (int i=1; i<=m; i++) {Rob[rt] . Val[i]=a[l][i];} return;} int Mid=mid (L,R); Build (L, Mid, L (RT)); Build (Mid+1, R, R (RT)), for (int i=1; i<=m; i++) {Rob[rt].val[i]=max (rob[l (RT)].val[i], rob[r (RT)].val[i]);}} int Query (int l, int r, int rt, int x) {if (L==rob[rt].left && r==rob[rt].right) {return rob[rt].val[x];} int max=0;if (l<=rob[l (RT)].right) {if (r<=rob[l (RT)].right) {max=query (l,r,l (RT), x);} Else{max=query (L,rob[l (RT)].right, L (RT), x);}} if (R>=rob[r (RT)].left) {if (L>=rob[r (RT)].left) {max=query (L,r,r (RT), x);} else Max=max (Max, Query (Rob[r (RT)].left, R, R (RT), x));} return Max;} BOOL OK (int mID) {bool flag=false;for (int i=1; i<=n-mid+1; i++) {int num=0;for (int j=1; j<=m; J + +) {temp[j]=query (I, I+mid-1, 1, j) ; num+=temp[j];} if (num<=k) {flag=true;memcpy (ans,temp,sizeof (temp)); Fill (temp, temp+6, 0);} if (flag) return True;else return false;} int main () {while (scanf ("%d%d%i64d", &n, &m, &k)!=eof) {for (Int. I=1; i<=n; i++) {for (int j=1; j<=m; J + +) { scanf ("%d", &a[i][j]);}}    Build (1,n,1);    int le=1, ri=n;    while (le<=ri) {int mid= (LE+RI)/2; if (OK (mid)) {le=mid+1;}    else ri=mid-1;    }//printf ("%d", ans[1]);    for (int i=1; i<m; i++) {printf ("%d", ans[i]); } printf ("%d\n", Ans[m]);}  return 0;}


Codeforces 514D R2D2 and Droid Army

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.