Submission #71492305


Source Code Expand

/* Code By WCM */
/*
Date:
大致思路:
复杂度:
期望得分:
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <vector>
#include <queue>
#include <map>
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define int long long

using namespace std;

inline int read();
void write(int);
void writeln(int);

const int N = 2e5 + 5;
int n, p[N], pos[N], G[N][2], du[N], fa[N], mx[N], f[N];
bool vis[N];
int getfa(int x) { return fa[x] == x ? x : fa[x] = getfa(fa[x]); }
void uni(int u, int v) {
	int fu = getfa(u), fv = getfa(v);
	if(fu == fv) return ;
	fa[fv] = fu, mx[fu] = max(mx[fu], mx[fv]);
}
int getmx(int x) { return mx[getfa(x)]; }
int Abs(int x) { return x < 0 ? -x : x; }

signed main() {

//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);

	n = read();
	for(int i = 1; i <= n; i++) p[i] = read(), pos[p[i]] = i;
	for(int i = 1; i <= n - 1; i++) {
		int u = p[i], v = p[i + 1];
		G[u][du[u]++] = v, G[v][du[v]++] = u;
	}
	for(int i = 1; i <= n; i++) fa[i] = mx[i] = i;
	for(int h = 1; h <= n; h++) {
		if(h > 1) {
			vis[h - 1] = 1;
			for(int j = 0; j < du[h - 1]; j++) if(vis[G[h - 1][j]]) uni(h - 1, G[h - 1][j]);
		}
		int res = 0;
		for(int j = 0; j < du[h]; j++) {
			int ni = G[h][j];
			if(ni < h) {
				int hh = getmx(ni);
				res = max(res, Abs(pos[h] - pos[hh]) + f[hh]);
			}
		}
		f[h] = res;
	}
	printf("%lld\n", f[n]);
	
//	printf("\nThe time used: ");
//	printf("%.2lfs",(double)clock()/CLOCKS_PER_SEC);

	return 0;

}

inline int read() {
	int res = 0, f = 1;
	char ch = getchar();
	while(ch < '0' || ch > '9') f = (ch == '-' ? -1 : 1), ch = getchar();
	while(ch >= '0' && ch <= '9') res = (res << 1) + (res << 3) + (ch ^ 48), ch = getchar();
	return res * f;
}

void write(int x) {
	static int sta[35];
	int top = 0;
	do { sta[top++] = x % 10, x /= 10; } while(x);
	while(top) putchar(sta[--top] ^ 48);
}

void writeln(int x) {
	write(x);
	putchar('\n');
}


Submission Info

Submission Time
Task F - Cat exercise
User WZwangchongming
Language C++23 (GCC 15.2.0)
Score 550
Code Size 2145 Byte
Status AC
Exec Time 17 ms
Memory 18652 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 550 / 550
Status
AC × 2
AC × 40
Set Name Test Cases
Sample example_00.txt, example_01.txt
All example_00.txt, example_01.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt
Case Name Status Exec Time Memory
example_00.txt AC 1 ms 3620 KiB
example_01.txt AC 1 ms 3876 KiB
hand_00.txt AC 11 ms 16532 KiB
hand_01.txt AC 11 ms 16248 KiB
hand_02.txt AC 11 ms 16412 KiB
hand_03.txt AC 12 ms 18652 KiB
hand_04.txt AC 11 ms 16248 KiB
hand_05.txt AC 1 ms 3732 KiB
hand_06.txt AC 1 ms 3876 KiB
hand_07.txt AC 11 ms 16532 KiB
random_00.txt AC 17 ms 16208 KiB
random_01.txt AC 17 ms 16420 KiB
random_02.txt AC 17 ms 16208 KiB
random_03.txt AC 17 ms 16472 KiB
random_04.txt AC 17 ms 16420 KiB
random_05.txt AC 17 ms 16392 KiB
random_06.txt AC 17 ms 16472 KiB
random_07.txt AC 17 ms 16392 KiB
random_08.txt AC 17 ms 16404 KiB
random_09.txt AC 17 ms 16420 KiB
random_10.txt AC 14 ms 16412 KiB
random_11.txt AC 15 ms 16404 KiB
random_12.txt AC 17 ms 16192 KiB
random_13.txt AC 16 ms 16404 KiB
random_14.txt AC 13 ms 16292 KiB
random_15.txt AC 17 ms 16476 KiB
random_16.txt AC 17 ms 16448 KiB
random_17.txt AC 13 ms 16412 KiB
random_18.txt AC 15 ms 16472 KiB
random_19.txt AC 14 ms 16248 KiB
random_20.txt AC 14 ms 16604 KiB
random_21.txt AC 13 ms 16248 KiB
random_22.txt AC 16 ms 16640 KiB
random_23.txt AC 13 ms 16420 KiB
random_24.txt AC 16 ms 16472 KiB
random_25.txt AC 13 ms 16576 KiB
random_26.txt AC 17 ms 16604 KiB
random_27.txt AC 14 ms 16676 KiB
random_28.txt AC 17 ms 16464 KiB
random_29.txt AC 17 ms 16604 KiB