CodeforcesRound#749(Div.1.2basedonTechnocup2022EliminationRound1)-D.Omkar and the Meaning of Life-题解

本文主要是介绍CodeforcesRound#749(Div.1.2basedonTechnocup2022EliminationRound1)-D.Omkar and the Meaning of Life-题解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

    • Codeforces Round #749 (Div. 1 + Div. 2, based on Technocup 2022 Elimination Round 1) - D. Omkar and the Meaning of Life
      • Problem Description
      • Interaction
      • Hack Format
      • Sample Input
      • Sample Onput
      • Note
  • 题目大意
  • 解题思路
  • AC代码

Codeforces Round #749 (Div. 1 + Div. 2, based on Technocup 2022 Elimination Round 1) - D. Omkar and the Meaning of Life

传送门
Time Limit: 2 seconds
Memory Limit: 256 megabytes

Problem Description

It turns out that the meaning of life is a permutation p 1 , p 2 , … , p n p1,p2,…,pn p1,p2,,pn of the integers 1 , 2 , … , n ( 2 ≤ n ≤ 100 ) 1,2,…,n (2≤n≤100) 1,2,,n(2n100). Omkar, having created all life, knows this permutation, and will allow you to figure it out using some queries.

A query consists of an array a 1 , a 2 , … , a n a1,a2,…,an a1,a2,,an of integers between 1 1 1 and n n n. a is not required to be a permutation. Omkar will first compute the pairwise sum of a a a and p p p, meaning that he will compute an array s s s where s j = p j + a j s_j=p_j+a_j sj=pj+aj for all j = 1 , 2 , … , n j=1,2,…,n j=1,2,,n. Then, he will find the smallest index k k k such that sk occurs more than once in s s s, and answer with k k k. If there is no such index k k k, then he will answer with 0 0 0.

You can perform at most 2 n 2n 2n queries. Figure out the meaning of life p p p.

Interaction

Start the interaction by reading single integer n ( 2 ≤ n ≤ 100 ) n (2≤n≤100) n(2n100) — the length of the permutation p p p.

You can then make queries. A query consists of a single line “ ? a 1 a 2 … a n ?\ a_1\ a_2\ …\ a_n ? a1 a2  an ( 1 ≤ a j ≤ n ) (1≤a_j≤n) (1ajn).

The answer to each query will be a single integer k k k as described above ( 0 ≤ k ≤ n ) (0≤k≤n) (0kn).

After making a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:

fflush(stdout) or cout.flush() in C++;
System.out.flush() in Java;
flush(output) in Pascal;
stdout.flush() in Python;

see documentation for other languages.

To output your answer, print a single line “ ! a 1 a 2 … a n !\ a_1\ a_2\ …\ a_n ! a1 a2  an” then terminate.

You can make at most 2 n 2n 2n queries. Outputting the answer does not count as a query.

Hack Format

To hack, first output a line containing n ( 2 ≤ n ≤ 100 ) n (2≤n≤100) n(2n100), then output another line containing the hidden permutation p 1 , p 2 , … , p n p_1,p_2,…,p_n p1,p2,,pn of numbers from 1 1 1 to n n n.

Sample Input

5201

Sample Onput


? 4 4 2 3 2? 3 5 1 5 5? 5 2 4 3 1! 3 2 1 5 4

Note

In the sample, the hidden permutation p p p is [ 3 , 2 , 1 , 5 , 4 ] [3,2,1,5,4] [3,2,1,5,4]. Three queries were made.

The first query is a = [ 4 , 4 , 2 , 3 , 2 ] a=[4,4,2,3,2] a=[4,4,2,3,2]. This yields s = [ 3 + 4 , 2 + 4 , 1 + 2 , 5 + 3 , 4 + 2 ] = [ 7 , 6 , 3 , 8 , 6 ] s=[3+4,2+4,1+2,5+3,4+2]=[7,6,3,8,6] s=[3+4,2+4,1+2,5+3,4+2]=[7,6,3,8,6]. 6 6 6 is the only number that appears more than once, and it appears first at index 2 2 2, making the answer to the query 2 2 2.

The second query is a = [ 3 , 5 , 1 , 5 , 5 ] a=[3,5,1,5,5] a=[3,5,1,5,5]. This yields s = [ 3 + 3 , 2 + 5 , 1 + 1 , 5 + 5 , 4 + 5 ] = [ 6 , 7 , 2 , 10 , 9 ] s=[3+3,2+5,1+1,5+5,4+5]=[6,7,2,10,9] s=[3+3,2+5,1+1,5+5,4+5]=[6,7,2,10,9]. There are no numbers that appear more than once here, so the answer to the query is 0.

The third query is a = [ 5 , 2 , 4 , 3 , 1 ] a=[5,2,4,3,1] a=[5,2,4,3,1]. This yields s = [ 3 + 5 , 2 + 2 , 1 + 4 , 5 + 3 , 4 + 1 ] = [ 8 , 4 , 5 , 8 , 5 ] s=[3+5,2+2,1+4,5+3,4+1]=[8,4,5,8,5] s=[3+5,2+2,1+4,5+3,4+1]=[8,4,5,8,5]. 5 5 5 and 8 8 8 both occur more than once here. 5 5 5 first appears at index 3 3 3, while 8 8 8 first appears at index 1 1 1, and 1 < 3 1<3 1<3, making the answer to the query 1 1 1.

Note that the sample is only meant to provide an example of how the interaction works; it is not guaranteed that the above queries represent a correct strategy with which to determine the answer.


题目大意

给你一个 n n n,这组数据其实是隐藏了 n n n的一个全排列。你可以进行最多 2 n 2n 2n次询问,每次输入一个新的序列,让隐藏的序列一一对应临时加上这个新的序列,之后把有相同的值的元素中,最小的下标返回给你。

让你通过询问,得出隐藏的序列。

比如样例隐藏了序列 [ 3 , 2 , 1 , 5 , 4 ] [3,2,1,5,4] [3,2,1,5,4],你进行了一次询问并让隐藏序列一一对应地临时加上你给的序列 [ 4 , 4 , 2 , 3 , 2 ] [4,4,2,3,2] [4,4,2,3,2]. 那么相加产生的临时序列是 [ 3 + 4 , 2 + 4 , 1 + 2 , 5 + 3 , 4 + 2 ] = [ 7 , 6 , 3 , 8 , 6 ] [3+4,2+4,1+2,5+3,4+2]=[7,6,3,8,6] [3+4,2+4,1+2,5+3,4+2]=[7,6,3,8,6]. 6 6 6 不只一个,所有有相同值的元素集合是: [ 第 2 个 元 素 , 第 5 个 元 素 ] [第2个元素,第5个元素] [25],其中下标最小的是第二个元素,它的下标是 2 2 2。因此,你得到了询问的结果: 2 2 2

解题思路

其实每次询问可以把其中一个数 a a a加一,其他所有数都加2。那么如果有 b = a − 1 b=a-1 b=a1,那么因为 a a a 加了 1 1 1 b b b 加了 2 2 2 ,所以 a a a b b b加上临时序列后的值就会相同,因此就会返回这两个数中下标最小的元素。如果很荣幸 b = a − 1 b=a-1 b=a1 b b b的前面,那么我们就得到了比 a a a 1 1 1的那个数的下标。

同理,只把 a a a加上 2 2 2,其他元素都加上 1 1 1,就可以令 a a a a + 1 a+1 a+1相等(如果存在的话)。如果 a + 1 a+1 a+1 a a a的前面,那么我们就能得到比 a a a 1 1 1的数的下标。

正好 2 ∗ n 2*n 2n次询问,我们可以得到 n − 1 n-1 n1个“大1关系”,即得到了 n − 1 n-1 n1对相差为 1 1 1的数。

因为没有 0 0 0,所以不存在比 1 1 1 1 1 1的数,由此特殊条件我们可以判断出最小的数 1 1 1所在的位置。之后依据 n − 1 n-1 n1对“大1关系”,就可以还原出原始数组的样子。


AC代码

/** @Author: LetMeFly* @Date: 2021-10-17 20:10:26* @LastEditors: LetMeFly* @LastEditTime: 2021-10-19 00:07:29*/ // LastEditTime: 2021-10-17 20:59:28
#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
struct Conditions {int l, r; // 下标为l的元素比下标为r的大1// bool xd;
} cond[1010];
int sx[1010] = {0}; // sx[i]用来记录下标为i的数是否当过“比其他数大1的数”。如果没有,那么下标为i的数就是1
int Next[1010]; // Next[i]代表下标为i的数+1的数的下标
int ans[1010]; // 答案。ans[i]代表下标为i的数的真实隐藏的数
int main() {int n;cin >> n; // n个数int cnt = 0; // 现在又了cnt个“大1关系”for (int i = 1; i <= n; i++) {printf("? "); // 询问for (int j = 1; j <= n; j++) {if (j != i) printf("2 "); // 这个数之外的数都加2else printf("1 "); // 这个数+1}puts(""); // 换行fflush(stdout); // 清空缓冲区int answer; // 这个询问的回答cd(answer); // scanf("%d", &answer);if (answer != 0 && answer < i) { // answer=0代表加上后没有相同的数,answer=i说明要找的数在i后面cond[cnt].l = answer; // 回答的数的下标cond[cnt].r = i; // 比只加了1的数 小1// cond[cnt].xd = true;cnt++; // 已有的“大1关系”的数++}printf("? "); // 询问for (int j = 1; j <= n; j++) {if (j != i) printf("1 "); // 其他数+2else printf("2 "); // 这个数+1}puts(""); // 换行fflush(stdout); // 清空缓冲区cd(answer); // 同上if (answer != 0 && answer < i) {cond[cnt].l = i;cond[cnt].r = answer;// cond[cnt].xd = false;cnt++;}}for (int i = 0; i < cnt; i++) {Next[cond[i].l] = cond[i].r; // 下标为cond[i].l的数 +1得到的数 的下标是cond[i].rsx[cond[i].r]++; // cond[i].r当过“比其他数大1的数”}int one; // 1所在的下标for (int i = 1; i <= n; i++) {if (!sx[i]) { // 这个数没当过“比其他数大1的数”one = i; // 1就是它了break;}}for (int i = 1; i <= n; i++) { // 一共要还原n个数ans[one] = i; // 答案(原始的隐藏的数)one = Next[one]; // 比它大1的数}printf("! "); // 输出最终答案for (int i = 1; i <= n; i++) {printf("%d ", ans[i]);}puts(""); // 换行return 0;
}

原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/120837226

这篇关于CodeforcesRound#749(Div.1.2basedonTechnocup2022EliminationRound1)-D.Omkar and the Meaning of Life-题解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/893886

相关文章

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

C - Word Ladder题解

C - Word Ladder 题解 解题思路: 先输入两个字符串S 和t 然后在S和T中寻找有多少个字符不同的个数(也就是需要变换多少次) 开始替换时: tips: 字符串下标以0开始 我们定义两个变量a和b,用于记录当前遍历到的字符 首先是判断:如果这时a已经==b了,那么就跳过,不用管; 如果a大于b的话:那么我们就让s中的第i项替换成b,接着就直接输出S就行了。 这样

【秋招笔试】9.07米哈游秋招改编题-三语言题解

🍭 大家好这里是 春秋招笔试突围,一起备战大厂笔试 💻 ACM金牌团队🏅️ | 多次AK大厂笔试 | 大厂实习经历 ✨ 本系列打算持续跟新 春秋招笔试题 👏 感谢大家的订阅➕ 和 喜欢💗 和 手里的小花花🌸 ✨ 笔试合集传送们 -> 🧷春秋招笔试合集 🍒 本专栏已收集 100+ 套笔试题,笔试真题 会在第一时间跟新 🍄 题面描述等均已改编,如果和你笔试题看到的题面描述

LeetCode 第414场周赛个人题解

目录 Q1. 将日期转换为二进制表示 原题链接 思路分析 AC代码 Q2. 范围内整数的最大得分 原题链接 思路分析 AC代码 Q3. 到达数组末尾的最大得分 原题链接 思路分析 AC代码 Q4. 吃掉所有兵需要的最多移动次数 原题链接 思路分析 AC代码 Q1. 将日期转换为二进制表示 原题链接 Q1. 将日期转换为二进制表示 思路分析

牛客小白月赛100部分题解

比赛地址:牛客小白月赛100_ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛_牛客竞赛OJ A.ACM中的A题 #include<bits/stdc++.h>using namespace std;#define ll long long#define ull = unsigned long longvoid solve() {ll a,b,c;cin>>a>>b>

P2858 [USACO06FEB] Treats for the Cows G/S 题解

P2858 题意 给一个数组。每天把最左或者最右的东西卖掉,第 i i i个东西,第 d a y day day天卖出的价格是 a [ i ] ∗ d a y a[i]*day a[i]∗day。 记忆化搜索 void dfs(int l,int r,int day,ll sum){if(v[l][r]>=sum)return;v[l][r]=sum;if(l>r)//这就是dp答案{

【C++题解】1272. 郭远摘苹果

欢迎关注本专栏《C++从零基础到信奥赛入门级(CSP-J)》 问题:1272. 郭远摘苹果 类型:二维数组 题目描述: 郭远有一天走到了一片苹果林,里面每颗树上都结有不同数目的苹果,郭远身上只能拿同一棵树上的苹果,他每到一棵果树前都会把自己身上的苹果扔掉并摘下他所在树上的苹果并带走(假设郭远会走过每一棵苹果树),问在郭远摘苹果的整个过程中,他身上携带的最多苹果数与最小苹果数的差是多少?

【最新华为OD机试E卷-支持在线评测】机器人活动区域(100分)多语言题解-(Python/C/JavaScript/Java/Cpp)

🍭 大家好这里是春秋招笔试突围 ,一枚热爱算法的程序员 ✨ 本系列打算持续跟新华为OD-E/D卷的三语言AC题解 💻 ACM金牌🏅️团队| 多次AK大厂笔试 | 编程一对一辅导 👏 感谢大家的订阅➕ 和 喜欢💗 🍿 最新华为OD机试D卷目录,全、新、准,题目覆盖率达 95% 以上,支持题目在线评测,专栏文章质量平均 94 分 最新华为OD机试目录: https://blog.

2023 CCPC(秦皇岛)现场(第二届环球杯.第 2 阶段:秦皇岛)部分题解

所有题目链接:Dashboard - The 2023 CCPC (Qinhuangdao) Onsite (The 2nd Universal Cup. Stage 9: Qinhuangdao) - Codeforces 中文题面: contest-37054-zh.pdf (codeforces.com) G. Path 链接: Problem - G - Codeforces