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 < ...
随机推荐
- 教你用JavaScript实现乘法游戏
案例介绍 欢迎来的我的小院,我是霍大侠,恭喜你今天又要进步一点点了!我们来用JavaScript编程实战案例,做一个乘法积分游戏.乘法游戏主要通过用户输入的数值和程序计算的数值进行对比,正确积一分,错 ...
- [python] Python map函数总结
Python map函数总结 本文主要介绍如何使用Python(Python3版本)的内置map()函数.简单来说map()函数会将指定的函数依次作用于某个序列的每个元素,并返回一个迭代器对象.map ...
- LeetCode-03 无重复字符的最长子串(Longest Substring Without Repeating Characters)
题目描述 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 &qu ...
- [Untiy]贪吃蛇大作战(三)——商店界面
游戏商店界面: 实际的效果图如下: 要实现这个滑动,首先我们需要,一个内容显示区域,一个内容滚动区域,如下图: 其中ItemContent挂载的组件如下: 红框标注的地方是右方的滑动块. 然后Item ...
- test20230111考试总结 -2023寒图论专题
前言 赛时得分情况: A B C D E F G H I \(\texttt{Total}\) \(\texttt{Rank}\) \(100\) \(100\) \(10\) \(58\) \(54 ...
- react系列-从0开始搭建一个react项目
从0开始搭建一个react项目 1.安装脚手架 npm install -g create-react-app 2.创建项目 D: //进入D盘 mkdir ReactDemo //创建ReactDe ...
- 记一次简单的诈骗网站Getshell
前言:在放假期间接到一个诈骗电话.然后说京东金条利率过高让我处理下(在疫情开放期间京东客服基本上是没有人工客服),然后就慢慢的被拉入钉钉会议,然后骗子给网站的时候发现域名不对就判定成了骗子就找理由有事 ...
- BUG日记之————>springboot使用QueryMapper多条件查询
使用MP的QueryMapper这里注意配置的Maven 注意这里的 .isEmpty 和hasText和hasLength不是一种类型
- springcloud-gateway整合jwt+jcasbin实现权限控制
jcasbin简介: jcasbin 是一个用 Java 语言打造的轻量级开源访问控制框架https://github.com/casbin/jcasbin,是casbin的Java语言版本.目前在 ...
- 浅谈Python中的with,可能有你不知道的
Python中的with,没那么简单,虽然也不难 https://docs.python.org/zh-cn/3.9/reference/compound_stmts.html#the-with-st ...