Educational Codeforces Round 137 (Rated for Div. 2) - D. Problem with Random Tests
期望 + 暴力
[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 都是等概率的
思路
- 把 \(s\) 的前导 0 去掉,这些位置无法变成 1
- \(a=s\) 一定是最优的,\(b\) 的选取是尽量把 \(a\) 的前面的 0 给补上
- 设 idx 为 \(a\) 中第一个 \(0\) 的位置,\(b\) 的开头一定是在 idx 之前,这样才能补上 idx 这个位置上的 0
- 从 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的更多相关文章
- Educational Codeforces Round 137 (Rated for Div. 2) A-F
比赛链接 A 题解 知识点:数学. \(4\) 位密码,由两个不同的数码组成,一共有 \(C_4^2\) 种方案.从 \(10-n\) 个数字选两个,有 \(C_{10-n}^2\) 种方案.结果为 ...
- 【Educational Codeforces Round 38 (Rated for Div. 2)】 Problem A-D 题解
[比赛链接] 点击打开链接 [题解] Problem A Word Correction[字符串] 不用多说了吧,字符串的基本操作 Problem B Run for your prize[贪心] ...
- 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 ...
- 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 ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
- 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 ...
- 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 ...
- Educational Codeforces Round 63 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
- 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 < ...
随机推荐
- 正则提取器和beanshell处理器组合,将提取的所有id拼接成字符串
1.添加正则表达式,提取所有id值 2.添加beanshell处理器将所有的id值拼接成字符串 方法一: int N = Integer.parseInt(vars.get("build_m ...
- 【运维笔录】局域网实现项目的CI/CD,Docker+Gitea+Drone搭建轻量级自动化部署平台
一.安装Docker 1.1 安装excpet $ apt-get -f install expect 1.2 编写执行Docker安装脚本 编写docker_dep.exp脚本 #!/usr/bin ...
- 如何优雅地升级一个Creator 2.x 项目到 3.6.2 ?
最近,我将之前用 Cocos Creator 2.x 写的一个微信小游戏<球球要回家>移植到了 Cocos Creator 3.6.2 上. 编程语言也从 JavaScript 迁移到了 ...
- windows系统批量转换CRLF和LF格式代码,解决eslint报错Delete `␍`解决'unix2dos' is not recognized as an internal or external command
每天都要开心(▽)哇: 最近写代码,工程配置为lf格式,但是拉取代码后,代码都变成了CRLF,于是满屏幕报错 在做跨平台开发移植的时候,最常见的问题就是不同操作系统的换行不同(例如,Windows 上 ...
- Sundial(一)
Sundial 源码梳理 - v2.5.6 代码目录一览 通过入口点说明 实现IServiceCollection,并返回IServiceCollection(Extensions/ScheduleS ...
- 最容易懂的策略模式消除if-else分支,实现开闭原则,提高可扩展性
1 介绍 策略模式最常用的场景就是用于消除代码中的if-else,这里所说的if-else并不是说任何简单的判断都引入策略模式来优化,这样反而会增加代码的复杂度. 反例:使用策略模式对一个boolea ...
- 数字游戏(NOIP 2003 PJT2)
一 原始数据处理 1.输入数据得到a[1]~a[n],复制扩展a[n+1]~a[2*n],以便处理不同点为起点出发. cin>>n>>m; for(int i=1;i<= ...
- linux09-分区挂载
1.Linux分区简要介绍 Linux来说无论有几个分区,分给哪一目录使用,它归根结底就只有一个根目录,一个独立且唯一的文件结构 , Linux中每个分区都是用来组成整个文件系统的一部分. Linux ...
- Snipaste下载安装(使用教程)
## Snipaste下载安装(使用教程)**一 简单介绍** Snipaste 是一个免费简单但强大的截图工具,也可以让你将截图贴回到屏幕上!下载并打开 Snipaste,按下 F1 来开始截图,再 ...
- 分布式机器学习:异步SGD和Hogwild!算法(Pytorch)
1 导引 我们在博客<分布式机器学习:同步并行SGD算法的实现与复杂度分析(PySpark)>和博客<分布式机器学习:模型平均MA与弹性平均EASGD(PySpark) >中介 ...