当前位置:网站首页>[noi simulation] regional division (conclusion, structure)
[noi simulation] regional division (conclusion, structure)
2022-07-07 06:50:00 【DD(XYX)】
Topic


1s , 512mb
Answer key
First , If there are two neighboring cities with the same party, there must be no solution .
We found that , The final plan must have three adjacent cities in a triangle , And after connecting this triangle , It is equivalent to removing the middle city , The rest is another sub problem .
therefore , We count the number of adjacent triples that can form triangles X X X( If a certain position i i i Satisfy a i a_i ai And its left and right positions just form a set { 0 , 1 , 2 } \{0,1,2\} { 0,1,2} , Can form a triple ), It's not hard to find out , When we connect One Three triangles , X X X The reduction is Odd number , And there is a scheme to make it greater than zero . In the end, there must be three cities left , just X X X Turn into 1 Of , therefore —— The answer is if and only if X > 0 X>0 X>0 And X X X And n n n The parity of is the same .
How to make X X X Constant is greater than zero ? In fact, just try to use Edge triples —— If and only if a certain triple is related to the left and right triples The central city is adjacent when , This triplet No Edge triples . If there is no triplet on the edge , Of course, it's OK to choose any one . Because we found that , If you choose a non edge triple , Will make X X X Reduce 3 3 3 , At this time, only when they are all non edge triples can we guarantee that there are triples .
So we use set And linked list maintenance triples , Time complexity O ( n log n ) O(n\log n) O(nlogn) .
After reading the solution , I found myself a XX.
First , If there is no adjacent same city , be X X X The parity of must be related to n n n identical . That is, as long as X > 0 X>0 X>0 There is a solution . And it's not difficult to prove , As long as it exists at the same time 0 , 1 , 2 0,1,2 0,1,2 , X X X Greater than 0 .
then , There is a very simple construction method :

CODE
Hit my stupid size , Avoiding my stupid responsibility
#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 100005
#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>
#define UIN unsigned int
#define eps 1e-6
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()
inline 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);}
inline void putnum(LL x) {
if(!x) {
putchar('0');return ;}
if(x<0) putchar('-'),x = -x;
return putpos(x);
}
inline void AIput(LL x,int c) {
putnum(x);putchar(c);}
int n,m,s,o,k;
int a[MAXN];
int l[MAXN],r[MAXN];
set<int> st,sp;
bool che(int x) {
if(a[l[x]]^a[x]^a[r[x]]) return st.erase(x),0;
return st.insert(x),1;
}
bool che2(int x) {
if(!(a[l[x]]^a[x]^a[r[x]]) && ((a[l[l[x]]]^a[l[x]]^a[x]) || (a[x]^a[r[x]]^a[r[r[x]]]))) return sp.insert(x),1;
return sp.erase(x),0;
}
void del(int x) {
st.erase(x); sp.erase(x);
int s = l[x],o = r[x];
AIput(min(s,o)+1,' '); AIput(max(s,o)+1,'\n');
r[s] = o; l[o] = s;
che(s); che(o); che2(s); che2(o);
che2(l[s]); che2(r[o]);
}
int main() {
freopen("divide.in","r",stdin);
freopen("divide.out","w",stdout);
n = read();
for(int i = 0;i < n;i ++) a[i] = read()+1;
for(int i = 0;i < n;i ++) {
if(a[i] == a[(i+1)%n]) return printf("No\n"),0;
l[i] = (i+n-1)%n; r[i] = (i+1)%n;
}
for(int i = 0;i < n;i ++) che(i),che2(i);
if((st.size()&1) == (n&1) && !st.empty()) {
printf("Yes\n");
for(int i = 1;i <= n-3;i ++) {
int t = *st.begin();
while(!sp.empty() && !che2(*sp.begin()));
if(!sp.empty()) t = *sp.begin();
del(t); while(!st.empty() && !che(*st.begin()));
}
}
else printf("No");
return 0;
}
边栏推荐
- Basic DOS commands
- Postgresql源码(59)分析事务ID分配、溢出判断方法
- Config分布式配置中心
- 算法---比特位计数(Kotlin)
- 从零到一,教你搭建「CLIP 以文搜图」搜索服务(二):5 分钟实现原型
- 常用函数detect_image/predict
- SolidWorks GB Library (steel profile library, including aluminum profile, aluminum tube and other structures) installation and use tutorial (generating aluminum profile as an example)
- SVN version management in use replacement release and connection reset
- Redis (II) - redis General Command
- 【luogu P1971】兔兔与蛋蛋游戏(二分图博弈)
猜你喜欢

Comment les entreprises gèrent - elles les données? Partager les leçons tirées des quatre aspects de la gouvernance des données

HKUST & MsrA new research: on image to image conversion, fine tuning is all you need

JWT的基础介绍

【NOI模拟赛】区域划分(结论,构造)
![Stack and queue-p78-8 [2011 unified examination true question]](/img/df/72ba22f1953551943494d661a56a3b.jpg)
Stack and queue-p78-8 [2011 unified examination true question]

地质学类比较有名的外文期刊有哪些?

毕业设计游戏商城

How can I check the DOI number of a foreign document?

Answer to the first stage of the assignment of "information security management and evaluation" of the higher vocational group of the 2018 Jiangsu Vocational College skills competition

2022 Android interview essential knowledge points, a comprehensive summary
随机推荐
[opencv] morphological filtering (2): open operation, morphological gradient, top hat, black hat
反射(二)
Answer to the first stage of the assignment of "information security management and evaluation" of the higher vocational group of the 2018 Jiangsu Vocational College skills competition
一条慢SQL拖死整个系统
LM11丨重构K线构建择时交易策略
地质学类比较有名的外文期刊有哪些?
Postgresql中procedure支持事务语法(实例&分析)
「运维有小邓」符合GDPR的合规要求
【NOI模拟赛】区域划分(结论,构造)
Comment les entreprises gèrent - elles les données? Partager les leçons tirées des quatre aspects de la gouvernance des données
MySQL SQL的完整处理流程
如何给目标机器人建模并仿真【数学/控制意义】
[start from scratch] detailed process of deploying yolov5 in win10 system (CPU, no GPU)
C interview 24 (pointer) define a double array with 20 elements a
RuntimeError: CUDA error: CUBLAS_STATUS_ALLOC_FAILED when calling `cublasCreate(handle)`问题解决
【从零开始】win10系统部署Yolov5详细过程(CPU,无GPU)
ViewModelProvider.of 过时方法解决
Leetcode T1165: 日志分析
常用函数detect_image/predict
MySQL的安装