获取中...

-

Just a minute...

2019牛客暑期多校训练营(第三场)

Crazy Binary String

题目思路:

定义1为+1,0为-1

扫一遍数组,计算出前缀和,并将每个数字出现的index记录下来

然后逆序往回找,想要区间0和1的个数相等,就要区间和为0

所以当我们找到一个区间前缀和和为n,只需要减去前缀和为n的区间

此时就找一下在他前面有没有前缀和为n的区间,刚才已经记录

题目代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <iomanip>
#include <string>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <utility>
#define LL long long
using namespace std;
const int MAX = 200010;//100000
const int MAX_1 = 100000;
const int INF = 0x3f3f3f3f;//1061109567,1e9,int-MAX2147483647;
const double EPS = 0.0000001;
const int MOD = 998244353;//998244353;
const double PI = acos(-1);
LL T,N,M;
/*-------------------------------------------------------------------------------------------*/
int sum[MAX];
vector<int> flag[MAX];
/* ------------------------------------------------------------------------------------------*/


int main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//freopen("input.in","r",stdin);
//freopen("output.out","w",stdout);
/* --------------------------------------------------------------------------------------*/
cin >> N;
char c;
int ans = 0;
int cnt0,cnt1;
cnt0 = cnt1 = 0;
for(int i = 0;i < N;i++)
{
cin >> c;
if(c == '0')
{
sum[i] = sum[i-1]- 1;
cnt0++;
flag[sum[i]+MAX_1].push_back(i);
}
else
{
cnt1++;
sum[i] = sum[i-1] + 1;
flag[sum[i]+MAX_1].push_back(i);
}

}
if(cnt0 == N || cnt1 == N)
{
cout << "0" << " 0" <<endl;
return 0;
}
for(int i = N-1;i >=0;i--)
{
int n;
if(sum[i] == 0)
{
ans = max(ans,i+1);
continue;
}
if(flag[sum[i]+MAX_1].size())
{
n = flag[sum[i]+MAX_1][0];
}
int m = i - n;
ans = max(m,ans);
if(ans == N) break;
}
cout << ans << " " << min(cnt0,cnt1)*2 <<endl;
return 0;
}
相关文章
评论
分享
  • NC-10

    NC-10
  • NC-9

    NC-9
  • NC-8

    NC-8
  • NC-7

    2019牛客暑期多校训练营(第七场)A.String题目思路:其实就是要求一个字符串,把原串的后缀拼到前面的时候,每一种后缀拼完字典序都比原串大 然后题目给你一个s,问你s最少能切割为多少个这样的字符串 可以发现,这样的串一定是0开头...

    NC-7
  • NC-6

    2019牛客暑期多校训练营(第六场)A.Garbage Classification就用map统计一下每种垃圾有多少个 然后在进行分类,看看每个类型的垃圾有多少个 最后就根据题意分类就好了 B.Shorten IPv6 Address...

    NC-6
  • NC-5

    2019牛客暑期多校训练营(第五场)A.digits 2输出N个N即可 B.generator 1题目思路:平常这个脑子会比比赛时候好用很多 比赛时候看到数据范围也想到了有没有十进制快速幂这种操作,但是没想到怎么实现 牛客这个测评鸡真...

    NC-5
  • NC-4

    2019牛客暑期多校训练营(第四场)A.meeting题目思路:看到就想到之前的换根dp,真的就可以 cfrerooting传送门 我们把他们要去的集合地点作为树根 然后dp[i]表示到达 i 节点需要的最长时间(树深度) 状态转移 ...

    NC-4
  • NC-2

    2019牛客暑期多校训练营(第二场)

    NC-2
  • NC-1

    NC-1
  • 线性动态规划

    一.最长上升(下降)子序列https://www.luogu.org/problemnew/show/P1091 https://www.luogu.org/problemnew/show/P1020 P1091:让每一个人做最中间人...

    线性动态规划
Please check the parameter of comment in config.yml of hexo-theme-Annie!