2018 ACM-ICPC, Syrian Collegiate Programming Contest

时间:2022-04-05 12:31:40

2018 ACM-ICPC, Syrian Collegiate Programming Contest

Hello SCPC 2018!

水题

Binary Hamming

水题

Portals

思路:并查集维护连通性

代码:

//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
// #pragma GCC diagnostic error "-std=c++11"
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3) #include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c);
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e8+;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /*-----------------------showtime----------------------*/ const int maxn = 2e6+;
char str[maxn];
int fa[maxn]; int find(int x){
if(fa[x] == x)return x;
return fa[x] = find(fa[x]);
}
void uni(int x,int y){
int fx = find(x);
int fy = find(y);
if(fx == fy) return ;
fa[fx] = fy;
}
int main(){
freopen("portals.in", "r", stdin);
int T; scanf("%d", &T);
while(T--){
int n; scanf("%d%s", &n, str);
for(int i=; i<=n; i++) fa[i] = i;
int s,t; /*
for(int i=0; i<n; i++){
if(str[i] == 's'){
s = i;
str[i] = '.';
}
if(str[i] == 'e'){
t = i;
str[i] = '.';
} }
*/
for(int i=; i<n; i++){
if(str[i] == 's'){
s = i;
}
if(str[i] == 'e'){
t = i;
} if(str[i] == 'o'){
uni(i,n);
if(i<n&&str[i+] == '.')uni(i,i+);
}
else if(str[i] == '.'){
if(i<n&&str[i+] == '.')uni(i,i+);
if(i<n&&str[i+] == 'o')uni(i,i+);
}
} if(abs(s-t) == ) {
puts("-1");
continue;
} int ans = inf, a[];
for(int i=; i<; i++){
int m = i;
for(int j=; j<; j++){
a[j] = m% ;
m = m/;
} int cnt = ;
int flag = ;
int tmp[];
tmp[] = tmp[] = -;
if(s->=&&str[s-] =='.'){
if(a[] == )cnt++;
else {
int f = find(s-);
tmp[]=f;
}
} else if(s->=&&str[s-] == 'o'){
int f = find(s-);
tmp[]=f;
} if(s+ < n && str[s+] =='.'){
if(a[] == )cnt++;
else {
int f = find(s+);
tmp[]=f;
}
}
else if(s+<n && str[s+] =='o'){
int f = find(s+);
tmp[] = f;
} //t
if(t->=&&str[t-] =='.'){
if(a[] == )cnt++;
else {
int f = find(t-);
if(f == tmp[] || f == tmp[])flag = ;
}
}
else if(t->=&&str[t-] == 'o'){
int f = find(t-);
if(f == tmp[] || f == tmp[])flag = ;
} if(t+<n&&str[t+] =='.'){
if(a[] == )cnt++;
else {
int f = find(t+);
if(f == tmp[] || f == tmp[])flag = ;
}
}
else if(t+<n&&str[t+] == 'o'){
int f = find(t+);
if(f == tmp[] || f == tmp[])flag = ;
} if(flag) {ans = min(ans, cnt);
if(ans == ) debug(i);
}
}
if(ans >= inf) puts("-1");
else printf("%d\n", ans);
} return ;
}

Carnival Slots

思路:dp

代码:

#include<bits/stdc++.h>
#define int long long
#define MAX(a,b,c) max(a,max(b,c))
#define MIN(a,b,c) min(a,min(b,c))
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef long long LL;
typedef unsigned long long ull;
typedef unsigned long long uLL;
using namespace std;
const int maxn=+;
const int mod=1e9+;
int gcd(int a,int b){return b ? gcd(b, a%b): a; }
int lowbit(int x) {return x&-x; }
char a[maxn][maxn];
int p[maxn];
int dp[maxn];
int c[maxn];
int32_t main()
{
freopen("balls.in","r",stdin); //重定向所有标准的输入为文件输入
//freopen("date.out","w",stdout);//重定向所有标准的输出为文件输出
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
int T; cin>>T;
while(T--)
{
int l,r; cin>>l>>r;
for(int i=;i<r;i++) cin>>p[i];
for(int i=;i<l;i++) cin>>a[i];
for(int i=;i<r;i++) cin>>dp[i],c[i]=dp[i];
for(int h=l-;h>=;h--)
{
for(int i=;i<r;i++)
{
if(a[h][i]=='\\' || a[h][i]=='/')
{
if(i==)
{
if(c[i]<c[i+]) dp[i]=c[i+];
else dp[i]=c[i];
}
else if(i==r-)
{
if(c[i-]>c[i]) dp[i]=c[i-];
else dp[i]=c[i];
}
else
{
if(c[i]>=c[i+] && c[i]>=c[i-] )
{
dp[i]=c[i];
continue;
}
else
{
if(c[i+]>=c[i-]) dp[i]=c[i+];
if(c[i+]<c[i-]) dp[i]=c[i-];
}
}
}
}
for(int i=;i<r;i++)
c[i]=dp[i];
}
int ans=;
for(int i=;i<r;i++)
{
ans+=p[i]*c[i];
}
cout<<ans<<endl;
}
}

2Nodes

Pretests

思路:sos dp求父集和,然后状压dp

代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define y1 y11
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define DEBUG
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define pdd pair<long double, long double>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head /*
sos dp
for (int i = 0; i <= k; i--) {
for (int mask = 0; mask < (1<<k); mask++ {
if(i == 0) dp[mask][i] = cnt[mask];
if(mask&(1<<i)) dp[mask][i] = dp[mask^(1<<i)][i-1] + dp[mask][i-1];
else dp[mask][i] = dp[mask][i-1];
}
}
*/
const int N = 2e6 + ;
const int INF = 0x7f7f7f7f;
int cnt[N];
int dp[N], pre[N];
char s[];
vector<int> vc;
int main() {
freopen("tests.in", "r", stdin);
int T, t, n;
scanf("%d", &T);
while(T--) {
scanf("%d %d", &t, &n);
int up = <<t;
for (int i = ; i < up; i++) dp[i] = INF, cnt[i] = ;
for (int i = ; i <= n; i++) {
scanf("%s", s);
int st = ;
for (int j = ; j < t; j++) st = st* + s[j]-'';
cnt[st]++;
} for (int i = ; i < t; i++) {
for (int mask = ; mask < up; mask++) {
if(mask&(<<i)) cnt[mask^(<<i)] += cnt[mask];
}
} dp[] = ;
for (int i = ; i < up; i++) {
for (int j = ; j < t; j++) {
if(!(i&(<<j))) {
if(dp[i] + cnt[i] < dp[i|(<<j)]) {
dp[i|(<<j)] = dp[i] + cnt[i];
pre[i|(<<j)] = i;
}
else if(dp[i] + cnt[i] == dp[i|(<<j)]) {
if(i > pre[i|(<<j)]) pre[i|(<<j)] = i;
}
}
}
}
vc.clear();
int k = up-;
while(k) {
for (int i = ; i < t; i++) {
if((k&(<<i)) != (pre[k]&(<<i))) {
vc.pb(i);
break;
}
}
k = pre[k];
}
reverse(vc.begin(), vc.end());
printf("%d\n", dp[up-]);
for (int v : vc) printf("%d ", t-v);
printf("\n");
}
return ;
}

Is Topo Logical?

思路:模拟

代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define pdd pair<long double, long double>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int N = 2e5 + ;
int a[N], b[N];
vector<int> vc, vcc;
vector<pii> t;
vector<pii> e;
int main() {
freopen("topo.in", "r", stdin);
int T, n;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
vc.clear();
vcc.clear();
t.clear();
e.clear();
for (int i = ; i <= n; i++) scanf("%d", &a[i]);
for (int i = ; i <= n; i++) scanf("%d", &b[i]);
for (int i = ; i <= n; i++) {
if(a[i] == ) {
vc.pb(i);
}
else if(b[i] == ) {
t.pb({a[i]-b[i], i});
}
else {
vcc.pb(i);
}
}
bool f = false;
sort(t.begin(), t.end());
for (int i = ; i < t.size(); i++) {
if(vc.size() >= t[i].fi) {
for (int j = ; j < t[i].fi; j++) {
e.pb({vc[j], t[i].se});
}
vc.pb(t[i].se);
}
else {
f = true;
break;
}
}
if(f) {
printf("-1\n");
continue;
}
if(vcc.size() == ) {
printf("-1\n");
continue;
}
for (int u : vcc) {
int sz = a[u] - b[u];
if(vc.size() >= sz) {
for (int j = ; j < sz; j++) {
e.pb({vc[j], u});
}
}
else {
f = true;
break;
}
}
if(f) {
printf("-1\n");
continue;
}
for (int i = ; i < vcc.size(); i++) {
b[vcc[i]]--;
if(i+ == vcc.size()) {
e.pb({vcc[i], vcc[]});
}
else {
e.pb({vcc[i], vcc[i+]});
}
}
int sz = vcc.size();
for (int i = ; i < sz; i++) {
if(!b[vcc[i]]) continue;
if(sz- >= b[vcc[i]]) {
int cnt = ;
for (int j = ; j < sz; j++) {
if(j != i && j != (i-+sz)%sz) {
cnt++;
e.pb({vcc[j], vcc[i]});
if(cnt == b[vcc[i]]) break;
}
}
}
else {
f = true;
break;
}
}
if(f) {
printf("-1\n");
continue;
}
printf("%d\n", e.size());
for (pii t :e) printf("%d %d\n", t.fi, t.se);
}
return ;
}

Bugged System

代码:

//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
// #pragma GCC diagnostic error "-std=c++11"
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3) #include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c);
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e8+;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /*-----------------------showtime----------------------*/ const int maxn = 2e5+;
int a[maxn],in[maxn],out[maxn];
int main(){
freopen("bugged.in", "r", stdin);
int T; scanf("%d", &T);
while(T--){
int n,m; scanf("%d%d", &n, &m);
for(int i=; i<=n; i++) scanf("%d", &a[i]);
ll sum = ;
memset(in, , sizeof(in));
memset(out, , sizeof(out)); for(int i=; i<=m; i++) {
int u,v;
scanf("%d%d", &u, &v);
sum+=abs(a[u] - a[v]);
in[u]++,out[v]++;
}
int flag = ;
for(int i=; i<=n; i++) if(in[i]!=out[i]) flag = ;
if(!flag) puts("-1");
else printf("%I64d\n", sum);
} return ;
}

Rise of the Robots

思路:三分套三分求最小圆覆盖

代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define pdd pair<long double, long double>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int N = ;
const double eps = 1e-;
double dx[N], dy[N];
int n;
double R, r, y;
double dis(double x1, double y1, double x2, double y2) {
return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
}
double chk2(double x, double y) {
double res = ;
for (int i = ; i <= n; i++) {
res = max(res, dis(x, y, dx[i], dy[i])+r);
}
return res;
}
double chk1(double x) {
double ly = -1e5, ry = 1e5, my1 = (ly+ly+ry)/3.0, my2 = (ly+ry+ry)/3.0;
while(ly + eps < ry) {
if(chk2(x, my1) > chk2(x, my2)) ly = my1;
else ry = my2;
my1 = (ly+ly+ry)/3.0, my2 = (ly+ry+ry)/3.0;
}
y = (my1+my2)/2.0;
return chk2(x, y);
}
int main() {
int T;
freopen("robots.in", "r", stdin);
scanf("%d", &T);
while(T--) {
scanf("%d %lf %lf", &n, &R, &r);
dx[] = dy[] = 0.0;
for (int i = ; i <= n; i++) scanf("%lf %lf", &dx[i], &dy[i]);
for (int i = ; i <= n; i++) dx[i] += dx[i-], dy[i] += dy[i-];
double lx = -1e5, rx = 1e5, mx1 = (lx+lx+rx)/3.0, mx2 = (lx+rx+rx)/3.0;
while(lx + eps < rx) {
if(chk1(mx1) > chk1(mx2)) lx = mx1;
else rx = mx2;
mx1 = (lx+lx+rx)/3.0, mx2 = (lx+rx+rx)/3.0;
}
double x = (mx1+mx2)/2.0;
chk1(x);
printf("%.8f %.8f\n", -x, -y);
}
return ;
}

Clarifications

思路:贪心模拟

代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define y1 y11
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define DEBUG
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define pdd pair<long double, long double>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int N = 2e5 + ;
const int INF = 0x3f3f3f3f;
vector<int> vc[N], now[N];
priority_queue<pii ,vector<pii>, greater<pii> > q1;
priority_queue<int ,vector<int>, greater<int> > q2;
int main() {
freopen("clar.in", "r", stdin);
int T, m, n, k, t, p;
scanf("%d", &T);
while(T--) {
scanf("%d %d %d", &m, &n, &k);
for (int i = ; i <= k; i++) vc[i].clear();
for (int i = ; i <= n+m; i++) now[i].clear();
while(!q1.empty()) q1.pop();
while(!q2.empty()) q2.pop();
for (int i = ; i <= n; i++) {
scanf("%d %d", &t, &p);
vc[p].pb(t);
}
for (int i = ; i <= k; i++) {
sort(vc[i].begin(), vc[i].end());
now[vc[i][]].pb(i);
for (int j = ; j < vc[i].size(); j++) {
if(vc[i][j] == vc[i][]) vc[i][j]++;
else break;
}
} int up = m + n, ans = ;
for (int i = ; i <= up; i++) {
if((int)now[i].size() > ) {
for (int ty : now[i]) {
if(vc[ty].size() > ) q1.push(pii{vc[ty][], ty});
else q1.push(pii{INF, ty});
}
}
if(q1.empty() && q2.empty()) continue;
ans = i;
if(!q1.empty()) {
pii p = q1.top();
q1.pop();
for (int j = ; j < vc[p.se].size(); j++) {
q2.push(vc[p.se][j]);
}
}
else if(!q2.empty()) {
int p = q2.top();
if(p <= i) q2.pop();
} if(!q2.empty()) {
int p = q2.top();
if(p <= i) q2.pop();
}
}
printf("%d\n", ans);
}
return ;
}

Tourists' Tour

代码:

//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
// #pragma GCC diagnostic error "-std=c++11"
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3) #include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c);
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e8+;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /*-----------------------showtime----------------------*/
const int maxn = 1e6+;
int mx;
stack<int>s;
int a[maxn],ans[maxn];
int h[maxn],tot;
struct node{
int nx,v;
}e[*maxn] ;
void add(int u,int v){
e[tot].v = v;
e[tot].nx = h[u];
h[u] = tot++;
}
void dfs(int u){
bool vis[] = {};
for(int i=h[u]; ~i; i = e[i].nx){
int v = e[i].v;
vis[ans[v]] = true;
}
for (int i = ; i <= ; i++) {
if(!vis[i]) {
ans[u] = i;
break;
}
}
mx = max(mx, ans[u]);
for(int i=h[u]; ~i; i = e[i].nx){
int v = e[i].v;
if(!ans[v]) dfs(v);
}
} int main(){
freopen("tour.in", "r", stdin);
int T,n; scanf("%d", &T);
while(T--){
scanf("%d", &n);
memset(ans, , sizeof(ans));
tot = ;
for(int i=; i<=n; i++) scanf("%d", &a[i]),h[i] = -;
while(!s.empty()) s.pop();
s.push();
mx = ;
ans[] = ;
for(int i=; i<=n; i++){
int x = a[i];
int cnt = ;
while(!s.empty() && x > a[s.top()]){
s.pop();
} if(!s.empty()) {
int u = s.top();
add(u,i);
add(i,u); }
s.push(i);
}
while(!s.empty()) s.pop();
s.push(n);
for(int i=n-; i>=; i--){
int x = a[i];
while(!s.empty() && x > a[s.top()]){
s.pop();
}
if(!s.empty()) {
int u = s.top();
add(u,i);
add(i,u);
}
s.push(i);
}
for(int i=; i<=n; i++)
if(!ans[i])dfs(i);
printf("%d\n", mx);
for(int i=; i<=n; i++) printf("%d ", ans[i]);
puts("");
}
return ;
}

Sad Meals