답안 #974064

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
974064 2024-05-02T17:32:21 Z c2zi6 Stranded Far From Home (BOI22_island) C++14
35 / 100
446 ms 64528 KB
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define replr(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define reprl(i, a, b) for (int i = int(a); i >= int(b); --i)
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define mkp(a, b) make_pair(a, b)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VPI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<VPI> VVPI;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<VL> VVL;
typedef vector<VVL> VVVL;
typedef vector<VPL> VVPL;
template<class T> T setmax(T& a, T b) {if (a < b) return a = b; return a;}
template<class T> T setmin(T& a, T b) {if (a < b) return a; return a = b;}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T>
using indset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int n, m;
VI s;
VVI gp;
VI ans;

// n, m <= 2000
namespace TEST1 {
	void main() {
		rep(r, n) {
			VI vis(n);
			ll sum = s[r];
			vis[r] = true;
			priority_queue<PII> pq;
			for (int v : gp[r]) pq.push({-s[v], v});
			while (pq.size()) {
				int u = pq.top().ss;
				pq.pop();
				if (vis[u]) continue;
				if (sum >= s[u]) {
					sum += s[u];
					vis[u] = true;
					for (int v : gp[u]) if (!vis[v]) pq.push({-s[v], v});
				} else {
					break;
				}
			}
			bool kexni = true;
			rep(u, n) if (!vis[u]) kexni = false;
			ans[r] = kexni;
		}
	}
}

// tree 
namespace TEST2 {
	bool check() {
		bool ret = true;
		replr(i, 1, n-1) if (s[i] > s[i-1]) ret = false;
		rep(u, n) if (u) {
			int cnt = 0;
			for (int v : gp[u]) if (v < u) cnt++;
			if (cnt != 1) ret = false;
		}
		return ret;
	}

	VL subsum;
	VI par;
	VI vat;
	void dfs1(int u = 0, int p = -1) {
		par[u] = p;
		subsum[u] = s[u];
		for (int v : gp[u]) if (v != p) {
			dfs1(v, u);
			subsum[u] += subsum[v];
		}
	}
	void dfs2(int u = 0, int p = -1) {
		if (u && vat[p]) vat[u] = true;
		for (int v : gp[u]) if (v != p) dfs2(v, u);
	}
	void main() {
		subsum = VL(n);
		par = VI(n);
		vat = VI(n);
		dfs1();
		replr(u, 1, n-1) {
			if (s[par[u]] > subsum[u]) vat[u] = true;
		}
		dfs2();
		rep(i, n) ans[i] = !vat[i];
	}
}

// cycle
namespace TEST3 {
	void main() {
		VPI sind;
		rep(i, n) sind.pb({s[i], i});
		sort(all(sind));
		reverse(all(sind));
		vector<pair<int, VI>> order;
		rep(i, n) {
			if (i == 0 || sind[i].ff != sind[i-1].ff) {
				order.pb({sind[i].ff, VI(0)});
			}
			order.back().ss.pb(sind[i].ss);
		}
		VL pref(2*n+1);
		replr(i, 1, 2*n) pref[i] = pref[i-1] + s[(i-1)%n];
		auto sum = [&](int l, int r) {
			if (l <= r) return pref[r+1] - pref[l];
			return pref[r+1+n] - pref[l];
		};
		set<int> ind;
		for (auto&[val, vec] : order) {
			for (int cur : vec) if (ind.size() == 0) ans[cur] = true;
			else {
				int r = (*ind.upper_bound(cur) + n) % n;
				int l = (*prev(ind.lower_bound(cur)) + n) % n;
				ll hav = sum(l+1, r-1);
				if (hav > s[l] && ans[l]) ans[cur] = true;
				if (hav > s[r] && ans[r]) ans[cur] = true;
			}
			for (int cur : vec) {
				ind.insert(cur-n);
				ind.insert(cur);
				ind.insert(cur+n);
			}
		}
	}
}

void solve() {
	cin >> n >> m;
	s = VI(n);
	for (int& x : s) cin >> x;
	gp = VVI(n);
	rep(i, m) {
		int u, v;
		cin >> u >> v;
		u--, v--;
		gp[u].pb(v);
		gp[v].pb(u);
	}
	ans = VI(n);
	if (n <= 2000 && m <= 2000) TEST1::main();
	else if (TEST2::check()) TEST2::main();
	else TEST3::main();
	for (int x : ans) cout << x; cout << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    int t = 1;
    /* cin >> t; */
    while (t--) solve();
}

Compilation message

island.cpp: In function 'void TEST3::main()':
island.cpp:128:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  128 |   for (auto&[val, vec] : order) {
      |             ^
island.cpp: In function 'void solve()':
island.cpp:162:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  162 |  for (int x : ans) cout << x; cout << endl;
      |  ^~~
island.cpp:162:31: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  162 |  for (int x : ans) cout << x; cout << endl;
      |                               ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 177 ms 588 KB Output is correct
5 Correct 133 ms 608 KB Output is correct
6 Correct 285 ms 600 KB Output is correct
7 Correct 178 ms 604 KB Output is correct
8 Correct 137 ms 348 KB Output is correct
9 Correct 196 ms 604 KB Output is correct
10 Correct 46 ms 600 KB Output is correct
11 Correct 45 ms 600 KB Output is correct
12 Correct 53 ms 604 KB Output is correct
13 Correct 80 ms 600 KB Output is correct
14 Correct 81 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 98 ms 23944 KB Output is correct
4 Correct 136 ms 23208 KB Output is correct
5 Correct 94 ms 18256 KB Output is correct
6 Correct 100 ms 19028 KB Output is correct
7 Correct 127 ms 19028 KB Output is correct
8 Correct 108 ms 19188 KB Output is correct
9 Correct 83 ms 18772 KB Output is correct
10 Correct 69 ms 19248 KB Output is correct
11 Correct 67 ms 19208 KB Output is correct
12 Correct 89 ms 18004 KB Output is correct
13 Correct 94 ms 30460 KB Output is correct
14 Correct 111 ms 30976 KB Output is correct
15 Correct 112 ms 31448 KB Output is correct
16 Correct 69 ms 30804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 712 KB Output is correct
2 Correct 446 ms 61788 KB Output is correct
3 Correct 444 ms 60128 KB Output is correct
4 Correct 95 ms 33104 KB Output is correct
5 Correct 208 ms 49496 KB Output is correct
6 Correct 429 ms 64528 KB Output is correct
7 Correct 300 ms 62900 KB Output is correct
8 Correct 288 ms 64084 KB Output is correct
9 Correct 76 ms 32108 KB Output is correct
10 Correct 215 ms 56252 KB Output is correct
11 Correct 214 ms 50116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 246 ms 49096 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 177 ms 588 KB Output is correct
5 Correct 133 ms 608 KB Output is correct
6 Correct 285 ms 600 KB Output is correct
7 Correct 178 ms 604 KB Output is correct
8 Correct 137 ms 348 KB Output is correct
9 Correct 196 ms 604 KB Output is correct
10 Correct 46 ms 600 KB Output is correct
11 Correct 45 ms 600 KB Output is correct
12 Correct 53 ms 604 KB Output is correct
13 Correct 80 ms 600 KB Output is correct
14 Correct 81 ms 600 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 98 ms 23944 KB Output is correct
18 Correct 136 ms 23208 KB Output is correct
19 Correct 94 ms 18256 KB Output is correct
20 Correct 100 ms 19028 KB Output is correct
21 Correct 127 ms 19028 KB Output is correct
22 Correct 108 ms 19188 KB Output is correct
23 Correct 83 ms 18772 KB Output is correct
24 Correct 69 ms 19248 KB Output is correct
25 Correct 67 ms 19208 KB Output is correct
26 Correct 89 ms 18004 KB Output is correct
27 Correct 94 ms 30460 KB Output is correct
28 Correct 111 ms 30976 KB Output is correct
29 Correct 112 ms 31448 KB Output is correct
30 Correct 69 ms 30804 KB Output is correct
31 Correct 0 ms 712 KB Output is correct
32 Correct 446 ms 61788 KB Output is correct
33 Correct 444 ms 60128 KB Output is correct
34 Correct 95 ms 33104 KB Output is correct
35 Correct 208 ms 49496 KB Output is correct
36 Correct 429 ms 64528 KB Output is correct
37 Correct 300 ms 62900 KB Output is correct
38 Correct 288 ms 64084 KB Output is correct
39 Correct 76 ms 32108 KB Output is correct
40 Correct 215 ms 56252 KB Output is correct
41 Correct 214 ms 50116 KB Output is correct
42 Correct 0 ms 344 KB Output is correct
43 Incorrect 246 ms 49096 KB Output isn't correct
44 Halted 0 ms 0 KB -