题目链接:https://ac.nowcoder.com/acm/contest/883/B

题目大意

   给定一个长度为 N 的 01 字符串,输出最长子串和子序列的长度,满足其中 0 和 1 的个数相等。

分析1(DP)

  子序列就不说了,谁都会求。
  这个 DP 是我自己想的,严格来说复杂度是 O(n2),然而数据比较松,就过了,速度居然很快,主要注释都写代码里了。

代码如下

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  5. #define Rep(i,n) for (int i = 0; i < (n); ++i)
  6. #define For(i,s,t) for (int i = (s); i <= (t); ++i)
  7. #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
  8. #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
  9. #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
  10. #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
  11. #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
  12.  
  13. #define pr(x) cout << #x << " = " << x << " "
  14. #define prln(x) cout << #x << " = " << x << endl
  15.  
  16. #define LOWBIT(x) ((x)&(-x))
  17.  
  18. #define ALL(x) x.begin(),x.end()
  19. #define INS(x) inserter(x,x.begin())
  20. #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
  21. #define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // ?? x ?????? c
  22. #define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
  23. #define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper);
  24.  
  25. #define ms0(a) memset(a,0,sizeof(a))
  26. #define msI(a) memset(a,inf,sizeof(a))
  27. #define msM(a) memset(a,-1,sizeof(a))
  28.  
  29. #define MP make_pair
  30. #define PB push_back
  31. #define ft first
  32. #define sd second
  33.  
  34. template<typename T1, typename T2>
  35. istream &operator>>(istream &in, pair<T1, T2> &p) {
  36. in >> p.first >> p.second;
  37. return in;
  38. }
  39.  
  40. template<typename T>
  41. istream &operator>>(istream &in, vector<T> &v) {
  42. for (auto &x: v)
  43. in >> x;
  44. return in;
  45. }
  46.  
  47. template<typename T>
  48. ostream &operator<<(ostream &out, vector<T> &v) {
  49. Rep(i, v.size()) out << v[i] << " \n"[i == v.size()];
  50. return out;
  51. }
  52.  
  53. template<typename T1, typename T2>
  54. ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
  55. out << "[" << p.first << ", " << p.second << "]" << "\n";
  56. return out;
  57. }
  58.  
  59. inline int gc(){
  60. static const int BUF = 1e7;
  61. static char buf[BUF], *bg = buf + BUF, *ed = bg;
  62.  
  63. if(bg == ed) fread(bg = buf, , BUF, stdin);
  64. return *bg++;
  65. }
  66.  
  67. inline int ri(){
  68. int x = , f = , c = gc();
  69. for(; c<||c>; f = c=='-'?-:f, c=gc());
  70. for(; c>&&c<; x = x* + c - , c=gc());
  71. return x*f;
  72. }
  73.  
  74. template<class T>
  75. inline string toString(T x) {
  76. ostringstream sout;
  77. sout << x;
  78. return sout.str();
  79. }
  80.  
  81. inline int toInt(string s) {
  82. int v;
  83. istringstream sin(s);
  84. sin >> v;
  85. return v;
  86. }
  87.  
  88. //min <= aim <= max
  89. template<typename T>
  90. inline bool BETWEEN(const T aim, const T min, const T max) {
  91. return min <= aim && aim <= max;
  92. }
  93.  
  94. typedef long long LL;
  95. typedef unsigned long long uLL;
  96. typedef pair< double, double > PDD;
  97. typedef pair< int, int > PII;
  98. typedef pair< int, PII > PIPII;
  99. typedef pair< string, int > PSI;
  100. typedef pair< int, PSI > PIPSI;
  101. typedef set< int > SI;
  102. typedef set< PII > SPII;
  103. typedef vector< int > VI;
  104. typedef vector< double > VD;
  105. typedef vector< VI > VVI;
  106. typedef vector< SI > VSI;
  107. typedef vector< PII > VPII;
  108. typedef map< int, int > MII;
  109. typedef map< int, string > MIS;
  110. typedef map< int, PII > MIPII;
  111. typedef map< PII, int > MPIII;
  112. typedef map< string, int > MSI;
  113. typedef map< string, string > MSS;
  114. typedef map< PII, string > MPIIS;
  115. typedef map< PII, PII > MPIIPII;
  116. typedef multimap< int, int > MMII;
  117. typedef multimap< string, int > MMSI;
  118. //typedef unordered_map< int, int > uMII;
  119. typedef pair< LL, LL > PLL;
  120. typedef vector< LL > VL;
  121. typedef vector< VL > VVL;
  122. typedef priority_queue< int > PQIMax;
  123. typedef priority_queue< int, VI, greater< int > > PQIMin;
  124. const double EPS = 1e-;
  125. const LL inf = 0x7fffffff;
  126. const LL infLL = 0x7fffffffffffffffLL;
  127. const LL mod = 1e9 + ;
  128. const int maxN = 1e5 + ;
  129. const LL ONE = ;
  130. const LL evenBits = 0xaaaaaaaaaaaaaaaa;
  131. const LL oddBits = 0x5555555555555555;
  132.  
  133. // dp[i][0] 表示以 a[i] 结尾 0 的个数比 1 的个数多一个的最长子串长度
  134. // dp[i][1] 表示以 a[i] 结尾 0 的个数与 1 的个数相等的最长子串长度
  135. // dp[i][2] 表示以 a[i] 结尾 0 的个数比 1 的个数少一个的最长子串长度
  136. int N, A, B, dp[maxN][], a[maxN];
  137.  
  138. int main(){
  139. //freopen("MyOutput.txt","w",stdout);
  140. //freopen("input.txt","r",stdin);
  141. //INIT();
  142. scanf("%d", &N);
  143. For(i, , N) scanf("%1d", &a[i]);
  144.  
  145. // 枚举以 a[i] 结尾的子串
  146. For(i, , N) {
  147. int x = , y = ;
  148. if(a[i]) ++B;
  149. else swap(x, y);
  150.  
  151. if(dp[i - ][x]) {
  152. int tmp = i - dp[i - ][x] - ;
  153.  
  154. if(dp[tmp][x]) dp[i][x] = dp[i - ][x] + dp[tmp][x] + ;// dp[tmp][x] > dp[i - 1][x]的情况
  155. else {
  156. // dp[tmp][x] <= dp[i - 1][x]的情况
  157. int cnt = ;
  158. rFor(j, i, i - dp[i - ][x]) {
  159. a[j] ? ++cnt : --cnt;
  160. if(cnt == x - ) dp[i][x] = i - j + ;
  161. }
  162. }
  163.  
  164. dp[i][] = dp[i - ][x] + ;
  165. }
  166. dp[i][y] = dp[i - ][] + ;
  167.  
  168. A = max(A, dp[i][]);
  169. }
  170.  
  171. B = min(B, N - B) << ;
  172. printf("%d %d\n", A, B);
  173. return ;
  174. }

分析2(正解)

  对于子串,我们可以把 0 看成 -1,求前缀和 preSum,如果满足 preSum[i] == preSum[j],那么 [i + 1, j] 这个区间上的子串一定满足条件,用个 map 记录一下每个和最早出现的位置即可,注意特判 preSum[i] == 0 的情况。

代码如下

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  5. #define Rep(i,n) for (int i = 0; i < (n); ++i)
  6. #define For(i,s,t) for (int i = (s); i <= (t); ++i)
  7. #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
  8. #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
  9. #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
  10. #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
  11. #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
  12.  
  13. #define pr(x) cout << #x << " = " << x << " "
  14. #define prln(x) cout << #x << " = " << x << endl
  15.  
  16. #define LOWBIT(x) ((x)&(-x))
  17.  
  18. #define ALL(x) x.begin(),x.end()
  19. #define INS(x) inserter(x,x.begin())
  20. #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
  21. #define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // ?? x ?????? c
  22. #define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
  23. #define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper);
  24.  
  25. #define ms0(a) memset(a,0,sizeof(a))
  26. #define msI(a) memset(a,inf,sizeof(a))
  27. #define msM(a) memset(a,-1,sizeof(a))
  28.  
  29. #define MP make_pair
  30. #define PB push_back
  31. #define ft first
  32. #define sd second
  33.  
  34. template<typename T1, typename T2>
  35. istream &operator>>(istream &in, pair<T1, T2> &p) {
  36. in >> p.first >> p.second;
  37. return in;
  38. }
  39.  
  40. template<typename T>
  41. istream &operator>>(istream &in, vector<T> &v) {
  42. for (auto &x: v)
  43. in >> x;
  44. return in;
  45. }
  46.  
  47. template<typename T>
  48. ostream &operator<<(ostream &out, vector<T> &v) {
  49. Rep(i, v.size()) out << v[i] << " \n"[i == v.size()];
  50. return out;
  51. }
  52.  
  53. template<typename T1, typename T2>
  54. ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
  55. out << "[" << p.first << ", " << p.second << "]" << "\n";
  56. return out;
  57. }
  58.  
  59. inline int gc(){
  60. static const int BUF = 1e7;
  61. static char buf[BUF], *bg = buf + BUF, *ed = bg;
  62.  
  63. if(bg == ed) fread(bg = buf, , BUF, stdin);
  64. return *bg++;
  65. }
  66.  
  67. inline int ri(){
  68. int x = , f = , c = gc();
  69. for(; c<||c>; f = c=='-'?-:f, c=gc());
  70. for(; c>&&c<; x = x* + c - , c=gc());
  71. return x*f;
  72. }
  73.  
  74. template<class T>
  75. inline string toString(T x) {
  76. ostringstream sout;
  77. sout << x;
  78. return sout.str();
  79. }
  80.  
  81. inline int toInt(string s) {
  82. int v;
  83. istringstream sin(s);
  84. sin >> v;
  85. return v;
  86. }
  87.  
  88. //min <= aim <= max
  89. template<typename T>
  90. inline bool BETWEEN(const T aim, const T min, const T max) {
  91. return min <= aim && aim <= max;
  92. }
  93.  
  94. typedef long long LL;
  95. typedef unsigned long long uLL;
  96. typedef pair< double, double > PDD;
  97. typedef pair< int, int > PII;
  98. typedef pair< int, PII > PIPII;
  99. typedef pair< string, int > PSI;
  100. typedef pair< int, PSI > PIPSI;
  101. typedef set< int > SI;
  102. typedef set< PII > SPII;
  103. typedef vector< int > VI;
  104. typedef vector< double > VD;
  105. typedef vector< VI > VVI;
  106. typedef vector< SI > VSI;
  107. typedef vector< PII > VPII;
  108. typedef map< int, int > MII;
  109. typedef map< int, string > MIS;
  110. typedef map< int, PII > MIPII;
  111. typedef map< PII, int > MPIII;
  112. typedef map< string, int > MSI;
  113. typedef map< string, string > MSS;
  114. typedef map< PII, string > MPIIS;
  115. typedef map< PII, PII > MPIIPII;
  116. typedef multimap< int, int > MMII;
  117. typedef multimap< string, int > MMSI;
  118. //typedef unordered_map< int, int > uMII;
  119. typedef pair< LL, LL > PLL;
  120. typedef vector< LL > VL;
  121. typedef vector< VL > VVL;
  122. typedef priority_queue< int > PQIMax;
  123. typedef priority_queue< int, VI, greater< int > > PQIMin;
  124. const double EPS = 1e-;
  125. const LL inf = 0x7fffffff;
  126. const LL infLL = 0x7fffffffffffffffLL;
  127. const LL mod = 1e9 + ;
  128. const int maxN = 1e5 + ;
  129. const LL ONE = ;
  130. const LL evenBits = 0xaaaaaaaaaaaaaaaa;
  131. const LL oddBits = 0x5555555555555555;
  132.  
  133. int N, A, B, cnt1, cnt0, sum;
  134. MII s;
  135.  
  136. int main(){
  137. //freopen("MyOutput.txt","w",stdout);
  138. //freopen("input.txt","r",stdin);
  139. //INIT();
  140. scanf("%d", &N);
  141. For(i, , N) {
  142. int x;
  143. scanf("%1d", &x);
  144. x ? ++cnt1 : ++cnt0;
  145. sum += (x << ) - ;
  146. if(sum == ) A = max(A, i);
  147. if(s[sum]) A = max(A, i - s[sum]);
  148. else s[sum] = i;
  149. }
  150.  
  151. B = min(cnt1, cnt0) << ;
  152. printf("%d %d\n", A, B);
  153. return ;
  154. }

2019 牛客多校第三场 B Crazy Binary String的更多相关文章

  1. 2019牛客多校第三场B Crazy Binary String 思维

    Crazy Binary String 思维 题意 给出01串,给出定义:一个串里面0和1的个数相同,求 满足定义的最长子序列和子串 分析 子序列好求,就是0 1个数,字串需要思考一下.实在没有思路可 ...

  2. 2019牛客多校第三场 F.Planting Trees

    题目链接 题目链接 题解 题面上面很明显的提示了需要严格\(O(n^3)\)的算法. 先考虑一个过不了的做法,枚举右下角的\((x,y)\),然后二分矩形面积,枚举其中一边,则复杂度是\(O(n^3 ...

  3. 2019牛客多校第三场D BigInteger——基础数论

    题意: 用  $A(n)$ 表示第 $n$ 个只由1组成分整数,现给定一个素数 $p$,求满足 $1 \leq i\leq n, 1 \leq j \leq m, A(i^j) \equiv 0(mo ...

  4. [2019牛客多校第三场][G. Removing Stones]

    题目链接:https://ac.nowcoder.com/acm/contest/883/G 题目大意:有\(n\)堆石头,每堆有\(a_i\)个,每次可以选其中两堆非零的石堆,各取走一个石子,当所有 ...

  5. [题解]Magic Line-计算几何(2019牛客多校第三场H题)

    题目链接:https://ac.nowcoder.com/acm/contest/883/H 题意: 给你偶数个点的坐标,找出一条直线将这n个点分成数量相等的两部分 并在这条直线上取不同的两个点,表示 ...

  6. [题解]Crazy Binary String-前缀和(2019牛客多校第三场B题)

    题目链接:https://ac.nowcoder.com/acm/contest/883/B 题意: 给你一段长度为n,且只有 ‘0’ 和 ‘1’ 组成的字符串 a[0,...,n-1].求子串中 ‘ ...

  7. 2019牛客多校第三场A Graph Games 分块思想

    题意:给你一张无向图,设s(x)为与x直接相连的点的集合,题目中有两种操作: 1:1 l r 将读入的边的序列中第l个到第r个翻转状态(有这条边 -> 没这条边, 没这条边 -> 有这条边 ...

  8. 启发式分治:2019牛客多校第三场 G题 Removing Stones

    问题可以转换为求有多少个区间数字的总和除2向下取整大于等于最大值.或者解释为有多少个区间数字的总和大于等于最大值的两倍(但是若区间数字总和为奇数,需要算作减1) 启发式分治: 首先按最大值位置分治,遍 ...

  9. 2019牛客多校第三场B-Crazy Binary String(前缀和+思维)

    Crazy Binary String 题目传送门 解题思路 把1记为1,把0记为-1,然后求前缀和,前缀和相等的就说明中间的01数一样.只要记录前缀和数值出现的位置即可更新出答案. 代码如下 #in ...

随机推荐

  1. 逆向思维——cf1241D

    /* 给定一个序列a,每次可以把值为x的所有元素放到a的首部或尾部,问将a变为lis的最少操作步数 对原序列离散化后重新打标记, 可以反着来考虑这个问题:即固定连续的元素值为[l,r]的点不动,那么剩 ...

  2. JAVA并发工具类---------------(CountDownLatch和CyclicBarrier)

    CountDownLatch是什么 CountDownLatch,英文翻译为倒计时锁存器,是一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 闭锁可以延迟线程的进 ...

  3. Java-Class-C:com.ylbtech.api.platfrom.util.RedisUtils.class

    ylbtech-Java-Class-C:com.ylbtech.api.platfrom.util.RedisUtils.class 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶 ...

  4. LeetCode 复制带随机指针的链表

    题目链接:https://leetcode-cn.com/problems/copy-list-with-random-pointer/ 题目大意 略. 分析 空间复杂度 O(1) 的做法非常开拓思维 ...

  5. Javascript 面向对象之继承

    本文参考书籍<<Javascript高级程序设计>> js继承方式:实现继承,主要依靠原型链实现. 原型链:基本思想:利用原型让一个引用类型继承另一个引用类型的属性和方法. 这 ...

  6. JasperReport环境设置

    JasperReport是一个纯Java库,而不是一个独立的应用程序.它不能单独运行,因此它需要被嵌入到另一个客户端或服务器端的Java应用程序.因为它是基于Java,它可以在任何支持Java的平台( ...

  7. 将某个Qt4项目升级到Qt5遇到的问题

    本文转载自http://hi.baidu.com/xchinux/item/9044d8ce986accbb0d0a7b87 一.将某个QT4项目改成QT5遇到的问题 该Qt4项目以前是使用Qt4.7 ...

  8. nfs下的exportfs命令和nfs客户端重新挂载

    工作中,如果使用了nfs服务器,会遇到修改nfs服务器配置的情况,如果想重新让客户端加载上修改后的配置,但是又不能重启rpcbind服务,我们需要使用export命令了 exportfs命令 常用选项 ...

  9. c# winForm DotNetBar控件之SuperGridControl

    1.添加表头 sgc.PrimaryGrid.SelectionGranularity = SelectionGranularity.Row;//点击选中一行 DevComponents.DotNet ...

  10. 【单调队列优化】[CF372C] Watching Fireworks is Fun

    突然发现我可能单调队列都打不来了...我太菜了... 这道题显然有$$f[i][j]=min\{f[i-1][k]+\vert j-a[i] \vert\}$$ 则$ans=\sum_{i=1}^{m ...