Educational Codeforces Round 137 (Rated for Div. 2) - D. Problem with Random Tests
2024-09-08 19:29:33
期望 + 暴力
[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;
}
最新文章
- Windows Server 2012 磁盘管理之 简单卷、跨区卷、带区卷、镜像卷和RAID-5卷
- 深入解析SQL Server并行执行原理及实践(上)
- 解决oracle 中文入库是乱码的问题
- 【原】js检测移动端横竖屏
- [C#] AY.WPF-图形编程-高中生为起点-研究报告1
- IOCP 浅析
- Integer cache
- 学Lua(上)
- 动态网页技术---JSP
- Js设置所有连接是触发/swt/的代码
- django模板 实现奇偶分行
- 关于Webapp的注意事项
- 一个初学所了解的jquery事件
- Android -- onMeasure()源码分析
- 初次使用Python脚本,proto协议文件转Lua代码
- 手把手教你用.NET Core写爬虫
- 心路历程(一)-自学java两个月心得
- JS 中 原生方法 (三) --- Date 日期
- redis4.0 cluster搭建
- js 原型原型链
热门文章
- CTFshow——funnyrsa1的wp理解
- 网络监测工具之Zabbix的搭建与测试方法(三) ---Zabbix Agent
- Hello 2023 A-D
- 软件安装——tortoiseGit安装和配置
- [Unity]Update()与FixedUpdate()
- 刷题笔记——2181.信息学奥赛一本通T1005-地球人口承载力估计
- 《机器人SLAM导航核心技术与实战》第1季:第4章_机器人传感器
- liunx系统安装JDK环境详细步骤
- VBA中的(升降序)排名问题
- CSS-@规则(At-rules)常用语法使用总结