正文
增加 33 行删除 33 行
280 行未改动全部展开2812822832842852862872882893 行未改动29329429529629729829930030125 行未改动32732832933033133233333433518 行未改动3543553563573583593603613623632 行未改动36636736836937037137237337437537637737837938038120 行未改动40240340440540640740840941014 行未改动42542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148231 行未改动51451551651751851952052152252313 行未改动53753853954054154254354454554654754854955055155255355418 行未改动57357457557657757857958058158258358458558658758858959059138 行未改动280 行未改动全部展开#include<bits/stdc++.h>using namespace std;int n, N, a[105], b[105], status[105], L[105], R[105], aL[105], aR[105];int tim, scnt, dfn[205], low[205], col[205], ins[205], pos[205], ans;vector<int> LLL, RRR, LLR, LRR, LLM, MRR, LMM, MMR, MMM, LMR;//十种情况vector<int> G[205], vec[205];stack<int> S;void genans(int now){if (ans == -1)3 行未改动}void add(int u, int v){G[u].emplace_back(v);}void clr(){//清空LLL.clear();RRR.clear();LLR.clear();LRR.clear();25 行未改动dfn[u] = low[u] = ++tim;S.push(u);ins[u] = 1;for (auto v : G[u]){if (!dfn[v]){//树边Tarjan(v);low[u] = min(low[u], low[v]);}else if (ins[v])low[u] = min(low[u], dfn[v]);18 行未改动status[i] = 2;else if (i > r)status[i] = 3;}for (int i = 1; i <= N; i++){//处理出每种颜色的位置vec[b[i]].emplace_back(i);if (status[i] == 1){if (!L[b[i]])L[b[i]] = i;}else if (status[i] == 3)2 行未改动aL[a[i]] = i;aR[a[i]] = i;}}int GetType(int i){//获取一个颜色的类型int cntL = 0, cntM = 0, cntR = 0;for (int pos : vec[i]){cntL += (status[pos] == 1);cntM += (status[pos] == 2);cntR += (status[pos] == 3);}if (cntL == 3)//按照十种情况分类return 1;//LLLelse if (cntR == 3)return 2;//RRRelse if (cntL == 2 && cntR == 1)20 行未改动cntL += (status[pos] == 1);cntM += (status[pos] == 2);cntR += (status[pos] == 3);}if (cntL == 3)//按照十种情况分类LLL.emplace_back(i);else if (cntR == 3)RRR.emplace_back(i);else if (cntL == 2 && cntR == 1)14 行未改动LMR.emplace_back(i);}}int check(int l, int r){//钦定 L 上 R 下,向上连为 [1, n],向下连为 [n + 1, 2n]genstatus(l, r);RubbishSorting(l, r);//判断 LLL 和 RRR 的无解情况if (LLL.size() || RRR.size())return -1;//LLL 最右侧的一个无法移到左边;RRR 对称,所以存在即无解//判断 LRR 和 LLR 的无解情况for (auto c1 : LRR)for (auto c2 : LLR){if (L[c1] < L[c2] && R[c1] < R[c2])return -1;}//样例很良心给了这个,这样会成环//判断 LMR 和 LLR/LRR 的情况for (auto c1 : LMR){for (auto c2 : LLR){if (L[c1] < L[c2] && R[c1] < R[c2]){//必须向上连add(c1 + n, c1);}}for (auto c2 : LRR){if (L[c2] < L[c1] && R[c2] < R[c1]){//必须向下连add(c1, c1 + n);}}}//通过 LLR 和 LRR 可以唯一确定 LMR 的方向//判断 LMR 和 LMR 的情况for (auto c1 : LMR){for (auto c2 : LMR){if (L[c1] < L[c2] && R[c1] < R[c2])add(c1 + n, c2 + n);//左边向下,右边不能向上,否则成环if (L[c2] < L[c1] && R[c2] < R[c1])add(c1, c2);//右边向上,左边不能向下,否则成环}}//两个 LMR 需要同向//判断 MMM、LLM、MRR、LMM、MMR 是否合法int p = 1;for (int i = l; i <= r; i++){//中间保留部分int typ = GetType(b[i]);if (typ == 9){//MMMwhile (p <= N && a[p] != b[i])p++;31 行未改动p++;}}//处理出每个 M 在 a 中对应的位置// 判断 LMR 的 M 限制for (int i = l; i <= r; i++){int x = GetType(b[i]);if (x != 10)continue;13 行未改动//当前位置应该在 (ml, mr) 区间内if (aL[b[i]] > mr || aR[b[i]] < ml)return -1;if (aL[b[i]] < ml){//向下连add(b[i], b[i] + n);}if (aR[b[i]] > mr){//向上连add(b[i] + n, b[i]);}}//基于 M 的位置限制// 显然有 LMR 和 LMR 之间的 M 不能相互越位,进行判断for (int i = l; i <= r; i++){int x = GetType(b[i]);if (x != 10)18 行未改动}}}//一定要搞清楚了//Tarjan 主体for (int i = 1; i <= n; i++)if (!dfn[i])Tarjan(i);// 判断是否有解for (int i = 1; i <= n; i++)if (col[i] == col[i + n])return -1;//矛盾了return N - (r - l + 1);}int main(){ios::sync_with_stdio(false);38 行未改动280 行未改动全部展开2812822832842852862872882893 行未改动29329429529629729829930030125 行未改动32732832933033133233333433518 行未改动3543553563573583593603613623632 行未改动36636736836937037137237337437537637737837938038120 行未改动40240340440540640740840941014 行未改动42542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148231 行未改动51451551651751851952052152252313 行未改动53753853954054154254354454554654754854955055155255355418 行未改动57357457557657757857958058158258358458558658758858959059138 行未改动280 行未改动全部展开#include<bits/stdc++.h>using namespace std;int n, N, a[105], b[105], status[105], L[105], R[105], aL[105], aR[105];int tim, scnt, dfn[205], low[205], col[205], ins[205], pos[205], ans;vector<int> LLL, RRR, LLR, LRR, LLM, MRR, LMM, MMR, MMM, LMR;//十种情况vector<int> G[205], vec[205];stack<int> S;void genans(int now){if (ans == -1)3 行未改动}void add(int u, int v){G[u].emplace_back(v);}void clr(){//清空LLL.clear();RRR.clear();LLR.clear();LRR.clear();25 行未改动dfn[u] = low[u] = ++tim;S.push(u);ins[u] = 1;for (auto v : G[u]){if (!dfn[v]){//树边Tarjan(v);low[u] = min(low[u], low[v]);}else if (ins[v])low[u] = min(low[u], dfn[v]);18 行未改动status[i] = 2;else if (i > r)status[i] = 3;}for (int i = 1; i <= N; i++){//处理出每种颜色的位置vec[b[i]].emplace_back(i);if (status[i] == 1){if (!L[b[i]])L[b[i]] = i;}else if (status[i] == 3)2 行未改动aL[a[i]] = i;aR[a[i]] = i;}}int GetType(int i){//获取一个颜色的类型int cntL = 0, cntM = 0, cntR = 0;for (int pos : vec[i]){cntL += (status[pos] == 1);cntM += (status[pos] == 2);cntR += (status[pos] == 3);}if (cntL == 3)//按照十种情况分类return 1;//LLLelse if (cntR == 3)return 2;//RRRelse if (cntL == 2 && cntR == 1)20 行未改动cntL += (status[pos] == 1);cntM += (status[pos] == 2);cntR += (status[pos] == 3);}if (cntL == 3)//按照十种情况分类LLL.emplace_back(i);else if (cntR == 3)RRR.emplace_back(i);else if (cntL == 2 && cntR == 1)14 行未改动LMR.emplace_back(i);}}int check(int l, int r){//钦定 L 上 R 下,向上连为 [1, n],向下连为 [n + 1, 2n]genstatus(l, r);RubbishSorting(l, r);//判断 LLL 和 RRR 的无解情况if (LLL.size() || RRR.size())return -1;//LLL 最右侧的一个无法移到左边;RRR 对称,所以存在即无解//判断 LRR 和 LLR 的无解情况for (auto c1 : LRR)for (auto c2 : LLR){if (L[c1] < L[c2] && R[c1] < R[c2])return -1;}//样例很良心给了这个,这样会成环//判断 LMR 和 LLR/LRR 的情况for (auto c1 : LMR){for (auto c2 : LLR){if (L[c1] < L[c2] && R[c1] < R[c2]){//必须向上连add(c1 + n, c1);}}for (auto c2 : LRR){if (L[c2] < L[c1] && R[c2] < R[c1]){//必须向下连add(c1, c1 + n);}}}//通过 LLR 和 LRR 可以唯一确定 LMR 的方向//判断 LMR 和 LMR 的情况for (auto c1 : LMR){for (auto c2 : LMR){if (L[c1] < L[c2] && R[c1] < R[c2])add(c1 + n, c2 + n);//左边向下,右边不能向上,否则成环if (L[c2] < L[c1] && R[c2] < R[c1])add(c1, c2);//右边向上,左边不能向下,否则成环}}//两个 LMR 需要同向//判断 MMM、LLM、MRR、LMM、MMR 是否合法int p = 1;for (int i = l; i <= r; i++){//中间保留部分int typ = GetType(b[i]);if (typ == 9){//MMMwhile (p <= N && a[p] != b[i])p++;31 行未改动p++;}}//处理出每个 M 在 a 中对应的位置// 判断 LMR 的 M 限制for (int i = l; i <= r; i++){int x = GetType(b[i]);if (x != 10)continue;13 行未改动//当前位置应该在 (ml, mr) 区间内if (aL[b[i]] > mr || aR[b[i]] < ml)return -1;if (aL[b[i]] < ml){//向下连add(b[i], b[i] + n);}if (aR[b[i]] > mr){//向上连add(b[i] + n, b[i]);}}//基于 M 的位置限制// 显然有 LMR 和 LMR 之间的 M 不能相互越位,进行判断for (int i = l; i <= r; i++){int x = GetType(b[i]);if (x != 10)18 行未改动}}}//一定要搞清楚了//Tarjan 主体for (int i = 1; i <= n; i++)if (!dfn[i])Tarjan(i);// 判断是否有解for (int i = 1; i <= n; i++)if (col[i] == col[i + n])return -1;//矛盾了return N - (r - l + 1);}int main(){ios::sync_with_stdio(false);38 行未改动