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; //
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 ;
}
思路: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;
}
}
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 + ;
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 ;
}
思路:模拟
代码:
#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 ;
}
代码:
//#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 ;
}
思路:三分套三分求最小圆覆盖
代码:
#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 ;
}
思路:贪心模拟
代码:
#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 ;
}
代码:
//#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 ;
}
2018 ACM-ICPC, Syrian Collegiate Programming Contest的更多相关文章
- ACM ICPC, JUST Collegiate Programming Contest (2018) Solution
A:Zero Array 题意:两种操作, 1 p v 将第p个位置的值改成v 2 查询最少的操作数使得所有数都变为0 操作为可以从原序列中选一个非0的数使得所有非0的数减去它,并且所有数不能 ...
- ACM ICPC, Amman Collegiate Programming Contest (2018) Solution
Solution A:Careful Thief 题意:给出n个区间,每个区间的每个位置的权值都是v,然后找长度为k的区间,使得这个区间的所有位置的权值加起来最大,输出最大权值, 所有区间不重叠 思路 ...
- (寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest(爽题)
layout: post title: (寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest(爽题) author: " ...
- (寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest
layout: post title: (寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest author: "luow ...
- ICPC — International Collegiate Programming Contest Asia Regional Contest, Yokohama, 2018–12–09 题解
目录 注意!!此题解存在大量假算法,请各位巨佬明辨! Problem A Digits Are Not Just Characters 题面 题意 思路 代码 Problem B Arithmetic ...
- 2015 ACM Syrian Collegiate Programming Contest
A. My Friend of Misery 计算出答案的上下界即可. 时间复杂度$O(n)$. #include<bits/stdc++.h> using namespace std; ...
- 2018 ACM-ICPC, Syrian Collegiate Programming Contest F - Pretests SOS dp
#include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk mak ...
- Gym.102006:Syrian Collegiate Programming Contest(寒假自训第11场)
学习了“叙利亚”这个单词:比较温和的一场:几何的板子eps太小了,坑了几发. A .Hello SCPC 2018! 题意:给定一个排列,问它是否满足,前面4个是有序的,而且前面4个比后面的都小. 思 ...
- Gym 100952E&&2015 HIAST Collegiate Programming Contest E. Arrange Teams【DFS+剪枝】
E. Arrange Teams time limit per test:2 seconds memory limit per test:64 megabytes input:standard inp ...
随机推荐
- MVC的HTTP请求处理过程(IIS应用程序池、CLR线程池)
主要内容 本文讲解的是:服务器接受Http Request请求之后,是如何进入.Net CLR,从而进一步操作的. 我们大家都知道,IIS必须先接受请求,然后才能有机会进入CLR,但对请求(reque ...
- transition:all .2s
all属性实际上是所有CSS属性的缩写,表示,所有的CSS属性都怎样怎样.
- 苹果审核被拒,解析奔溃日志.txt转crash文件
1. 桌面新建一个文件夹,用来存放crash相关的东西.取名crash 2.下载苹果官方邮件里给的后缀名为 .txt 的被拒附件(这三个txt格式文件为苹果返回的崩溃日志文件),把这三个文件放在刚新建 ...
- -webkit-line-clamp、-webkit-box-orient vue 打包部署后不起作用??
场景分析:实际开发中,文字描述过长,需要两行或三行显示缩略显示: 实现过程: 实现过程遇到的问题:打包到线上后发现并没有-webkit-box-orient属性,导致省略号并没有按预期展示: 解决方法 ...
- WebService简单搭建和调用
http://www.cnblogs.com/JiYF/p/6164859.html https://www.cnblogs.com/JiYF/p/6169349.html #region Invok ...
- bugfree3.0.1-BUG解决方案修改
该篇内容来自文档“masterBugFree2.pdf”,记录在这里. 1.如何将解决方案改为中文 在\Bugfree\Lang\ZH_CN_UTF-8 \_COMMON.php 文件中做如下修改/* ...
- Redis入门到高可用(十四)—— bitmap
一.初步认识 二.API 1.setbit 2.getbit 3.bitcount 4.bitop 5.bitpos 三.位图使用 四. 使用经验
- Cartographer源码阅读(6):LocalTrajectoryBuilder和PoseExtrapolator
LocalTrajectoryBuilder意思是局部轨迹的构建,下面的类图中方法的参数没有画进去. 注意其中的三个类:PoseExtrapolator类,RealTimeCorrelativeSca ...
- Oracle数据库基础入门《二》Oracle内存结构
Oracle数据库基础入门<二>Oracle内存结构 Oracle 的内存由系统全局区(System Global Area,简称 SGA)和程序全局区(Program Global Ar ...
- Bukkit之yaml动态读取
在使用bukkit框架写插件的时候会经常使用到yml格式的文件来存储配置或者玩家数据,这里来说一下实现yml中数据的动态读写: 先来看一下yml文件中的内容结构 public boolean addB ...