2018 ACM-ICPC, Syrian Collegiate Programming Contest
2018 ACM-ICPC, Syrian Collegiate Programming Contest
水题
水题
C 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; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e8+7; const double esp = 1e-8; 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=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } /*-----------------------showtime----------------------*/ const int maxn = 2e6+9; 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=0; 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=0; 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+1] == '.')uni(i,i+1); } else if(str[i] == '.'){ if(i<n&&str[i+1] == '.')uni(i,i+1); if(i<n&&str[i+1] == 'o')uni(i,i+1); } } if(abs(s-t) == 1) { puts("-1"); continue; } int ans = inf, a[4]; for(int i=0; i<16; i++){ int m = i; for(int j=0; j<4; j++){ a[j] = m% 2; m = m/2; } int cnt = 0; int flag = 1; int tmp[2]; tmp[0] = tmp[1] = -1; if(s-1>=0&&str[s-1] =='.'){ if(a[0] == 0)cnt++; else { int f = find(s-1); tmp[0]=f; } } else if(s-1>=0&&str[s-1] == 'o'){ int f = find(s-1); tmp[0]=f; } if(s+1 < n && str[s+1] =='.'){ if(a[1] == 0)cnt++; else { int f = find(s+1); tmp[1]=f; } } else if(s+1<n && str[s+1] =='o'){ int f = find(s+1); tmp[1] = f; } //t if(t-1>=0&&str[t-1] =='.'){ if(a[2] == 0)cnt++; else { int f = find(t-1); if(f == tmp[0] || f == tmp[1])flag = 0; } } else if(t-1>=0&&str[t-1] == 'o'){ int f = find(t-1); if(f == tmp[0] || f == tmp[1])flag = 0; } if(t+1<n&&str[t+1] =='.'){ if(a[3] == 0)cnt++; else { int f = find(t+1); if(f == tmp[0] || f == tmp[1])flag = 0; } } else if(t+1<n&&str[t+1] == 'o'){ int f = find(t+1); if(f == tmp[0] || f == tmp[1])flag = 0; } if(flag) {ans = min(ans, cnt); if(ans == 1) debug(i); } } if(ans >= inf) puts("-1"); else printf("%d\n", ans); } return 0; }
思路: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=500+10; const int mod=1e9+7; 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(0); cout.tie(0); int T; cin>>T; while(T--) { int l,r; cin>>l>>r; for(int i=0;i<r;i++) cin>>p[i]; for(int i=0;i<l;i++) cin>>a[i]; for(int i=0;i<r;i++) cin>>dp[i],c[i]=dp[i]; for(int h=l-1;h>=0;h--) { for(int i=0;i<r;i++) { if(a[h][i]=='\\' || a[h][i]=='/') { if(i==0) { if(c[i]<c[i+1]) dp[i]=c[i+1]; else dp[i]=c[i]; } else if(i==r-1) { if(c[i-1]>c[i]) dp[i]=c[i-1]; else dp[i]=c[i]; } else { if(c[i]>=c[i+1] && c[i]>=c[i-1] ) { dp[i]=c[i]; continue; } else { if(c[i+1]>=c[i-1]) dp[i]=c[i+1]; if(c[i+1]<c[i-1]) dp[i]=c[i-1]; } } } } for(int i=0;i<r;i++) c[i]=dp[i]; } int ans=0; for(int i=0;i<r;i++) { ans+=p[i]*c[i]; } cout<<ans<<endl; } }
E 2Nodes
F 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 + 10; const int INF = 0x7f7f7f7f; int cnt[N]; int dp[N], pre[N]; char s[105]; 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 = 1<<t; for (int i = 0; i < up; i++) dp[i] = INF, cnt[i] = 0; for (int i = 1; i <= n; i++) { scanf("%s", s); int st = 0; for (int j = 0; j < t; j++) st = st*2 + s[j]-'0'; cnt[st]++; } for (int i = 0; i < t; i++) { for (int mask = 0; mask < up; mask++) { if(mask&(1<<i)) cnt[mask^(1<<i)] += cnt[mask]; } } dp[0] = 0; for (int i = 0; i < up; i++) { for (int j = 0; j < t; j++) { if(!(i&(1<<j))) { if(dp[i] + cnt[i] < dp[i|(1<<j)]) { dp[i|(1<<j)] = dp[i] + cnt[i]; pre[i|(1<<j)] = i; } else if(dp[i] + cnt[i] == dp[i|(1<<j)]) { if(i > pre[i|(1<<j)]) pre[i|(1<<j)] = i; } } } } vc.clear(); int k = up-1; while(k) { for (int i = 0; i < t; i++) { if((k&(1<<i)) != (pre[k]&(1<<i))) { vc.pb(i); break; } } k = pre[k]; } reverse(vc.begin(), vc.end()); printf("%d\n", dp[up-1]); for (int v : vc) printf("%d ", t-v); printf("\n"); } return 0; }
思路:模拟
代码:
#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 + 5; 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 = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) scanf("%d", &b[i]); for (int i = 1; i <= n; i++) { if(a[i] == 0) { vc.pb(i); } else if(b[i] == 0) { t.pb({a[i]-b[i], i}); } else { vcc.pb(i); } } bool f = false; sort(t.begin(), t.end()); for (int i = 0; i < t.size(); i++) { if(vc.size() >= t[i].fi) { for (int j = 0; 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() == 1) { printf("-1\n"); continue; } for (int u : vcc) { int sz = a[u] - b[u]; if(vc.size() >= sz) { for (int j = 0; j < sz; j++) { e.pb({vc[j], u}); } } else { f = true; break; } } if(f) { printf("-1\n"); continue; } for (int i = 0; i < vcc.size(); i++) { b[vcc[i]]--; if(i+1 == vcc.size()) { e.pb({vcc[i], vcc[0]}); } else { e.pb({vcc[i], vcc[i+1]}); } } int sz = vcc.size(); for (int i = 0; i < sz; i++) { if(!b[vcc[i]]) continue; if(sz-2 >= b[vcc[i]]) { int cnt = 0; for (int j = 0; j < sz; j++) { if(j != i && j != (i-1+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 0; }
代码:
//#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; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e8+7; const double esp = 1e-8; 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=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } /*-----------------------showtime----------------------*/ const int maxn = 2e5+9; 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=1; i<=n; i++) scanf("%d", &a[i]); ll sum = 0; memset(in, 0, sizeof(in)); memset(out, 0, sizeof(out)); for(int i=1; i<=m; i++) { int u,v; scanf("%d%d", &u, &v); sum+=abs(a[u] - a[v]); in[u]++,out[v]++; } int flag = 1; for(int i=1; i<=n; i++) if(in[i]!=out[i]) flag = 0; if(!flag) puts("-1"); else printf("%I64d\n", sum); } return 0; }
思路:三分套三分求最小圆覆盖
代码:
#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 = 333; const double eps = 1e-7; 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 = 0; for (int i = 0; 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[0] = dy[0] = 0.0; for (int i = 1; i <= n; i++) scanf("%lf %lf", &dx[i], &dy[i]); for (int i = 2; i <= n; i++) dx[i] += dx[i-1], dy[i] += dy[i-1]; 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 0; }
思路:贪心模拟
代码:
#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 + 5; 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 = 1; i <= k; i++) vc[i].clear(); for (int i = 1; i <= n+m; i++) now[i].clear(); while(!q1.empty()) q1.pop(); while(!q2.empty()) q2.pop(); for (int i = 1; i <= n; i++) { scanf("%d %d", &t, &p); vc[p].pb(t); } for (int i = 1; i <= k; i++) { sort(vc[i].begin(), vc[i].end()); now[vc[i][0]].pb(i); for (int j = 1; j < vc[i].size(); j++) { if(vc[i][j] == vc[i][0]) vc[i][j]++; else break; } } int up = m + n, ans = 0; for (int i = 1; i <= up; i++) { if((int)now[i].size() > 0) { for (int ty : now[i]) { if(vc[ty].size() > 1) q1.push(pii{vc[ty][1], 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 = 1; 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 0; }
代码:
//#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; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e8+7; const double esp = 1e-8; 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=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } /*-----------------------showtime----------------------*/ const int maxn = 1e6+9; int mx; stack<int>s; int a[maxn],ans[maxn]; int h[maxn],tot; struct node{ int nx,v; }e[4*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[6] = {0}; for(int i=h[u]; ~i; i = e[i].nx){ int v = e[i].v; vis[ans[v]] = true; } for (int i = 1; i <= 5; 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, 0, sizeof(ans)); tot = 0; for(int i=1; i<=n; i++) scanf("%d", &a[i]),h[i] = -1; while(!s.empty()) s.pop(); s.push(1); mx = 1; ans[1] = 1; for(int i=2; i<=n; i++){ int x = a[i]; int cnt = 0; 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-1; i>=1; 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=1; i<=n; i++) if(!ans[i])dfs(i); printf("%d\n", mx); for(int i=1; i<=n; i++) printf("%d ", ans[i]); puts(""); } return 0; }