D. Problem with Random Tests

这篇具有很好参考价值的文章主要介绍了D. Problem with Random Tests。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Problem - 1743D - Codeforces

D. Problem with Random Tests,算法

D. Problem with Random Tests,算法 

思路:因为是或,所以答案一定会比原串更大,并且为了保留更多的1,我们可以选择原串作为其中一个串,另一个串则要找到第一个为0的位置,我们希望让这个为1,为了让这个位置在或之后为1,需要满足两个条件,假设这个位置为id,那么首先要满足长度至少为n-id+1,并且这个串的最高位还要是1,所以满足条件的起始位置一定在第一个1到第一个1之后的第一个0之前,而这之间有30个1的概率为1/2^(30)所以基本不可能,所以可以直接进行暴力

这个题数据随机是一个关键点,因为在数据随机的情况下,每个位置为1的概率为1/2,所以连续的1不会很长,那么就可以暴力的做

// Problem: D. Problem with Random Tests
// Contest: Codeforces - Educational Codeforces Round 137 (Rated for Div. 2)
// URL: https://codeforces.com/problemset/problem/1743/D
// Memory Limit: 512 MB
// Time Limit: 4000 ms

#include<iostream>
#include<cstring>
#include<string>
#include<sstream>
#include<bitset>
#include<deque>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<cassert>
#include<queue>
#include<map>
#include<stack>
#include<vector> 
#include<set>
#include<cstdlib>
#define fi first
#define se second
#define i128 __int128
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> PII;
typedef pair<int,pair<int,int> > PIII;
const double eps=1e-7;
const int N=1e6+7 ,M=5e5+7, INF=0x3f3f3f3f,mod=1e9+7,mod1=998244353;
const long long int llINF=0x3f3f3f3f3f3f3f3f;
inline ll read() {ll x=0,f=1;char c=getchar();while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
while(c>='0'&&c<='9') {x=(ll)x*10+c-'0';c=getchar();} return x*f;}
inline void write(ll x) {if(x < 0) {putchar('-'); x = -x;}if(x >= 10) write(x / 10);putchar(x % 10 + '0');}
inline void write(ll x,char ch) {write(x);putchar(ch);}
void stin() {freopen("in_put.txt","r",stdin);freopen("my_out_put.txt","w",stdout);}
bool cmp0(int a,int b) {return a>b;}
template<typename T> T gcd(T a,T b) {return b==0?a:gcd(b,a%b);}
template<typename T> T lcm(T a,T b) {return a*b/gcd(a,b);}
void hack() {printf("\n----------------------------------\n");}

int T,hackT;
int n,m,k;
string str;

void check(int id,int len,string &ans,string temp,int r) {
	string s1=temp.substr(id,len);
	string s2=ans.substr(r,len);
	string s3=temp.substr(r,len);
	
	for(int i=0;i<s1.size();i++) {
		if(s3[i]=='1') s1[i]='1';
	}
	
	if(s1>s2) {
		string p=ans.substr(0,temp.size()-len)+s1;
		ans=p;
	}
}

void solve() {
	n=read();
	cin>>str;
	
	int l=0;
	while(l<n&&str[l]=='0') l++;
	
	if(l>=n) printf("0\n");
	else {
		string ans=str.substr(l,n-l);
		int r=0;
		while(r<ans.size()&&ans[r]=='1') r++;

		if(r>=ans.size()) cout<<ans<<'\n';
		else {
			string temp=ans;
			int len=ans.size()-r;
			for(int i=0;i<r;i++) {
				check(i,len,ans,temp,r);
			}
			
			cout<<ans<<'\n';
		}
	}
}

int main() {
    // init();
    // stin();
	//ios::sync_with_stdio(false); 

    // scanf("%d",&T);
    T=1; 
    while(T--) hackT++,solve();
    
    return 0;       
}          

 文章来源地址https://www.toymoban.com/news/detail-650417.html

到了这里,关于D. Problem with Random Tests的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LeetCode //C - 138. Copy List with Random Pointer

    A linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list, or null. Construct a deep copy of the list. The deep copy should consist of exactly n brand new nodes, where each new node has its value set to the value of its corresponding original node. Both the next and random pointer

    2024年02月11日
    浏览(22)
  • Tree of Thoughts: Deliberate Problem Solving with Large Language Models

    本文是LLM系列的文章,针对《Tree of Thoughts: Deliberate Problem Solving with Large Language Models》的翻译。 语言模型越来越多地被部署用于解决各种任务中的一般问题,但在推理过程中仍然局限于token级别的从左到右的决策过程。这意味着他们可能无法完成需要探索、战略前瞻或初始决

    2024年02月11日
    浏览(36)
  • LeetCode 138. Copy List with Random Pointer【链表,DFS,迭代,哈希表】中等

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月09日
    浏览(30)
  • 【论文阅读】(2013)Exact algorithms for the bin packing problem with fragile objects

    论文来源:(2013)Exact algorithms for the bin packing problem with fragile objects 作者:Manuel A. Alba Martínez 等人 我们得到了一组物体,每个物体都具有重量和易碎性,以及大量没有容量的垃圾箱。 我们的目标是找到装满所有物体所需的最少垃圾箱数量,使每个垃圾箱中物体重量的总和小

    2024年02月11日
    浏览(33)
  • 开源项目运行时报错A problem was found with the configuration of task ‘:app:checkDebugManifest‘

    下载开源项目后,对gradle-wrapper.properties中的gradle版本进行了升级,造成了如下问题: 1: Task failed with an exception. ----------- * What went wrong: A problem was found with the configuration of task \\\':app:checkDebugManifest\\\' (type \\\'CheckManifest\\\').   - Type \\\'com.android.build.gradle.internal.tasks.CheckManifest\\\' property \\\'manif

    2023年04月08日
    浏览(25)
  • 已解决note: This error originates from a subprocess,and is likely not a problem with pip.

    已解决(pip安装第三方模块lxml模块报错)Building wheels for collected packages: lxml Building wheel for lxml (setup.py) … error error: subprocess-exited-with-error python setup.py bdist_wheel did not run successfully. note: This error originates from a subprocess,and is likely not a problem with pip. ERROR: Failed building wheel for lxml n

    2024年01月18日
    浏览(28)
  • pip安装库时报错:This error originates from a subprocess, and is likely not a problem with pip.

    前言 一 二、使用步骤 1.引入库 2.读入数据 总结   安装库时出现以下报错 查看要安装的库的版本: 我的python版本3.10所以安了mayavi的最高的版本  successful

    2024年02月11日
    浏览(38)
  • Problem P09. [算法课动态规划] 换硬币

    这将会是一个系统性的算法学习专栏,编程语言为C++,适用于刚开始学算法的学生和博友,建议需要的朋友收藏订阅,是免费的,希望对大家能够有所帮助。 动态规划当前状态和之前状态息息相关。比如这题:组成硬币,已经组成好的硬币x加上面值为y的硬币就可以组成好

    2024年02月22日
    浏览(25)
  • CRF算法(Conditional Random Fields)揭秘

    CRF基本介绍 在机器学习中,建模线性序列结构的方法,除了HMM算法,另一个重要的模型就是CRF。HMM为了降低模型复杂性,对 观测变量 做了独立假设(即 隐状态 之间有相关性,而 观测变量 之间没有相关性),这在某种程度上损害了模型的准确性;CRF弥补了这个缺陷,它同样假

    2024年02月22日
    浏览(27)
  • Random Walk算法详解(附python代码)

            一维随机游走问题:设一个质点(随机游走者)沿着一条直线运动,单位时间内只能运动一个单位长度,且只能停留在该直线上的整数点,假设在时刻t,该质点位于直线上的点i,那么在时刻t +1,该质点的位置有三种可能: ①以p 的概率跳到整数点i-1 ②或以q的概率

    2024年02月01日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包