Loading

P1053 [NOIP2005 提高组] 篝火晚会

\(\text{slolution}\)

强调一下本人还是很乐观的(qwq),并没有想象中那么消极。

明显我们先建出一个环,然后正着反着求一遍差值,统计差值里个数的最大值就好了,具体看代码。

#define debug
#include <bits/stdc++.h>

using namespace std;

#define fir first
#define sec second
#define int long long 

char _c; bool _f; template < class T > inline void read ( T &x ) {
	_f = 0, x = 0;
	while ( _c = getchar (), !isdigit (_c) ){
		if ( _c == '-' ) { _f = 1; }
	}
	while ( isdigit (_c) ){
		x = x * 10 + _c - '0', _c = getchar ();
		if (_f) { x = -x; }
	}
}

const int N = 1e5 + 5;

int n;
int l[N], r[N], a[N], t[N];

void Solve () {
	cin >> n;
	for ( int i = 1; i <= n; i ++ ) {
		cin >> l[i] >> r[i];
	}
	a[1] = 1, a[2] = r[1];
	for ( int i = 2; i < n; i ++ ) {
		if ( a[i - 1] == l[a[i]] ) {
			a[i + 1] = r[a[i]];
		}
		else if ( a[i - 1] == r[a[i]] ) {
			a[i + 1] = l[a[i]];
		}
		else {
			cout << -1;
			return ;
		}
	}
	int maxi = 0;
	for ( int i = 1; i <= n; i ++ ) {
		t[( a[i] - i + n ) % n] ++;
		maxi = max ( maxi, t[( a[i] - i + n ) % n] );
	}
	reverse ( a + 1, a + 1 + n );
	memset ( t, 0, sizeof ( t ) );
	for ( int i = 1; i <= n; i ++ ) {
		t[( a[i] - i + n ) % n] ++;
		maxi = max ( maxi, t[( a[i] - i + n ) % n] );
	}
	cout << n - maxi;
}

signed main () {
#ifdef debug
	freopen ( "Code.in", "r", stdin );
	freopen ( "Code.out", "w", stdout );
#endif
	Solve ();
	return 0;
}

很神奇 qwq。

posted @ 2023-08-04 16:06  Alexande  阅读(82)  评论(0)    收藏  举报