首页 > 其他分享 >Codeforces Round 988 (Div. 3) E题解析

Codeforces Round 988 (Div. 3) E题解析

时间:2024-11-18 17:10:11浏览次数:1  
标签:988 const temp int Codeforces long typedef 答案 Div

E题

题目链接

Codeforces Round 988 (Div. 3)

题目描述

题目的思路

根据题目的意思,我们可以推断出算法时间复杂度应该在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;
}
/*

*/

标签:988,const,temp,int,Codeforces,long,typedef,答案,Div
From: https://www.cnblogs.com/era768/p/18553140

相关文章

  • Codeforces Round 988 (Div. 3) A~D
    CodeforcesRound988(Div.3)A.Twice这个题就是简单的贪心题,在相同大小的元素里面,我们只能选择两个来对评分更新,所以最多能更新(相同元素的个数)/2次,记录相同元素的个数就行了//Problem:A.Twice//Contest:Codeforces-CodeforcesRound988(Div.3)//URL:https......
  • 题解:CF contest 2037 : [Codeforces Round 988 (Div. 3)] (A-E)
    ATwice题面Kinich醒来迎接新的一天的开始。他打开手机,查看邮箱,发现了一份神秘的礼物。他决定打开礼物的盒子。Kinich用\(n\)整数打开数组\(a\)。最初,Kinich的分数是\(0\)。他将执行任意次数的以下操作:—选择两个索引\(i\)和\(j\)\((1\leqi\lt;j\leqn)\),确......
  • Codeforces Round 988 (Div. 3)
    CodeforcesRound988(Div.3)总结A没啥好说的,用桶存出现次数。#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<vector>#include<queue>#include<map>usingnames......
  • Solution - Codeforces 1957E Carousel of Combinations
    首先这个\(C(i,j)\bmodj\)的形式就非常怪,于是首先肯定要先研究一下这个值。先考虑如何求\(C(i,j)\)。可以考虑先选出要用的\(j\)个数,再乘上其排列成环的方案数,那么有\(C(i,j)=\binom{i}{j}\times(j-1)!\)。那么就是来考虑\(\binom{i}{j}\times(j-1)!\bmod......
  • 【星航计划】2024.11 Div. 3 题解
    2024--星航计划--十一月份--基础算法A.分段每一段连续的\(1\)之间是独立的,我们只需要关心一段连续的1的结果。可以证明对于一段连续的\(1\),最优策略是将其划分成多个单独的\(1\)以及可能余下的连续两个\(1\)。对于\(k\)个连续的\(1\),如果\(k\)是奇数,......
  • Codeforces Round 987 (Div. 2) - 比赛总结
    Preface我是若只。A.PenchickandModernMonument先吃三发罚时。最优策略应当是把所有数都调成众数,然而我一开始就忙着往后面做,胡乱猜了个结论就WA了,又猜了一个又WA了,再猜了一个再WA了。点击查看代码constintN=105;intn,a[N];intmain(){ intT;read(T);......
  • Codeforces Round 986 (Div. 2)
    CodeforcesRound986(Div.2)总结A按题意模拟即可,因为\(n,a,b\)很小,可以多循环几遍来判断。只循环十遍的吃罚时qwq。#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<vector>#include<queue&......
  • Refact.ai Match 1 (Codeforces Round 985)
    Refact.aiMatch1(CodeforcesRound985)总结A集合中的元素为\(l\lex\ler\),有\(k\)个\(x\)的倍数在其中才可删,可以求出最大可删的元素,直接计算。#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#inclu......
  • 第九章DIV+CSS布局
    9.1DIV+CSS概述9.1.1认识DIVdiv标签在Web标准网页中属于块级元素9.1.2DIV的宽高设置9.1.2.1宽高属性9.1.2.2div标签内直接设置宽高9.1.2.3div使用选择器设置宽高<!DOCTYPEhtml><html> <head> <metacharset="utf-8"/> <title></title> <styletype......
  • Codeforces Round 987 (Div. 2)
    好不容易的一次CF阳间赛,这次题目普遍较长。A-PenchickandModernMonument题目大意将一个非递增的序列通过操作某些数(可增大可减小),最终使得序列变成一个非递减的序列,求出最少要操作多少次?解题思路这个题也是想了不断时间,而且还提交错误一次,原因就是调试的代码没......