期望 + 暴力

[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. jmeter 之修改报告取样间隔时间以及APDEX 区间设置

    1.取样间隔时间设置 在jmeter 生成的报告中取样间隔默认设置的是1分钟,而非1秒,故样本间的间隔为1分钟,如下图所示: 取样间隔时间可通过修改bin/user.properties配置文件实现自 ...

  2. 物联网 IOT 设备如何脱离信息孤岛?

    以下内容为本人的学习笔记,如需要转载,请声明原文链接微信公众号「englyf」https://mp.weixin.qq.com/s/tb5eOFNUZLtPPLipLAh3vA 本文大概 1435 个 ...

  3. Java8常见函数式接口总结

    函数式接口 函数式接口:有且仅有一个抽象方法的接口. 使用@FunctionalInterface注解来标记.如果接口不是函数式接口就会编译出错 满足条件的接口即使不加上注解,那也是函数式接口 函数式 ...

  4. [C#]关于逆变与协变的基本概念和修饰符in与out的意义

    协变与逆变的概念 假如两个类型X和Y具有特殊关系,X类型的每个值都能转换成Y类型.我们将I<X>向I<Y>的转换称为协变转换.反之我们将I<Y>向I<X> ...

  5. Git + Jenkins 自动化 NGINX 发布简易实现

    概述 之前基于 GitLab + Jenkins 实现了简单的 NGINX 的自动化发布. 具体包含如下的组件: GitLab 包括 GItLab 的 WebHook: Jenkins 及其插件: G ...

  6. IO多路复用完全解析

    上一篇文章以近乎啰嗦的方式详细描述了BIO与非阻塞IO的各种细节.如果各位还没有读过这篇文章,强烈建议先阅读一下,然后再来看本篇,因为逻辑关系是层层递进的. 1. 多路复用的诞生 非阻塞IO使用一个线 ...

  7. Random概述和基本使用-Random生成指定范围的随机数

    Random概述和基本使用 什么是Random类 此类的实例用于生成伪随机数. 例如,以下代码使用户能够得到一个随机数: Random r = new Random(); int i = r.next ...

  8. vulnhub靶场之DIGITALWORLD.LOCAL: VENGEANCE

    准备: 攻击机:虚拟机kali.本机win10. 靶机:digitalworld.local: VENGEANCE,下载地址:https://download.vulnhub.com/digitalw ...

  9. 如何在 C# 项目中链接一个文件夹下的所有文件

    在 C# 项目中通过链接方式引入文件可以让我们在项目中使用这些文件中的代码.常见的比如链接 AssemblyInfo.cs 文件,这样我们就可以在项目中使用这个文件中的版本号等信息.但是如果我们想要链 ...

  10. Jenkins CLI命令行

    Jenkins CLI命令行 jenkins不光可以UI操作还提供了命令行接口 位置 首页->系统管理->工具和动作->Jenkins 命令行接口 在这个界面下载一个jenkins- ...