期望 + 暴力

[Problem - D - Codeforces](https://codeforces.com/contest/1743/problem/E)

题意

给出一个长度为 \(n\;(1<=n<=10^6)\) 的字符串 \(s\), 选取两个 \(s\) 的子串 \(a,b\), 使得 \(a\;or\;b\) 的值最大

保证 \(s\) 中每个位置出现 0 或 1 都是等概率的

思路

  1. 把 \(s\) 的前导 0 去掉,这些位置无法变成 1
  2. \(a=s\) 一定是最优的,\(b\) 的选取是尽量把 \(a\) 的前面的 0 给补上
  3. 设 idx 为 \(a\) 中第一个 \(0\) 的位置,\(b\) 的开头一定是在 idx 之前,这样才能补上 idx 这个位置上的 0
  4. 从 0 到 idx - 1 暴力枚举 b 的开头即可,因为每个位置出现 0/1 是等概率的,所以 idx 不会很大

代码

#include <bits/stdc++.h>
using namespace std;
#define endl "\n" typedef long long ll;
typedef pair<int, int> PII; string s;
int n, t, cnt;
const int N = 110;
int pos[N], d[N];
vector<int> vt; int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> s;
int idx = n;
for (int i = 0; i < n; i++)
{
if (s[i] == '1')
{
idx = i;
break;
}
}
if (idx == n)
{
cout << 0 << endl;
return 0;
}
s = s.substr(idx);
n = s.size();
for (int i = 0; i < n; i++)
{
if (s[i] == '0')
{
idx = i;
break;
}
}
string ans = s;
for (int i = 0; i < idx; i++)
{
string now = s;
for (int j = 0; idx + j < n; j++)
{
if (s[i + j] == '1')
now[idx + j] = '1';
}
ans = max(ans, now);
}
cout << ans << endl;
return 0;
}

Educational Codeforces Round 137 (Rated for Div. 2) - D. Problem with Random Tests的更多相关文章

  1. Educational Codeforces Round 137 (Rated for Div. 2) A-F

    比赛链接 A 题解 知识点:数学. \(4\) 位密码,由两个不同的数码组成,一共有 \(C_4^2\) 种方案.从 \(10-n\) 个数字选两个,有 \(C_{10-n}^2\) 种方案.结果为 ...

  2. 【Educational Codeforces Round 38 (Rated for Div. 2)】 Problem A-D 题解

    [比赛链接] 点击打开链接 [题解] Problem A Word Correction[字符串] 不用多说了吧,字符串的基本操作 Problem B  Run for your prize[贪心] ...

  3. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  4. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  5. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  6. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

  7. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

  8. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...

  9. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

  10. Educational Codeforces Round 39 (Rated for Div. 2) G

    Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...

随机推荐

  1. 【LGR-125】洛谷 11 月月赛 I & JROI-7 & JRKSJ-5

    P8846 『JROI-7』PMK 配匹串符字 简要题意 给出一正整数 \(n(1 \leq n \leq 10^5)\),求出一个由小写英文字母组成的字符串 \(S\),使得 \(|S|=n\) 且 ...

  2. 解决angular11打包报错Type 'Event' is missing the following properties from type 'any[]': ...Type 'Event' is not assignable to type 'string'

    出现这种情况,需要检查一下以下事项 1.ts类型声明和html里写的是否一致 1.1举例如下,子组件代码需要注意事项,子组件调用父组件方法,点击传参给父组件,在父组件触发一些时间,当前this指向是父 ...

  3. Java 进阶P-6.6+P-7.1

    接口设计模式 接口 接口是纯抽象类 所有的成员函数都是抽象函数 所有的成员变量都是public static final 接规定了长什么样,但是不管里面有什么 实现接口 类用extends,接口用im ...

  4. HelloWorld的常见问题与Notepad++使用-程序注释与Hello World说明

    HelloWorld的常见问题与Notepad++使用 编译和运行是两回事 编译:是指将我们编写的Java源文件翻译成JVM认识的class文件,在这个过程中, javac 编译器会检查我们 所写的程 ...

  5. vue学习笔记(一)---- vue指令(在vue中使用样式的方式)

    认真学习,认真记录,每天都要有进步呀!!! 加油叭!!! 一.在vue中使用样式的方式: 1. 直接传递一个数组,注意: 这里的 class 需要使用 v-bind 做数据绑定 <style&g ...

  6. KStudio-Java程序连接KingbaseES数据库异常

    错误信息: --KStudio客户端工具错误信息 The conncetion attempt failed.Reason:connect time out --Java应用程序控制台日志 Cause ...

  7. 一把梭:REST API 全用 POST(转载)

    一把梭:REST API 全用 POST(转载) 原文链接: https://coolshell.cn/articles/22173.html 写这篇文章的原因主要还是因为V2EX上的这个贴子,这个贴 ...

  8. FreeBSD 安装 fcitx5的配置

    link: Chinese Pinyin Package for typing Chinese sudo pkg install -y zh-CJKUnifonts sudo pkg install ...

  9. Vue.config.js配置 最新可用版本

    最近 在学前端,然后,学了这个vue-cli脚手架,虽然,我这个vue-cli还不算入门,后我会把这个笔记补上 下面是我的Vue.config.js的配置,我感觉这个复用的程度高,所以记下 了这个随笔 ...

  10. 力扣---2319. 判断矩阵是否是一个 X 矩阵

    如果一个正方形矩阵满足下述 全部 条件,则称之为一个 X 矩阵 :    矩阵对角线上的所有元素都 不是 0    矩阵中所有其他元素都是 0给你一个大小为 n x n 的二维整数数组 grid ,表 ...