Codeforces 1129E - Legendary Tree(思维题)
考虑以 \(1\) 为根,记 \(siz_i\) 为 \(i\) 子树的大小,那么可以通过询问 \(S=\{2,3,\cdots,n\},T=\{1\}\) 以及每个 \(u\) 得出 \(siz_u\) 的值。
考虑将所有点按 \(siz\) 从小到大排序并维护一个集合 \(st\) 表示目前还没有找到父亲的点的集合,那么我们枚举到一个点 \(x\) 时就在 \(st\) 中二分找到所有父亲为 \(x\) 的点并将它们从 \(st\) 中删除,具体步骤是,我们先二分找出 \(st\) 中编号最小的点,即二分出一个 \(mid\) 后就询问 \(S=st\) 中编号最小的 \(mid\) 个点组成的集合,\(T=\{1\}\),\(u=x\),如果交互库返回的值 \(>0\) 就向左二分,否则向右二分,二分出第一个后再找第二个、第三个……以此类推。删完这些点之后加入将 \(x\) 加入集合,如此进行下去即可。最后扫描到 \(1\) 时就将 \(st\) 中的点的父亲全部设为 \(1\) 并清空 \(st\)
时间复杂度 \(n\log^2n\),询问次数 \(n\log n\),实测在 \(7000\) 左右,可以通过此题的限制。
看到没?什么超纲的算法都没有。所以啊,菜是原罪/kk——Codeforces Round #691 (Div.2) 题解
const int MAXN=500;
int n,siz[MAXN+5],ord[MAXN+5];set<int> st;
bool cmp(int x,int y){return siz[x]<siz[y];}
bool check(int x,int l,int r){
set<int>::iterator it=st.begin();
for(int i=1;i<l;i++) ++it;printf("%d\n",r-l+1);
for(int i=1;i<=r-l+1;i++) printf("%d%c",*it++," \n"[i==r-l+1]);
printf("1\n1\n%d\n",x);fflush(stdout);
int t;scanf("%d",&t);return t>0;
}
int main(){
scanf("%d",&n);siz[1]=n;
for(int i=2;i<=n;i++){
printf("%d\n",n-1);
for(int j=2;j<=n;j++) printf("%d%c",j," \n"[j==n]);
printf("1\n1\n%d\n",i);fflush(stdout);
scanf("%d",&siz[i]);
} for(int i=1;i<=n;i++) ord[i]=i;sort(ord+1,ord+n+1,cmp);
vector<pii> ans;
for(int i=1;i<n;i++){
if(!st.empty()){
int cur=0;vector<int> son;
while(cur<st.size()){
int l=cur+1,r=st.size(),p=st.size()+1;
while(l<=r){
int mid=l+r>>1;
if(check(ord[i],cur+1,mid)) p=mid,r=mid-1;
else l=mid+1;
} if(p!=st.size()+1){
set<int>::iterator it=st.begin();
for(int j=1;j<p;j++) ++it;
son.pb(*it);
} cur=p;
} for(int x:son) st.erase(st.find(x)),ans.pb(mp(x,ord[i]));
} st.insert(ord[i]);
} printf("ANSWER\n");
for(int x:st) ans.pb(mp(x,1));
for(pii p:ans) printf("%d %d\n",p.fi,p.se);
fflush(stdout);
return 0;
}
Codeforces 1129E - Legendary Tree(思维题)的更多相关文章
- Codeforces.1129E.Legendary Tree(交互 二分)
题目链接 \(Description\) 有一棵\(n\)个点的树.你需要在\(11111\)次询问内确定出这棵树的形态.每次询问你给定两个非空且不相交的点集\(S,T\)和一个点\(u\),交互库会 ...
- CF--思维练习-- CodeForces - 215C - Crosses(思维题)
ACM思维题训练集合 There is a board with a grid consisting of n rows and m columns, the rows are numbered fr ...
- Codeforces 675C Money Transfers 思维题
原题:http://codeforces.com/contest/675/problem/C 让我们用数组a保存每个银行的余额,因为所有余额的和加起来一定为0,所以我们能把整个数组a划分为几个区间,每 ...
- Codeforces 1090D - Similar Arrays - [思维题][构造题][2018-2019 Russia Open High School Programming Contest Problem D]
题目链接:https://codeforces.com/contest/1090/problem/D Vasya had an array of n integers, each element of ...
- CodeForce - 1189 D1. Add on a Tree (思维题)
Note that this is the first problem of the two similar problems. You can hack this problem only if y ...
- codeforces 1140D(区间dp/思维题)
D. Minimum Triangulation time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Codeforces 957 水位标记思维题
A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...
- ACM思维题训练 Section A
题目地址: 选题为入门的Codeforce div2/div1的C题和D题. 题解: A:CF思维联系–CodeForces -214C (拓扑排序+思维+贪心) B:CF–思维练习-- CodeFo ...
- Codeforces 1129 E.Legendary Tree
Codeforces 1129 E.Legendary Tree 解题思路: 这题好厉害,我来复读一下官方题解,顺便补充几句. 首先,可以通过询问 \(n-1\) 次 \((S=\{1\},T=\{ ...
随机推荐
- Git浅析
Git浅析 索引 Git的常用命令 GitHub的使用 Git版本创建和回退 Git的工作区和暂存区 Git分支管理 1-Git的常用命令 01.创建一个版本库--进入相应的目录 git init 可 ...
- Linux C语言链表详细分析
链表是一种常见的基础数据结构,结构体指针在这里得到了充分的利用.链表可以动态的进行存储分配,也就是说,链表是一个功能极为强大的数组,他可以在节点中定义多种数据类型,还可以根据需要随意增添,删除,插入节 ...
- 今天学习了BootStrap
今天学习了BootStrap 一.BootStrap介绍 Bootstrap是一个前端开发的框架,来自 Twitter,是目前很受欢迎的前端框架.Bootstrap 是基于 HTML.CSS.Java ...
- VMware Workstation 16.2 Pro for Linux SLIC 2.6 & Unlocker
请访问原文链接:https://sysin.org/blog/vmware-workstation-16-linux-slic/,查看最新版.原创作品,转载请保留出处. 作者:gc(at)sysin. ...
- Unity的Deferred Shading
什么是Deferred Shading Unity自身除了支持前向渲染之外,还支持延迟渲染.Unity的rendering path可以通过Edit/Project Settings中的Graphic ...
- hdu 5056 Boring count (类似单调队列的做法。。)
给一个由小写字母构成的字符串S,问有多少个子串满足:在这个子串中每个字母的个数都不超过K. 数据范围: 1<=T<= 1001 <= the length of S <= 10 ...
- linux 的 逻辑卷管理
lvm 逻辑卷管理器 关于逻辑卷管理lvm的一些操作 新建磁盘 sdcfdisk /dev/sdc 创建分区,更改分区id 为8e,改变分区类型为 lvm linux 创建物理卷与pv相关 pvcre ...
- istio基础详解
1.Istio介绍? 官方文档:https://istio.io/docs/concepts/what-is-istio/ 中文官方文档:https://istio.io/zh/docs/concep ...
- JS数据类型转换问题
一.数据类型的转换 数据类型的转换方法 强制转换(显示转换,主动转换) 字符转数值 parseInt(要转换的数值或变量) 转整数 从左向右依次转换,遇到第一个非数字的字符,停止转换 忽略小数点后的内 ...
- go闭包使用
1.带参数闭包函数 func main() { //先调用闭包外面的方法传给变量 add_func := add(1, 2) //再调用里面的方法,因为有了i++ 同一个内存地址 在一次编译中i的值会 ...