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 ...
随机推荐
- Android基础总结+SQlite数据库【申明:来源于网络】
Android基础总结+SQlite数据库[申明:来源于网络] 基础总结篇之一:Activity生命周期:http://blog.csdn.net/liuhe688/article/details/6 ...
- ASP.NET MVC中使用FluentValidation验证实体(转载)
1.FluentValidation介绍 FluentValidation是与ASP.NET DataAnnotataion Attribute验证实体不同的数据验证组件,提供了将实体与验证分离开来的 ...
- etcd集群的搭建
由于最近在学习kubernetes,etcd作为kubernetes集群的主数据库,必须先启动. etcds实例名称 IP地址 Hostname etcd 1 192.168.142.161 kube ...
- luogu3978 [TJOI2015]概率论
题目链接:洛谷 题目大意:求所有$n$个点的有根二叉树的叶子节点数总和/$n$个点的有根二叉树的个数. 数据范围:$n\leq 10^9$ 生成函数神题!!!!(我只是来水博客的) 首先$n$个点的有 ...
- java+tomcat开发环境搭建
java+tomcat开发环境搭建 一.jdk环境变量设置 ...........这里省略n个字............. 二.tomcat环境变量设置 安装好tomcat后 1.新建环境变量: CA ...
- Mysql数据库配置参数详解大全
名称 是否需要重启 值 允许值 描述 auto_increment_increment 否 1 1-65,535 auto_increment_increment和auto_increment_off ...
- SQLAlchemy(包含有Flask-Migrate知识点)
what's the SQLAlchemy SQLAlchemy是一个基于Python实现的ORM框架.该框架建立在 DB API之上,使用关系对象映射进行数据库操作,简言之便是:将类和对象转换成SQ ...
- 020-并发编程-java.util.concurrent之-jdk6/7/8中ConcurrentHashMap、HashMap分析
一.概述 哈希表(hash table)也叫散列表,是一种非常重要的数据结构,应用场景及其丰富,许多缓存技术(比如memcached)的核心其实就是在内存中维护一张大的哈希表. 是根据关键码值(Key ...
- python基础(15)-socket网络编程&socketserver
socket 参数及方法说明 初始化参数 sk = socket.socket(参数1,参数2,参数3) 参数1:地址簇 socket.AF_INET IPv4(默认) socket.AF_INET6 ...
- 56.关于vue项目的seo问题
不可否定的是,vue现在火.但是在实际项目中,特别是像一下交互网站,我们不可避免会考虑到的是seo问题,这直接关系到我们网站的排名,很多人说用vue搭建的网站不能做优化,那我们真的要放弃vue,放弃前 ...