GYM 100488 B.Impossible to Guess(构造)
来源:互联网 发布:数组 字符串 转换 js 编辑:程序博客网 时间:2024/05/18 00:33
Description
一个1~n的排列p[1],p[2],…,p[n],每次询问区间[l,r]会告诉将p[l],…,p[r]升序排列后输出,至多[n/2]次询问复原p序列
Input
一个整数n表示序列长度(1<=n<=100)
Output
输出Q l r表示询问[l,r],之后会告诉升序排列后的p[l],…,p[r],当确定p序列后,输出A,之后输出这n个数
Sample Input
3
3
1 3
Sample Output
Q 1 1
Q 1 2
A 3 1 2
Solution
[n/2]次查询[i,i+(n-1)/2](1<=i<=(n+1)/2],每次查询用一个set存起来,那么相邻两个set的差就可以确定两个数字,那么我们就确定了1,2,…,(n-1)/2,(n+1)/2+1,…,(n-1)/2+(n-1)/2+1,当n为奇数时只有(n+1)/2未被确定,而第一次查询已经知道了前(n+1)/2个数字,现在前(n-1)/2个数字已经确定,故从set中删去这些数后剩余的就是(n+1)/2位置的数,当n为偶数时,第n个位置的数也确定不了,但是由于这是一个1~n的排列,而且前n-1个数已经确定,故剩余的数字就是第n个位置的数
以1 2 3 4 5和1 2 3 4 5 6为例:
1 2 3 4 5:查询(1,3),(2,4),(3,5)
(1,3)和(2,4)比较就得到了1位置和4位置的答案
(2,4)和(3,5)比较就得到了2位置和5位置的答案
从(1,3)的查询中删去1,2位置的数字就是3位置的答案
1 2 3 4 5 6:查询(1,3),(2,4),(3,5)
(1,3)和(2,4)比较就得到了1位置和4位置的答案
(2,4)和(3,5)比较就得到了2位置和5位置的答案
从(1,3)的查询中删去1,2位置的数字就是3位置的答案
从1~n中删去1,2,3,4,5位置的数字就是6位置的答案
Code
#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#include<cmath>#include<vector>#include<queue>#include<map>#include<set>#include<ctime>using namespace std;typedef long long ll;#define INF 0x3f3f3f3f#define maxn 111set<int>s[maxn];set<int>::iterator it;int n,a[maxn];int main(){ while(~scanf("%d",&n)) { s[0].clear(); for(int i=1;i<=(n+1)/2;i++) { s[i].clear(); printf("Q %d %d\n",i,(n-1)/2+i);cout.flush(); for(int j=0;j<(n+1)/2;j++) { int temp; scanf("%d",&temp); s[i].insert(temp); s[0].insert(temp); } } for(int i=1;i<=(n-1)/2;i++) { for(it=s[i].begin();it!=s[i].end();it++) if(s[i+1].find(*it)==s[i+1].end()) { a[i]=*it; break; } for(it=s[i+1].begin();it!=s[i+1].end();it++) if(s[i].find(*it)==s[i].end()) { a[(n-1)/2+i+1]=*it; break; } } for(int i=1;i<=(n-1)/2;i++)s[1].erase(a[i]); a[(n+1)/2]=*s[1].begin(); if(n%2==0) { for(int i=1;i<=n;i++) if(s[0].find(i)==s[0].end()) { a[n]=i; break; } } printf("A "); for(int i=1;i<=n;i++) printf("%d%c",a[i],i==n?'\n':' '),cout.flush(); } return 0;}
- GYM 100488 B.Impossible to Guess(构造)
- GYM 100488 J.Hyperdromes Strike Back(构造)
- GYM 100488 M.Construct a Permutation(构造)
- GYM 100488 Construct a Permutation(构造|想法)
- GYM 101149 M.Ex Machina(构造)
- GYM 101173 K.Key Knocking(构造)
- GYM 101173 L.Lost Logic(构造)
- Codeforces Gym 101142 J. Java2016 (构造)
- B. Guess the Permutation
- cf#Wunder Fund Round 2016 -B- Guess the Permutation-构造-乱搞
- GYM 100247 B. Similar Strings(水~)
- GYM 100030 B.Epic Battle(博弈论)
- GYM 100182 B.Euclid(计算几何)
- GYM 100883 B.Rectangles(水~)
- GYM 101147 B.Street(Floyd)
- GYM 100971 B.Derangement(水~)
- GYM 101086 B.Brother Louie(dfs)
- GYM 100694 B.Far Manager(水~)
- oracle 每日一题-数字格式化模型
- nginx 多个80端口转发
- java native2ascii的用法介绍
- hadoop 2.6全分布安装
- 九度 oj 题目1010:A + B
- GYM 100488 B.Impossible to Guess(构造)
- 【《Effective C#》提炼总结】提高Unity中C#代码质量的21条准则
- day01
- Spring Boot 部署与服务配置
- 生存射手学习笔记
- 测试工具对比汇总
- UVALive 3667 dfs
- DES加密算法
- 函数模板