题解:P10806 [CEOI2024] 洒水器
题解区贪心题解看不明白啊。
Solution
先二分,最优性转可行性。判定是否可行。经典问题模型,左摆或右摆判断能否覆盖完整个区间。在经典模型中,每个花洒(或灯)的覆盖的长度可能不同。
这个经典问题的 dp 思路是,顺序考虑每个花洒(或灯),它向左扫能覆盖
但这道题简单之处在于,所有花洒的覆盖范围是相同的,也即二分的
那么思路就很明了了。记
复杂度
Code
#include<bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = a; i <= b; ++i)
#define per(i, a, b) for(int i = a; i >= b; --i)
const int maxn = 1e5 + 5;
int n, m, s[maxn], f[maxn];
int g[maxn], pre[maxn], nw[maxn], suf[maxn];
char Plan[maxn];
inline bool chck(int k){
rep(i, 1, n)
suf[i] = upper_bound(f + 1, f + m + 1, s[i] + k) - f - 1;
g[0] = 0;
rep(i, 1, n) g[i] = pre[i] = 0;
rep(i, 1, n){
if(g[i - 1] == m){ g[i] = g[i - 1], pre[i] = -2; continue;}
if(f[g[i - 1] + 1] < s[i] - k) return 0;
if(f[g[i - 1] + 1] < s[i]) g[i] = nw[i], pre[i] = -1;
else g[i] = suf[i], pre[i] = -2;
if(i > 1 and f[g[i - 2] + 1] >= s[i] - k){
if(suf[i - 1] > g[i])
g[i] = suf[i - 1], pre[i] = 1;
}
}
if(g[n] < m) return 0;
int x = n;
while(x){
if(pre[x] < 0){
if(pre[x] == -1) Plan[x] = 'L';
else Plan[x] = 'R';
--x;
} else{
Plan[x] = 'L', Plan[x - 1] = 'R';
--x; --x;
}
}
return 1;
}
int main(){
// freopen("P10806_24.in", "r", stdin);
ios_base::sync_with_stdio(0);
cin.tie(NULL), cout.tie(NULL);
cin >> n >> m;
rep(i, 1, n) cin >> s[i];
rep(i, 1, m) cin >> f[i];
if(n == 1){
if(s[1] <= f[1]){
cout << f[m] - s[1] << '\n';
cout << "R" << '\n';
} else if(s[1] >= f[m]){
cout << s[1] - f[1] << '\n';
cout << "L" << '\n';
} else cout << -1 << '\n';
return 0;
}
rep(i, 1, n)
nw[i] = upper_bound(f + 1, f + m + 1, s[i]) - f - 1;
int l = 0, r = 1e9, ans = -1;
while(l <= r){
int mid = l + r >> 1, tmp = chck(mid);
if(tmp) ans = mid, r = mid - 1;
else l = mid + 1;
}
cout << ans << '\n';
rep(i, 1, n) cout << Plan[i]; cout << '\n';
return 0;
}