Codeforces 873 B. Balanced Substring(前缀和 思维)

题目链接: Balanced Substring

题意:

  求一个只有1和0的字符串中1与0个数相同的子串的最大长度。

题解:

  我的解法是设1的权值是1,设0的权值是-1,求整个字符串的前缀和并记录每个前缀和出现的最后位置。因为两个相同的前缀和之间的子串一定符合条件,最后只用遍历一次,将每个前缀与和这个前缀值相同的位置之间的长度求出来就是符合条件的子串长度。但是这里一定要注意!在整个子串未开始遍历的时候这个子串的前缀和也是0。我就是在这个地方错了,这里给出错地方的数据。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int MAX_N = 1e5+9;
 4 const int INF = 1e5;
 5 char vec[MAX_N];
 6 int res[MAX_N],val[MAX_N*2];
 7 int N,M,t,num;
 8 void init()
 9 {
10     res[0] = 0;
11     memset(val,0,sizeof(val));
12 }
13 int main()
14 {
15     cin>>N;
16     scanf("%s",vec+1);
17     for(int i=0;i<=N;i++)
18     {
19         if(vec[i] == ‘0‘) res[i+1] = res[i] - 1;
20         else res[i+1] = res[i] + 1;
21         val[res[i+1] + INF] = i;
22     }
23     int ans = -1;
24     for(int i=1;i<=N+1;i++)
25     {
26         ans = max(ans,val[res[i]+INF] - i + 1);
27     }
28     cout<<ans<<endl;
29     return 0;
30 }
31
32 /*
33 18
34 011010101110111101
35
36 ans : 8
37 */

原文地址:https://www.cnblogs.com/doggod/p/8502853.html

时间: 2024-08-30 13:46:14

Codeforces 873 B. Balanced Substring(前缀和 思维)的相关文章

CF873B Balanced Substring (前缀和)

CF873B Balanced Substring (前缀和) 蛮有意思的一道题,不过还是.....................因为CF评测坏了,没有试过是否可过. 显然求\(\sum[i][0] - \sum[l][0] = \sum[i][1] - \sum[l][1]\) \(\sum[i][0] - \sum[l][1] = \sum[i][0] - \sum[l][0]\) 然后hash一下DP即可. #include <iostream> #include <cstdio

CodeForces - 873B Balanced Substring(思维)

inputstandard input outputstandard output You are given a string s consisting only of characters 0 and 1. A substring [l,?r] of s is a string slsl?+?1sl?+?2- sr, and its length equals to r?-?l?+?1. A substring is called balanced if the number of zero

Balanced Substring

You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s is a string slsl + 1sl + 2... sr, and its length equals to r - l + 1. A substring is called balanced if the number of zeroes (0) equals to the number of ones in t

Codeforces Round #546 (Div. 2) D 贪心 + 思维

https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则,假如u在v相邻前面,那么u和v可以交换位置,问你是队列最后一个人的时候你最前可以换到前面哪里 题解 因为相邻才能换,所以最后一个换到前面一定是一步一步向前走,所以不存在还要向后走的情况 设最后一个为u,假设前面有一个能和u换位置的集合,那么需要将这些点尽量往后移动去接u 假设前面有一个不能和u换位置的集合S,

CodeForces 1292A NEKO&#39;s Maze Game(思维)

1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <string> 5 #include <math.h> 6 #include <algorithm> 7 #include <vector> 8 #include <stack> 9 #include <queue> 10 #include <

Codeforces Round #400 C 前缀和,思维

ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals 题意:n个数,问有多少个区间的和是k的次方数,即sum([l, r])=k^x, x>=0. abs(k)<=10. tags:一开始O(n^2)统计,果然炸了.. 这题要在统计到第 i 个数时,看s[i]-k^x是否在前面出现过.因为k指数增长很快,这样就是O(n). // #400 #include<b

CF 1196D2 RGB Substring (hard version) --- 前缀和 + 思维

简单的情况不用前缀和,直接暴力就好,数据范围大的D2,就用前缀和数组存不满足三种情况的数,最后减一下取小就可以. 1 #include<bits/stdc++.h> 2 #define mem(a) memset(a,0,sizeof(a)) 3 #define ll long long 4 #define ld long double 5 #define ull unsigned long long 6 #define mp make_pair 7 #define pb push_back

Codeforces 1082C Multi-Subject Competition(前缀+思维)

题目链接:Multi-Subject Competition 题意:给定n名选手,每名选手都有唯一选择的科目si和对应的能力水平.并且给定科目数量为m.求选定若干个科目,并且每个科目参与选手数量相同的情况下的最大能力水平. 题解:每位选手扔到对应的科目里面,从1-m遍历科目,能力值排序下,维护下能力值和,大于0就给到当前位置人数答案加上该值,否则跳出(给负价值是没有意义的),最后遍历一遍人数对应的价值,拿最大的即可. 1 #include <vector> 2 #include <cst

codeforces 349B Color the Fence 贪心,思维

1.codeforces 349B    Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1-9每个字母分别要ai升油漆,问最多可画多大的数字. 贪心,也有点考思维. #include<bits/stdc++.h> using namespace std; #define LL long long #define INF 0x3f3f3f3f int main() { int v,a[1