【CodeForces - 514D】R2D2 and Droid Army(二分+滑动窗口ST表,或 尺取+单调队列或STLmultiset)
題干:
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?
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.
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.
Output
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.
Examples
Input
5 2 4 4 0 1 2 2 1 0 2 1 3Output
2 2Input
3 2 4 1 2 1 3 2 2Output
1 3Note
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.
題目大意:
有?n?個機器人站成一排。每個機器人可以表示成?m?個整數?a1,?a2,?...,?am,其中?ai?是機器人的第?i?項特征值。 R2-D2 想要摧毀盡量多的連續的機器人。 他有?m?種武器, 第?i?種武器可以將所有機器人第?i?項特征值減少1。 (如果該機器人的特征值已經為0,則什么都不發生).
如果一個機器人所有特征值都為0,則認為該機器人被摧毀。R2-D2 可以用?k?次武器。問 R2-D2 最多可以摧毀多少連續的機器人?
Input
第一行包含三個整數?n,?m,?k?(1?≤?n?≤?105,?1?≤?m?≤?5,?0?≤?k?≤?109) — 分別表示機器人數,特征值數和可以動用武器的次數。
之后?n?行每行描述一個機器人。每行包含?m?個整數?a1,?a2,?...,?am?(0?≤?ai?≤?108),其中?ai是這個機器人的第?i?項特征值。
Output
輸出?m?個用空格分開的數,其中第?i?個數表示第?i?種武器使用的次數。
如果有多種方案,輸出任意一種。
可以使用少于?k?次射擊。
解題報告:
先二分,然后check的時候滑動窗口,剛開始是想拿個multiset維護窗口內元素,來尋找最大值,復雜度兩個log,T了,無奈,換成ST表,復雜度降到O(nmlogn),這題也可以直接尺取,復雜度O(nm)
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; ll n,m,k; int a[MAX][6]; int ans[6],tmp[6]; int dp[6][MAX][24],qq[MAX]; void init() {for(int k = 1; k<=m; k++) {for(int i = 1; i<=n; i++) dp[k][i][0] = a[i][k];for(int j = 1; (1<<j) <= n; j++) {for(int i = 1; i+(1<<j)-1 <= n; i++) {dp[k][i][j] = max(dp[k][i][j-1] , dp[k][i + (1<<(j-1))][j-1]);}}}for(int i = 1; i<=n; i++) {int k = 0;while(1<<(k+1) <= i) k++;qq[i] = k;} } int cal(int l,int r,int kk) {int k = qq[r-l+1];return max(dp[kk][l][k],dp[kk][r- (1<<k) + 1][k]); } bool ok(int len) {if(len == 0) return 1;ll tt=0;for(int i = 1; i+len-1<=n; i++) {tt=0;for(int j = 1; j<=m; j++) tmp[j] = cal(i,i+len-1,j),tt += tmp[j];if(tt <= k) return 1;} return 0; } int main() {cin>>n>>m>>k;for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {scanf("%d",&a[i][j]);}}init();int l = 0,r = n,mid,ans[6];while(l<=r) {mid = l+r>>1;if(ok(mid)) {for(int i = 1; i<=m; i++) ans[i] = tmp[i];l = mid+1;}else r = mid-1;}for(int i = 1; i<=m; i++) printf("%d ",ans[i]);return 0 ; } /* 1 1 0 0 */?
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的【CodeForces - 514D】R2D2 and Droid Army(二分+滑动窗口ST表,或 尺取+单调队列或STLmultiset)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电视画面天花板终于来了!索尼画谛A95K
- 下一篇: 全面自主可控!首个全链路国产操作系统Op