An army of n droids is lined up in one row. Each droid is described bym integers a1,?a2,?...,?am, whereai 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 hasm weapons, the i-th weapon can affect all the droids in the army by destroying one detail of thei-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 mostk shots. How many shots from the weapon of what type should R2-D2 make to destroy the sequence of consecutive droids of maximum length?
InputThe 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 containsm integers a1,?a2,?...,?am (0?≤?ai?≤?108), where ai is the number of details of thei-th type for the respective robot.
OutputPRint m space-separated integers, where thei-th number is the number of shots from the weapon of thei-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.
ExamplesInput5 2 44 01 22 10 21 3Output2 2Input3 2 41 21 32 2Output1 3NoteIn the first test the second, third and fourth droids will be destroyed.
In the second test the first and second droids will be destroyed.
題目大意:
一共有N個人,每個人有M個屬性值,當一個人的所有屬性值都小于等于0的時候,這個人就算被銷毀了。
我們每次操作可以選一種屬性值進行攻擊,使得所有人的這個屬性的值都-1.
我們最多可以進行K次操作,
問我們最多可以干掉多少個連續的人。
問這種時候的具體操作(每一種屬性用了多少次操作)。
思路:
1、比較經典的模型,對于連續的X個人,假如都將其干掉的時候,需要對于每種屬性使用的最少操作,就是對應這連續的X個人每種屬性的最大值。
2、那么問題轉化到區間最大值上來,這里我們可以使用RMQ來解,也可以用線段樹來解。
接下來我們可以考慮枚舉人數,然后O(NLogN)的去維護當前情況是否可行,直到枚舉到不可行為止前的那個答案,就是最終答案。
由此看來,枚舉人數是具有單調性的,要干掉更多的人,就需要更多的操作,那么我們可以二分這個人數。
對于可行方案,增加人數,不可行方案,減少人數。
3、二分過程中,維護最后一次可行解的答案,輸出即可。
Ac代碼:
#include<stdio.h>#include<string.h>#include<math.h>#include<iostream>using namespace std;int maxn[10][200005][20];int a[200060][10];int output[10];int n,m,k;void ST(){ for(int i=1;i<=m;i++) { for(int j=1;j<=n;j++) { maxn[i][j][0]=a[j][i]; } } int len=floor(log10(double(n))/log10(double(2))); for(int z=1;z<=m;z++) { for(int j=1;j<=len;j++) { for(int i=1;i<=n+1-(1<<j);i++) { maxn[z][i][j]=max(maxn[z][i][j-1],maxn[z][i+(1<<(j-1))][j-1]); } } }}int Slove(int mid){ int ans[10]; for(int i=1;i<=n;i++) { if(i+mid-1>n)break; else { int a=i;int b=i+mid-1; int len= floor(log10(double(b-a+1))/log10(double(2))); for(int z=1;z<=m;z++) { ans[z]=max(maxn[z][a][len], maxn[z][b-(1<<len)+1][len]); } int sum=0; for(int z=1;z<=m;z++) { sum+=ans[z]; } if(sum<=k) { for(int z=1;z<=m;z++) { output[z]=ans[z]; } return 1; } } } return 0;}int main(){ while(~scanf("%d%d%d",&n,&m,&k)) { for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { scanf("%d",&a[i][j]); } } ST(); int l=0; int r=n; while(r-l>=0) { int mid=(l+r)/2; if(Slove(mid)==1) { l=mid+1; } else r=mid-1; } for(int i=1;i<=m;i++) { printf("%d ",output[i]); } printf("/n"); }}
新聞熱點
疑難解答