期望 + 暴力

[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. NW js 打包入门教程

    NW js 打包入门教程 NW.JS的安装与打包_u013288292的博客-CSDN博客_nwjs打包

  2. 浅谈LCA问题(最近公共祖先)(四种做法)

    [模板]最近公共祖先(LCA) \(update \ 2023.1.3\) 新增了树链剖分 题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入格式 第一行包含三个正整数 \ ...

  3. SQL中常用函数操作

    --在SQL SERVER中批量替换字符串的方法 update [Table] set [Field] = REPLACE([Field],'被替换的原内容','要替换的内容') update HBb ...

  4. 2023牛客寒假算法基础集训营2 ABCDEFHJKL

    比赛链接 A 题解 知识点:数学. 用 \(n\) 减去区间1的端点得到匹配的一个区间,求一下与区间2的交集. 一个小公式,两区间 \([L_1,R_1]\) 和 \([L_2,R_2]\) 的交集长 ...

  5. 修改hosts文件需要vi命令

    i 在光标前插入. 保存 按esc后 shift+: 输入wq! 保存

  6. 反射概述-获取字节码Class对象的三种方式

    反射概述 判定结果∶*红色:失败*绿色:成功*一般我们会使用断言操作来处理结果*Assert.assertEquals(期望的结果,运算的结果);补充∶*Before:*修饰的方法会在测试方法之前被自 ...

  7. Add All&shuffle-sort(List)

    Add All&shuffle Collections常用功能 java.utils.collections是集合工具类,用来对集合进行操作.部分方法如下︰ public static < ...

  8. 创建型模式 - 简单工厂模式StaticFactoryMethod

    简单工厂模式的定义         创建型模式:         我们把被创建的对象称为产品,把创建产品的对象称为工厂.如果要创建的产品不多,只要一个工厂类就可以完成,这种模式叫简单工厂模式. 在简单 ...

  9. ffmpeg拉取rtsp视频流

    公司项目需要提供实时显示网络摄像头实时视频. void RTSPFFmpeg::rtsp_open(const char *url) { AVFormatContext* format_ctx = a ...

  10. 【随笔记】Tina 系统的 ADB、声卡、网卡、串口多路共存

    全志 Tina 系统的 USB Gadget 配置方法,随笔记录,实测为 R311 平台,其它平台应该通用.  一.配置内核 二.编译驱动并加载 # UAC insmod usb_f_uac1.ko ...