这个题目代码看了我半天。。
我们把终点关注在b数组, 我们先将b[ i ] 变成 h - b[ i ]并排好序, 对于一个a[ j ]来说如果它能和b[ i ]匹配, 那么它能和b[ k ], k < i, 匹配。
什么情况下能匹配成功呢, 就是b数组中 前 i 个数至少能和 len - i + 1, 个数匹配,这个随便想想就知道, 或者通过霍尔定理也能很快得到,
那么就能用线段树取维护了。
#include<bits/stdc++.h> #define LL long long #define LD long double #define ull unsigned long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #define PII pair<int, int> #define SZ(x) ((int)x.size()) #define ALL(x) (x).begin(), (x).end() #define fio ios::sync_with_stdio(false); cin.tie(0); using namespace std; const int N = 2e5 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 998244353; const double eps = 1e-8; const double PI = acos(-1); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;} template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;} template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;} template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} int n, len, h; int a[N], b[N], c[N]; struct SegmentTree { #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 int mx[N << 2], lazy[N << 2]; inline void pull(int rt) { mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]); } inline void push(int rt) { if(lazy[rt]) { mx[rt << 1] += lazy[rt]; mx[rt << 1 | 1] += lazy[rt]; lazy[rt << 1] += lazy[rt]; lazy[rt << 1 | 1] += lazy[rt]; lazy[rt] = 0; } } void build(int l, int r, int rt) { if(l == r) { mx[rt] = len - l + 1; return; } int mid = l + r >> 1; build(lson); build(rson); pull(rt); } void update(int L, int R, LL val, int l, int r, int rt) { if(R < l || r < L || R < L) return; if(L <= l && r <= R) { mx[rt] += val; lazy[rt] += val; return; } push(rt); int mid = l + r >> 1; update(L, R, val, lson); update(L, R, val, rson); pull(rt); } } Tree; int main() { scanf("%d%d%d", &n, &len, &h); for(int i = 1; i <= len; i++) { scanf("%d", &b[i]); b[i] = h - b[i]; } sort(b + 1, b + 1 + len); for(int i = 1; i <= n; i++) { scanf("%d", &a[i]); c[i] = upper_bound(b + 1, b + 1 + len, a[i]) - b - 1; } Tree.build(1, len, 1); for(int i = 1; i <= len; i++) Tree.update(1, c[i], -1, 1, len, 1); int ans = 0; for(int i = 1; i + len - 1 <= n; i++) { ans += Tree.mx[1] <= 0; Tree.update(1, c[i], 1, 1, len, 1); if(i + len <= n) Tree.update(1, c[i + len], -1, 1, len, 1); } printf("%d\n", ans); return 0; } /* */
原文地址:https://www.cnblogs.com/CJLHY/p/10877402.html
时间: 2024-10-15 13:04:10