编程语言
首页 > 编程语言> > 2022年中国高校计算机大赛-团队程序设计天梯赛(GPLT)上海理工大学校内选拔赛

2022年中国高校计算机大赛-团队程序设计天梯赛(GPLT)上海理工大学校内选拔赛

作者:互联网

比赛链接

2022年中国高校计算机大赛-团队程序设计天梯赛(GPLT)上海理工大学校内选拔赛

G.天气预报

给定一个01串,找到有多少个至少包含a个0,b个1的子串
image

解题思路

双指针

可以发现,对于一个位置来说,其往左满足至少有 \(a\) 个 \(0\) 且至少有 \(b\) 个 \(1\) 的位置 \(p\) 是确定的,且当前位置右移时,\(p\) 至少不会左移

代码

// 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