Codeforces Round 988 (Div. 3) E题解析
E题
题目链接
题目描述

题目的思路
根据题目的意思,我们可以推断出算法时间复杂度应该在O(N)
对于这道题而言,我们可以分析下思路
首先我们先从1~n的范围里面询问答案
如果出现0就跳过,因为无序操作
如果出现非0答案temp就记录下1~i的01序列的个数
如果我询问出来的答案都是0的话,说明我的序列肯定全是0,或者全是1,这样是无法确定的,所以我们可以输出“IMPOSSIBLE”
否则我们就可以构造满足非零答案temp的目标子串
我们可以先放i-temp-1个1,再放temp个0,再放1个1,这个1可以和前面的temp个0组成temp个01序列
然后再对i+1~n的范围进行询问答案,询问出来的答案标记为now,用temp来表示上一次询问的答案
这里以正序询问出来的答案为例
如果询问出来的答案大于上一次询问出来的答案,就说明需要增加,就向ans加“1",这个操作会和前面的0结合成01序列。
如果询问出来的答案不变,那么就说明不需要增加,向ans后面加“0”,这个1无论和前面的“0”或者“1”都不会组成01序列。
最后输出即可
AC代码
逆序查询(判断答案的逻辑和正序相反,因为构造出来的答案是反的)
点击查看代码
// Problem: E. Kachina's Favorite Binary String
// Contest: Codeforces - Codeforces Round 988 (Div. 3)
// URL: https://codeforces.com/contest/2037/problem/E#
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define debug1(a) cout << #a << '=' << a << endl;
#define debug2(a, b) cout << #a << " = " << a << " " << #b << " = " << b << endl;
#define debug3(a, b, c) cout << #a << " = " << a << " " << #b << " = " << b << " " << #c << " = " << c << endl;
#define debug4(a, b, c, d) cout << #a << " = " << a << " " << #b << " = " << b << " " << #c << " = " << c << " " << #d << " = " << d << endl;
#define debug5(a, b, c, d, e) cout << #a << " = " << a << " " << #b << " = " << b << " " << #c << " = " << c << " " << #d << " = " << d << " " << #e << " = " << e << endl;
#define vec(a) \
for (int i = 0; i < a.size(); i++) \
cout << a[i] << ' '; \
cout << endl;
#define darr(a, _i, _n) \
cout << #a << ':'; \
for (int ij = _i; ij <= _n; ij++) \
cout << a[ij] << ' '; \
cout << endl;
#define endl "\n"
#define fi first
#define se second
#define caseT \
int T; \
cin >> T; \
while (T--)
// #define int long long
// #define int __int128
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 99999999;
// const int N = ;
void solve()
{
int n;
cin>>n;
int s1=0,s0=0;
int temp;
string s;
for(int i=n-1;i>=1;i--)
{
cout<<"? "<<i<<" "<<n<<endl;
cin>>temp;
if(!temp)continue;
else{
s1=temp;
s0=n-i-temp;
break;
}
}
if(!s1)
{
cout<<"! IMPOSSIBLE"<<endl;
return;
}
for(int i=1;i<=s0;i++)s+="0";
for(int i=1;i<=s1;i++)s+="1";
s+="0";
//这样构造就保证了第一个询问出来不为0的答案是temp
for(int i=n-s1-s0-1;i>=1;i--)
{
cout<<"? "<<i<<" "<<n<<endl;
int now;
cin>>now;
if(now>temp)s+="0";
else s+="1";
temp=now;
}
reverse(s.begin(),s.end());
cout<<"! "<<s<<endl;
}
signed main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
// cout.tie(0);
//caseT
int t;
cin>>t;
while(t--)
{
solve();
}
return 0;
}
/*
*/
正序查询
点击查看代码
// Problem: E. Kachina's Favorite Binary String
// Contest: Codeforces - Codeforces Round 988 (Div. 3)
// URL: https://codeforces.com/contest/2037/problem/E#
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define debug1(a) cout << #a << '=' << a << endl;
#define debug2(a, b) cout << #a << " = " << a << " " << #b << " = " << b << endl;
#define debug3(a, b, c) cout << #a << " = " << a << " " << #b << " = " << b << " " << #c << " = " << c << endl;
#define debug4(a, b, c, d) cout << #a << " = " << a << " " << #b << " = " << b << " " << #c << " = " << c << " " << #d << " = " << d << endl;
#define debug5(a, b, c, d, e) cout << #a << " = " << a << " " << #b << " = " << b << " " << #c << " = " << c << " " << #d << " = " << d << " " << #e << " = " << e << endl;
#define vec(a) \
for (int i = 0; i < a.size(); i++) \
cout << a[i] << ' '; \
cout << endl;
#define darr(a, _i, _n) \
cout << #a << ':'; \
for (int ij = _i; ij <= _n; ij++) \
cout << a[ij] << ' '; \
cout << endl;
#define endl "\n"
#define fi first
#define se second
#define caseT \
int T; \
cin >> T; \
while (T--)
// #define int long long
// #define int __int128
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 99999999;
// const int N = ;
void solve()
{
int n;
cin>>n;
int temp=0;//记录的是上一次的查询结果
int p=0;//标记的是否发生改变
vector<int>ans(n+1);
for(int i=2;i<=n;i++)
{
cout<<"? "<<1<<" "<<i<<endl;
int x;
cin>>x;
if(x&&!p)//如果查询出来的答案大于0且没有改变过
{
for(int j=1;j<i-x;j++)ans[j]=1;
for(int j=i-x;j<i;j++)ans[j]=0;
ans[i]=1;
p=1;
}else{
if(x==temp)ans[i]=0;
else ans[i]=1;
}
temp=x;
}
cout<<"! ";
if(!p)cout<<"IMPOSSIBLE"<<endl;
else{
for(int i=1;i<=n;i++)cout<<ans[i];
cout<<endl;
}
}
signed main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
// cout.tie(0);
//caseT
int t;
cin>>t;
while(t--)
{
solve();
}
return 0;
}
/*
*/
Codeforces Round 988 (Div. 3) E题解析的更多相关文章
- Codeforces Round #378 (Div. 2) D题(data structure)解题报告
题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...
- Codeforces Round #612 (Div. 2) 前四题题解
这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...
- Codeforces Round #713 (Div. 3)AB题
Codeforces Round #713 (Div. 3) Editorial 记录一下自己写的前二题本人比较菜 A. Spy Detected! You are given an array a ...
- Codeforces Round #833 (Div. 2)补题
Codeforces Round #833 (Div. 2) D. ConstructOR 知识点:高位和对低位无影响 一开始以为和广州的M一样,是数位dp,后来发现只要找到一个就行 果然无论什么时候 ...
- Codeforces Round #552 (Div. 3) A题
题目网址:http://codeforces.com/contest/1154/problem/ 题目意思:就是给你四个数,这四个数是a+b,a+c,b+c,a+b+c,次序未知要反求出a,b,c,d ...
- Codeforces Round #412 Div. 2 补题 D. Dynamic Problem Scoring
D. Dynamic Problem Scoring time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
- Codeforces Round #425 (Div. 2))——A题&&B题&&D题
A. Sasha and Sticks 题目链接:http://codeforces.com/contest/832/problem/A 题目意思:n个棍,双方每次取k个,取得多次数的人获胜,Sash ...
- Codeforces Round #579 (Div. 3) 套题 题解
A. Circle of Students 题目:https://codeforces.com/contest/1203/problem/A 题意:一堆人坐成一个环,问能否按逆时针或者顺时针 ...
- Codeforces Round #786 (Div. 3) 补题记录
小结: A,B,F 切,C 没写 1ll 对照样例才发现,E,G 对照样例过,D 对照样例+看了其他人代码(主要急于看后面的题,能调出来的但偷懒了. CF1674A Number Transforma ...
随机推荐
- Win11如何找回熟悉的开始菜单、任务栏和右键菜单
背景 公司政策满3年可以换新电脑,前段时间申请了下,到手后发现是Win11系统,配置翻倍,欣然接受,把一些常用的软件都安装上,但是,用了一段时间后,发现右键刷新要点击2次,开始菜单找东西也完全靠搜索, ...
- csdn 下载券恶心之处
今天在csdn碰到一个恶心事,啥事呢?下载券.详细的说,就是人家码友把下载积分都设置成0了,让大家自行下载.结果,却不行,非得搞个下载券,得去做任务,给它的广告爹爹们点点任务才能获取下载券的code. ...
- Seata 1.3.0 ERROR i.s.c.r.n.NettyClientChannelManager -no available service 'null' found, please make sure registry config correct
根据个人经验,报这个错误是因为nacos里并没有同步seata的config导致的 配置文档:https://www.bookstack.cn/read/seata-1.3.0/4b2f4de4831 ...
- SPSS25.0中文破解版安装教程及使用教程
目录 第一步,下载链接: 下载并解压,管理员身份运行SPSS 25 64bit.exe: 第二步,安装过程一路默认,安装路径可以改变,然后等待安装完成即可: 第三步,安装完成后,立即启动SPPS; 第 ...
- 【YashanDB知识库】ycm托管数据库时报错OM host ip:127.0.0.1 is not support join to YCM
问题现象 托管数据库时检查报错OM的IP是127.0.0.1,不支持托管到YCM OM 问题的风险及影响 导致数据库无法托管监控 问题影响的版本 问题发生原因 安装数据库时修改了OM的监听ip为127 ...
- 学习笔记:robots.txt文件
1.1 介绍 robots.txt文件是一种用于指导搜索引擎爬虫在网站上哪些页面可以被抓取,哪些页面不应该被抓取的文本文件.这个文件通常放置在网站的根目录下. 1.2 由来 robots.txt标准最 ...
- Node.js开发博客项目笔记-搭建环境(2)
搭建环境 首先新建blog-1文件夹,在文件夹下初始化package.json,执行命令: npm init -y 生成的package.json文件中的main属性默认值index.js改成bin/ ...
- Go runtime 调度器精讲(四):运行 main goroutine
原创文章,欢迎转载,转载请注明出处,谢谢. 0. 前言 皇天不负有心人,终于我们到了运行 main goroutine 环节了.让我们走起来,看看一个 goroutine 到底是怎么运行的. 1. 运 ...
- CSS & JS Effect – 画三角形 Triangle
前言 画三角形有什么用? 可以做这样的 Design 参考 5 Ways To Create A Triangle With CSS Border Triangle 用 border 做 三角形应该是 ...
- Qt中当程序结束时线程的退出
在Qt程序结束时应该如何退出正在运行的任务子线程? 因个人经验和能力有限,本文仅供参考,有错误或者考虑不完善的地方请各位批评指正. 一.正常情况下如何创建和退出线程 1.继承QThread,重写run ...