2022年中國高校計算機大賽-團隊程式設計天梯賽(GPLT)上海理工大學校內選拔賽
阿新 • • 發佈:2022-03-27
比賽連結
2022年中國高校計算機大賽-團隊程式設計天梯賽(GPLT)上海理工大學校內選拔賽
G.天氣預報
給定一個01串,找到有多少個至少包含a個0,b個1的子串
解題思路
雙指標
可以發現,對於一個位置來說,其往左滿足至少有 \(a\) 個 \(0\) 且至少有 \(b\) 個 \(1\) 的位置 \(p\) 是確定的,且當前位置右移時,\(p\) 至少不會左移
- 時間複雜度:\(O(n)\)
程式碼
// Problem: 天氣預報 // Contest: NowCoder // URL: https://ac.nowcoder.com/acm/contest/30532/G // Memory Limit: 524288 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org) // %%%Skyqwq #include <bits/stdc++.h> //#define int long long #define help {cin.tie(NULL); cout.tie(NULL);} #define pb push_back #define fi first #define se second #define mkp make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; } template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; } template <typename T> void inline read(T &x) { int f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar(); x *= f; } int n,a,b; LL res; int main() { cin>>n>>a>>b; string s; cin>>s; s=' '+s; for(int i=1,cnt0=0,cnt1=0,l=1;i<=n;i++) { if(s[i]=='0')cnt0++; else cnt1++; while(l<i) { if(s[l]=='0') { if(cnt0>a)l++,cnt0--; else break; } else { if(cnt1>b)l++,cnt1--; else break; } } if(cnt0>=a&&cnt1>=b)res+=l; } cout<<res+(a==0&&b==0); return 0; }