列表

详情


NC213128. 2020

描述

Bobo has a string of length n consisting of only digits 0, 1, and 2, and he wants to pick some *disjoint* subsequences which equal to 2020, as many as possible.

Formally, Bobo would like to find k quadrangle where

*
*
* for .

Find the maximum value of k.

输入描述

The input consists of several test cases terminated by end-of-file.

The first line of each test case contains an integer n and the second line contains a string .

*
*
* The sum of n does not exceed .

输出描述

For each test case, print an integer which denotes the result.

示例1

输入:

4
2222
7
2101210
8
22002200

输出:

0
1
2

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C(clang11) 解法, 执行用时: 13ms, 内存消耗: 392K, 提交时间: 2020-11-05 15:41:40

#include<stdio.h>
#define N 100010
#define EOF (-1)
int n,x,l2,l0,r2,r0;
char ch[100010];
int main()
{
    while(scanf("%d",&n)!=EOF){
        l2=l0=r2=r0=0;
        scanf("%s",ch);
        for(int i=0; i<n; i++){
            x=ch[i]-'0';
            l2+=(x==2);
            l0+=(x==0 && l0<l2);
            r2+=(x==2 && r2<(l2>>1) && r2<l0);
            r0+=(x==0 && r0<(l0>>1) && r0<r2);
        }
        printf("%d\n",r0);
    }
    return 0;
return 0;
}

C++(clang++11) 解法, 执行用时: 12ms, 内存消耗: 484K, 提交时间: 2020-11-07 23:22:00

#include <cstdio>
int n,t,dp[4];
char s[100001];
int main()
{
    while(t=dp[0]=dp[1]=dp[2]=dp[3]=0,~scanf("%d%s",&n,s))
    {
        for(int i=0;i<n;i++)
            if(s[i]=='2')dp[0]++,dp[2]+=dp[1]>dp[2]&&dp[0]/2>dp[2];
            else if(s[i]=='0')dp[1]+=dp[0]>dp[1],dp[3]+=dp[2]>dp[3]&&dp[1]/2>dp[3];
        printf("%d\n",dp[3]);
    }
    return 0;
}

上一题