标签:typedef 2022 int long 上海理工大学 cnt1 cnt0 GPLT define
比赛链接
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;
}
标签:typedef,2022,int,long,上海理工大学,cnt1,cnt0,GPLT,define 来源: https://www.cnblogs.com/zyyun/p/16064908.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。