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個(gè)人,每個(gè)人有M個(gè)屬性值,當(dāng)一個(gè)人的所有屬性值都小于等于0的時(shí)候,這個(gè)人就算被銷毀了。
我們每次操作可以選一種屬性值進(jìn)行攻擊,使得所有人的這個(gè)屬性的值都-1.
我們最多可以進(jìn)行K次操作,
問我們最多可以干掉多少個(gè)連續(xù)的人。
問這種時(shí)候的具體操作(每一種屬性用了多少次操作)。
思路:
1、比較經(jīng)典的模型,對(duì)于連續(xù)的X個(gè)人,假如都將其干掉的時(shí)候,需要對(duì)于每種屬性使用的最少操作,就是對(duì)應(yīng)這連續(xù)的X個(gè)人每種屬性的最大值。
2、那么問題轉(zhuǎn)化到區(qū)間最大值上來,這里我們可以使用RMQ來解,也可以用線段樹來解。
接下來我們可以考慮枚舉人數(shù),然后O(NLogN)的去維護(hù)當(dāng)前情況是否可行,直到枚舉到不可行為止前的那個(gè)答案,就是最終答案。
由此看來,枚舉人數(shù)是具有單調(diào)性的,要干掉更多的人,就需要更多的操作,那么我們可以二分這個(gè)人數(shù)。
對(duì)于可行方案,增加人數(shù),不可行方案,減少人數(shù)。
3、二分過程中,維護(hù)最后一次可行解的答案,輸出即可。
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"); }}
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注