当前位置:网站首页>[noi Simulation Competition] geiguo and time chicken (structure)
[noi Simulation Competition] geiguo and time chicken (structure)
2022-06-24 08:54:00 【DD(XYX)】
Topic
1s,512mb
Geiguo is a magical country . Because of the good governance given to Taizu ( And found the giving country theorem ), To speed up the development of science and technology , Invented the time chicken . Give Taizu the honor to be the first person to eat time chicken .
At first the chicken would jump to 0 0 0 moment . Then walk normally every time , When they arrive in 2 n + 1 2n+1 2n+1 Caught when . But what makes Taizu unexpected , The timeline of its passage is disrupted by the wormhole . say concretely , i i i Time and j j j Always connected by a wormhole . If time chicken from i − 1 i-1 i−1 here we are i i i moment , It will be immediately transmitted to j j j moment . If time chicken from j − 1 j-1 j−1 here we are j j j moment , It will be immediately transmitted to i i i moment .
All in all n n n Connect two different moments to the wormhole . Every 1 ∼ 2 n 1\sim 2n 1∼2n Is just connected by a wormhole . because 0 0 0 Time and 2 n + 1 2n+1 2n+1 There is no wormhole at all times , It is easy to prove that time chicken can always reach 2 n + 1 2n+1 2n+1 moment . Because of the long time , The wormhole has disappeared , Only the chicken footprints can be seen at any time .
You need to restore a feasible wormhole matching scheme according to the records given to Taizu , Or tell Taizu that the record is wrong .
Input format
The first line has two integers n , m n,m n,m, Indicates that the total time is 2 n + 1 2n+1 2n+1 And the total length of time that footprints exist .
Next line m m m Number , The first i i i Number a i a_i ai Express ( a i , a i + 1 ) (a_i,a_i+1) (ai,ai+1) During this period of time, there are chicken feet .
Output format
If there is no solution output No
, Otherwise, output a line first Yes
, Then the output n n n That's ok , Two numbers per line x , y x,y x,y , Express ( x , y ) (x,y) (x,y) There is a wormhole between them .
If there are multiple construction schemes , Output any group .
The sample input
3 6
0 1 2 4 5 6
Sample output
Yes
1 5
2 6
3 4
Data range and tips
m ≤ 2 n + 1 , a i − 1 < a i , a 1 = 0 , a m = 2 n m\leq2n+1,a_{i-1}<a_i,a_1=0,a_m=2n m≤2n+1,ai−1<ai,a1=0,am=2n ;
0 ≤ n ≤ 100000 , 0 ≤ m ≤ 200000 0\leq n\leq100000,0\leq m\leq 200000 0≤n≤100000,0≤m≤200000 .
Answer key
We make the number of consecutive segments left in the middle to be A A A , So easy to prove when m − A − 1 m-A-1 m−A−1 There must be no solution when it is an odd number , Because you will match the necessary interval to the non - walkable interval .
Then there are two situations , ( m − A − 1 ) % 4 = 0 (m-A-1)\%4=0 (m−A−1)%4=0 and ( m − A − 1 ) % 4 = 2 (m-A-1)\%4=2 (m−A−1)%4=2 .
The first case can be constructed to prove that there must be a solution :
- First connect the two ends of each non walkable extremely long continuous segment , In this way, there are still m − A − 1 m-A-1 m−A−1 Can match each other .
- Put these matching points on a double ended queue in order .
- Take out the two elements at the beginning of the queue each time A < B A<B A<B , Two elements of the tail of the team C < D C<D C<D .
- matching ( A , C ) , ( B , D ) (A,C),(B,D) (A,C),(B,D) , This will go through ( C , C + 1 ) , ( B , B + 1 ) , (C,C+1),(B,B+1), (C,C+1),(B,B+1), The rest of the queue , ( A , A + 1 ) , ( D , D + 1 ) (A,A+1),(D,D+1) (A,A+1),(D,D+1) .
- Jump back to 3 Step , Until the queue is empty .
In the second case, we find that there may be a solution , If and only if there is such a structure :
The length of the middle section shall be at least 2 Extremely long walkable interval of , The two sides are separated by non walkable sections , about At least one side There is a matching walkable interval .
Take the left as an example , If there is a walkable zone on the left , It is the first red dot on the left in the above figure , Then we can build such a structure :
This point matches the first point on the left of the middle section , Two non walkable sections are connected with a large arch bridge at both ends , The remaining matching points are m − A − 3 m-A-3 m−A−3 A the , Can be viewed as % 4 = 0 \%4=0 %4=0 The case structure of . Delete the process through this structure , The rest of the walking is just continuous , Easy to prove correct .
CODE
#include<map>
#include<set>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<random>
#include<bitset>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
// #pragma GCC optimize(2)
using namespace std;
#define MAXN 200005
#define LL long long
#define ULL unsigned long long
#define ENDL putchar('\n')
#define DB double
#define lowbit(x) (-(x) & (x))
#define FI first
#define SE second
#define PR pair<int,int>
int xchar() {
static const int maxn = 1000000;
static char b[maxn];
static int pos = 0,len = 0;
if(pos == len) pos = 0,len = fread(b,1,maxn,stdin);
if(pos == len) return -1;
return b[pos ++];
}
// #define getchar() xchar()
LL read() {
LL f = 1,x = 0;int s = getchar();
while(s < '0' || s > '9') {
if(s<0)return -1;if(s=='-')f=-f;s = getchar();}
while(s >= '0' && s <= '9') {
x = (x<<1) + (x<<3) + (s^48);s = getchar();}
return f*x;
}
void putpos(LL x) {
if(!x)return ;putpos(x/10);putchar((x%10)^48);}
void putnum(LL x) {
if(!x) {
putchar('0');return ;}
if(x<0) putchar('-'),x = -x;
return putpos(x);
}
void AIput(LL x,int c) {
putnum(x);putchar(c);}
int n,m,s,o,k;
int a[MAXN],ct;
PR b[MAXN];
bool f[MAXN],aa[MAXN];
deque<int> q;
int main() {
freopen("shuttle.in","r",stdin);
freopen("shuttle.out","w",stdout);
n = read(); m = read();
int cn = 0,flg = 0,l = 0,r = 0;
for(int i = 1;i <= m;i ++) {
a[i] = read();
if(i > 1 && a[i] > a[i-1]+1) {
cn ++; aa[a[i]] = 1;
if(cn > 1 && a[i-1] == a[i-2]+1) flg = 1,r = i-1;
}
}
if((m - cn) % 4 == 1) {
for(int i = 2;i <= m;i ++) {
if(a[i] > a[i-1]+1) {
b[++ ct] = {
a[i-1]+1,a[i]};
f[a[i-1]+1] = f[a[i]] = 1;
}
else q.push_back(a[i]);
}
while(!q.empty()) {
int A,B,C,D;
A = q.front(); q.pop_front();
B = q.front(); q.pop_front();
D = q.back(); q.pop_back();
C = q.back(); q.pop_back();
b[++ ct] = {
A,C}; b[++ ct] = {
B,D};
f[A] = f[B] = f[C] = f[D] = 1;
}
int p = 0;
for(int i = 1;i <= (n<<1);i ++) {
if(!f[i]) {
if(p) b[++ ct] = {
p,i},p = 0;
else p = i;
}
}
sort(b + 1,b + 1 + ct);
printf("Yes\n");
for(int i = 1;i <= n;i ++) {
AIput(b[i].FI,' ');
AIput(b[i].SE,'\n');
}
return 0;
}
if((m - cn) % 4 == 3 && flg) {
l = r-1;
while(!aa[a[l]]) l --;
int p1,p2;
p1 = a[l-1]+1; p2 = a[r+1];
b[++ ct] = {
p1,p2}; f[p1] = f[p2] = 1;
p1 = a[l]; p2 = a[r]+1;
b[++ ct] = {
p1,p2}; f[p1] = f[p2] = 1;
int ll = l-1,rr = r+2;
while(aa[a[ll]]) ll --;
while(aa[a[rr]] && rr <= m) rr ++;
if(ll > 1) {
p1 = a[ll]; p2 = a[l+1];
b[++ ct] = {
p1,p2}; f[p1] = f[p2] = 1;
}
else if(rr <= m) {
p1 = a[r]; p2 = a[rr];
b[++ ct] = {
p1,p2}; f[p1] = f[p2] = 1;
}
else return printf("No\n"),0;
for(int i = 2;i <= m;i ++) {
if(a[i] > a[i-1]+1) {
if(i != l && i != r+1) {
b[++ ct] = {
a[i-1]+1,a[i]};
f[a[i-1]+1] = f[a[i]] = 1;
}
}
else if(!f[a[i]]) {
q.push_back(a[i]);
}
}
while(!q.empty()) {
int A,B,C,D;
A = q.front(); q.pop_front();
B = q.front(); q.pop_front();
D = q.back(); q.pop_back();
C = q.back(); q.pop_back();
b[++ ct] = {
A,C}; b[++ ct] = {
B,D};
f[A] = f[B] = f[C] = f[D] = 1;
}
int p = 0;
for(int i = 1;i <= (n<<1);i ++) {
if(!f[i]) {
if(p) b[++ ct] = {
p,i},p = 0;
else p = i;
}
}
sort(b + 1,b + 1 + ct);
printf("Yes\n");
for(int i = 1;i <= n;i ++) {
AIput(b[i].FI,' ');
AIput(b[i].SE,'\n');
}
return 0;
}
printf("No\n");
return 0;
}
边栏推荐
猜你喜欢
Opencv daily function structure analysis and shape descriptor (7) finding polygon (contour) / rotating rectangle intersection
[pytorch basic tutorial 30] code analysis of DSSM twin tower model
Centos7 installation of jdk8, mysql5.7 and Navicat connection to virtual machine MySQL and solutions (solutions to MySQL download errors are attached)
用VNC Viewer的方式远程连接无需显示屏的树莓派
The form image uploaded in chorme cannot view the binary image information of the request body
【使用 PicGo+腾讯云对象存储COS 作为图床】
110. balanced binary tree recursive method
Detailed explanation of Base64 coding and its variants (to solve the problem that the plus sign changes into a space in the URL)
Jenkins自动化部署,连接不到所依赖的服务【已解决】
pymysql 向MySQL 插入数据无故报错
随机推荐
Database to query the quantity of books lent in this month. If it is higher than 10, it will display "more than 10 books lent in this month". Otherwise, it will display "less than 10 books lent in thi
2022.06.23(LC_144,94,145_二叉树的前序、中序、后序遍历)
Wan Weiwei, a researcher from Osaka University, Japan, introduced the rapid integration method and application of robot based on WRS system
常用表情符号
解决:模型训练时loss出现nan
听说你还在花钱从网上买 PPT 模板?
Array opposite pointer series
所说的Get post:请求的区别,你真的知道了吗??????
Prompt code when MySQL inserts Chinese data due to character set problems: 1366
【LeetCode】387. 字符串中的第一个唯一字符
pm2 部署 nuxt3.js 项目
Jenkins自动化部署,连接不到所依赖的服务【已解决】
Earthly 容器镜像构建工具 —— 筑梦之路
Earthly container image construction tool -- the road to dream
Detailed explanation of Base64 coding and its variants (to solve the problem that the plus sign changes into a space in the URL)
opencv最大值滤波(不局限于图像)
工具类
【LeetCode】415. 字符串相加
Liunx Mysql安装
leetcode 1642. Furthest building you can reach