B - Median Pyramid Easy 构造题
B - Median Pyramid Easy
Time limit : 2sec / Memory limit : 256MB
Score : 400 points
Problem Statement
We have a pyramid with N steps, built with blocks. The steps are numbered 1 through N from top to bottom. For each 1≤i≤N, step i consists of 2i−1 blocks aligned horizontally. The pyramid is built so that the blocks at the centers of the steps are aligned vertically.
A pyramid with N=4 steps
Snuke wrote a permutation of (1, 2, …, 2N−1) into the blocks of step N. Then, he wrote integers into all remaining blocks, under the following rule:
- The integer written into a block b must be equal to the median of the three integers written into the three blocks directly under b, or to the lower left or lower right of b.
Writing integers into the blocks
Afterwards, he erased all integers written into the blocks. Now, he only remembers that the integer written into the block of step 1 was x.
Construct a permutation of (1, 2, …, 2N−1) that could have been written into the blocks of step N, or declare that Snuke's memory is incorrect and such a permutation does not exist.
Constraints
- 2≤N≤105
- 1≤x≤2N−1
Input
The input is given from Standard Input in the following format:
N x
Output
If no permutation of (1, 2, …, 2N−1) could have been written into the blocks of step N, print No
.
Otherwise, print Yes
in the first line, then print 2N−1 lines in addition.
The i-th of these 2N−1 lines should contain the i-th element of a possible permutation.
Sample Input 1
4 4
Sample Output 1
Yes
1
6
3
7
4
5
2
This case corresponds to the figure in the problem statement.
Sample Input 2
2 1
Sample Output 2
No
No matter what permutation was written into the blocks of step N, the integer written into the block of step 1 would be 2.
思路:
要使得最上面的数是x。那么,特判完特殊情况后。(就是输出NO的,)
如果x==1或者x==2*n-1是不行的,因为他们不可能是中间数。不能弄上去。
否则我们尽可能使得倒数第二行得x个数尽量多。
这样是最优的。
比如x=4 n = 4
那么应该是1 6 3 4 5 2 7
有三个4弄上去了
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string> const int maxn = 2e5 + ;
int a[maxn];
bool book[maxn];
void work() {
int n, x;
scanf("%d%d", &n, &x);
if (x == || x == * n - ) {
printf("No\n");
return;
}
int t = * n - ;
a[t / + ] = x;
a[t / ] = x - ;
a[t / + ] = x + ;
book[x] = book[x - ] = book[x + ] = ;
if (t / - > && x + <= t) {
a[t / - ] = x + ;
book[x + ] = ;
}
if (t / + <= t && x - >= ) {
a[t / + ] = x - ;
book[x - ] = ;
}
int to = ;
for (int i = ; i <= t; ++i) {
if (a[i] != ) continue;
while (book[to] != ) to++;
a[i] = to;
book[to] = ;
}
printf("Yes\n");
for (int i = ; i <= t; ++i) {
printf("%d\n", a[i]);
}
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
work();
return ;
}
B - Median Pyramid Easy 构造题的更多相关文章
- $AT2163\ Median\ Pyramid\ Easy$ 构造
正解:构造 解题报告: 传送门$QwQ$ 考虑如果有两个相邻格子是相同数字那么它们以上这两列就都会是这列数字(显然$QwQ$? 所以考虑只要构造出第$n-1$行的中心和中心右侧($or$左侧一样的$Q ...
- AT2163 [AGC006B] Median Pyramid Easy
需要一点灵感的题目. 可以发现这样一个事情,当三个数中有两个数相同时,中为数一定是这两个相同的数. 基于这个观察,我们想让每一行都存在这样两个相同的两个数,就一定能保证第一层的值为 \(x\) 了. ...
- cf251.2.C (构造题的技巧)
C. Devu and Partitioning of the Array time limit per test 1 second memory limit per test 256 megabyt ...
- hdu4671 Backup Plan ——构造题
link:http://acm.hdu.edu.cn/showproblem.php?pid=4671 其实是不难的那种构造题,先排第一列,第二列从后往前选. #include <iostrea ...
- Educational Codeforces Round 7 D. Optimal Number Permutation 构造题
D. Optimal Number Permutation 题目连接: http://www.codeforces.com/contest/622/problem/D Description You ...
- Codeforces 482 - Diverse Permutation 构造题
这是一道蛮基础的构造题. - k +(k - 1) -(k - 2) 1 + k , 1 , k , 2, ....... ...
- BZOJ 3097: Hash Killer I【构造题,思维题】
3097: Hash Killer I Time Limit: 5 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 963 Solved: 36 ...
- CF1110E Magic Stones(构造题)
这场CF怎么这么多构造题…… 题目链接:CF原网 洛谷 题目大意:给定两个长度为 $n$ 的序列 $c$ 和 $t$.每次我们可以对 $c_i(2\le i<n)$ 进行一次操作,也就是把 $c ...
- CDOJ 1288 旅游的Final柱 构造题
旅游的Final柱 题目连接: http://acm.uestc.edu.cn/#/problem/show/1288 Description 柱神要去打Final啦~(≧▽≦)/~啦啦啦 柱神来到了 ...
随机推荐
- Linux下Fork与Exec
一.引言 对于没有接触过Unix/Linux操作系统的人来说,fork是最难理解的概念之一:它执行一次却返回两个值.fork函数是Unix系统最杰出的成就之一,它是七十年代UNIX早期的开发者经过长期 ...
- codeforces 706C C. Hard problem(dp)
题目链接: C. Hard problem time limit per test 1 second memory limit per test 256 megabytes input standar ...
- STL容器特征总结与迭代器失效
Vector 内部数据结构:连续存储,例如数组. 随机访问每个元素,所需要的时间为常量. 在末尾增加或删除元素所需时间与元素数目无关,在中间或开头增加或删除元素所需时间随元素数目呈线性变化. 可动态增 ...
- Linux 文本的^M问题
很多人在windows中使用文本编辑器编辑好文本后,传送到linux系统后,使用vi工具打开后发现每一行文本最后都有一个^M号,原因是: 在DOS使用的换行符为 \r\n,我们称为CR(\r)与LF( ...
- Codeforces 762B USB vs. PS/2 贪心
Codeforces 762B 题目大意: 有a台只有USB接口的电脑,b台PS/2接口的电脑,c台两种接口都有的电脑.每台电脑只用装一个鼠标.给出n个鼠标及其费用,每个鼠标只能使用一遍.在最大化有鼠 ...
- Hibernate区分不同对象的方法
1.关系数据库按主键区分不同记录. create table CUSTOMERS (ID int promary key not null, NAME varchar(15)); insert ...
- Inno Setup整理
1.如何使inno setup添加快捷方式默认选中 在[Tasks]段,有 Flags:unchecked改成 Flags: checkablealone; 即可 完整代码示例: [Tasks] Na ...
- 利用 druid 的 sql parser 模块解析 sql 语句
druid 是阿里开源在 github 上面的数据库连接池,里面有一个专门解析 sql 语句的模块 源码位置: https://github.com/alibaba/druid SQL Parse ...
- 20.Consent Controller Get请求逻辑实现
在这里之前讲的这一块的信息就要登场了 需要通过构造函数把这几个注入进来 县引入这三个命名空间 把这三个注入进来,这就是显示依赖 先通过returnUrl拿到这个Request request拿到后,就 ...
- [Makefile] Makefile 及其工作原理
转自:https://www.linuxidc.com/Linux/2018-09/154071.htm 当你需要在一些源文件改变后运行或更新一个任务时,通常会用到 make 工具.make 工具需要 ...