Codeforces Round #291 (Div. 2) D. R2D2 and Droid Army [线段树+线性扫一遍]
An army of n droids is lined up in one row. Each droid is described by m integers a1, a2, ..., am, where ai is the number of details of the i-th type in this droid's mechanism. R2-D2 wants to destroy the sequence of consecutive droids of maximum length. He has m weapons, the i-th weapon can affect all the droids in the army by destroying one detail of the i-th type (if the droid doesn't have details of this type, nothing happens to it).
A droid is considered to be destroyed when all of its details are destroyed. R2-D2 can make at most k shots. How many shots from the weapon of what type should R2-D2 make to destroy the sequence of consecutive droids of maximum length?
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.
Next n lines follow describing the droids. Each line contains m integers a1, a2, ..., am (0 ≤ ai ≤ 108), where ai is the number of details of the i-th type for the respective robot.
Print m space-separated integers, where the i-th number is the number of shots from the weapon of the i-th type that the robot should make to destroy the subsequence of consecutive droids of the maximum length.
If there are multiple optimal solutions, print any of them.
It is not necessary to make exactly k shots, the number of shots can be less.
5 2 4
4 0
1 2
2 1
0 2
1 3
2 2
3 2 4
1 2
1 3
2 2
1 3
In the first test the second, third and fourth droids will be destroyed.
In the second test the first and second droids will be destroyed.
题解如标题,线段树+线性扫一遍即可~~~
9855946 | 2015-02-15 10:16:13 | njczy2010 | D - R2D2 and Droid Army | GNU C++ | Accepted | 186 ms | 27412 KB |
9855894 | 2015-02-15 10:10:30 | njczy2010 | D - R2D2 and Droid Army | GNU C++ | Wrong answer on test 6 | 15 ms | 27400 KB |
1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cstdio> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<map> 9 #include<set> 10 #include<stack> 11 #include<string> 12 13 #define N 100005 14 #define M 1505 15 //#define mod 10000007 16 //#define p 10000007 17 #define mod2 1000000000 18 #define ll long long 19 #define LL long long 20 #define eps 1e-6 21 //#define inf 2147483647 22 #define maxi(a,b) (a)>(b)? (a) : (b) 23 #define mini(a,b) (a)<(b)? (a) : (b) 24 25 using namespace std; 26 27 ll n,m,k; 28 ll ma; 29 ll ans[10]; 30 31 typedef struct 32 { 33 ll t[7]; 34 }PP; 35 PP a[N]; 36 PP tree[4*N]; 37 38 PP build(ll i,ll l,ll r) 39 { 40 //printf(" i=%I64d l=%I64d r=%I64d\n",i,l,r); 41 if(l==r){ 42 tree[i]=a[l]; 43 return tree[i]; 44 } 45 PP le,ri; 46 ll mid=(l+r)/2; 47 le=build(2*i,l,mid); 48 ri=build(2*i+1,mid+1,r); 49 ll j; 50 for(j=1;j<=m;j++){ 51 tree[i].t[j]=max(le.t[j],ri.t[j]); 52 } 53 return tree[i]; 54 } 55 56 PP query(ll i,ll l,ll r,ll L,ll R) 57 { 58 //printf(" i=%I64d l=%I64d r=%I64d L=%I64d R=%I64d\n",i,l,r,L,R); 59 if(l>=L && r<=R) return tree[i]; 60 ll mid; 61 mid=(l+r)/2; 62 PP le,ri,re; 63 ll j; 64 for(j=1;j<=m;j++){ 65 le.t[j]=ri.t[j]=0; 66 } 67 if(mid>=L){ 68 le=query(i*2,l,mid,L,R); 69 } 70 if(mid<R){ 71 ri=query(i*2+1,mid+1,r,L,R); 72 } 73 for(j=1;j<=m;j++){ 74 re.t[j]=max(le.t[j],ri.t[j]); 75 } 76 return re; 77 } 78 79 void ini() 80 { 81 memset(ans,0,sizeof(ans)); 82 ma=0; 83 ll i,j; 84 for(i=1;i<=n;i++){ 85 for(j=1;j<=m;j++){ 86 scanf("%I64d",&a[i].t[j]); 87 } 88 } 89 //printf(" bb\n"); 90 build(1,1,n); 91 } 92 93 void solve() 94 { 95 ll st,en; 96 st=1; 97 en=0; 98 ll now=0; 99 PP re; 100 ll j; 101 for(j=1;j<=m;j++){ 102 re.t[j]=0; 103 } 104 while(en<n) 105 { 106 en++; 107 now=0; 108 for(j=1;j<=m;j++){ 109 re.t[j]=max(re.t[j],a[en].t[j]); 110 now+=re.t[j]; 111 } 112 while(now>k){ 113 st++; 114 if(st>en){ 115 for(j=1;j<=m;j++){ 116 re.t[j]=0; 117 } 118 break; 119 } 120 re=query(1,1,n,st,en); 121 now=0; 122 for(j=1;j<=m;j++){ 123 now+=re.t[j]; 124 } 125 //printf(" st=%I64d en=%I64d now=%I64d\n",st,en,now); 126 } 127 if(now>k) continue; 128 // printf(" st=%I64d en=%I64d now=%I64d ma=%I64d\n",st,en,now,ma); 129 if(en-st+1>ma){ 130 ma=en-st+1; 131 for(j=1;j<=m;j++){ 132 ans[j]=re.t[j]; 133 } 134 } 135 } 136 } 137 138 void out() 139 { 140 printf("%I64d",ans[1]); 141 ll i; 142 for(i=2;i<=m;i++){ 143 printf(" %I64d",ans[i]); 144 } 145 printf("\n"); 146 } 147 148 int main() 149 { 150 //freopen("data.in","r",stdin); 151 //freopen("data.out","w",stdout); 152 //scanf("%d",&T); 153 //for(int ccnt=1;ccnt<=T;ccnt++) 154 //while(T--) 155 //scanf("%d%d",&n,&m); 156 while(scanf("%I64d%I64d%I64d",&n,&m,&k)!=EOF) 157 { 158 ini(); 159 solve(); 160 out(); 161 } 162 return 0; 163 }