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 < ...
随机推荐
- xxl-job定时调度任务Java代码分析
简介 用xxl-job做后台任务管理, 主要是快速解决定时任务的HA问题, 项目代码量不大, 功能精简, 没有特殊依赖. 因为产品中用到了这个项目, 上午花了点时间研究了一下运行机制. 把看到的记一下 ...
- JavaScript:对象:如何创建对象?
JS是面向对象的语言,除开基础数据类型,其他所有的数据类型都是对象,包括函数. 如何去理解对象,什么是对象呢? 举个例子,比如我们将日常生活中见到的猫这种动物,抽象成一个类Cat,这里不去谈类是什么概 ...
- 关于jQuery的操作
jQuery简介 简化了JS 类似于 后端 JDBC(操作数据库的基本API) dbutils(封装JDBC) xxx.jar 前端 JS ...
- shell端口监听异常邮箱告警
业务场景:应用发布监听服务是否正常启动,因为服务器资源不够上不了prometheus.grafana,所以写的shell脚本监听.此脚本适用于初创公司及小微企业使用. 准备工作 除了shell脚本这里 ...
- 腾讯云服务器CentOS 7.6安装基本中间件
腾讯云服务器CentOS 7.6安装基本中间件 摘要:由于最近开始学习Redis和Zookeeper了,因此使用云服务器的频率开始多了起来,并且开始了基础的安装教学,由于我之前确实没用过Linux ...
- 网络流棋盘模型 | P3355 骑士共存问题 P4304 [TJOI2013]攻击装置
题面(骑士共存问题) 在一个 \(n \times n\) 个方格的国际象棋棋盘上,马(骑士)可以攻击的棋盘方格如图所示.棋盘上某些方格设置了障碍,骑士不得进入. 对于给定的 \(n \times n ...
- 优化if...else...语句
写代码的时候经常遇到这样的场景:根据某个字段值来进行不同的逻辑处理.例如,不同的会员等级在购物时有不同的折扣力度.如果会员的等级很多,那么代码中与之相关的if...elseif...else...会特 ...
- 函数式编程思想概述-冗余的Runnable代码
函数式编程思想概述 在数学中,函数就是有输入量.输出星的一套计算方案,也就是"拿什么东西做什么事情".相对而言,面向对象过分强调"必须通过对象的形式来做事情", ...
- Containerd NRI 插件
Github:https://github.com/containerd/nri.git Slide:https://static.sched.com/hosted_files/kccncna2022 ...
- 【学习日志】volatile关键字的作用
消除指令重排序 保证了不同线程对变量进行操作时的可见性,cpu对变量值修改后,其他线程读取变量信息时从内存读取而非cpu缓存