답안 #247044

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
247044 2020-07-10T19:59:26 Z opukittpceno_hhr Drzava (COCI15_drzava) C++17
72 / 160
1000 ms 31980 KB
#ifndef LOCAL
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#endif
 
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cstring>
#include <unordered_set>
#include <unordered_map>
#include <numeric>
#include <ctime>
#include <bitset>
#include <complex>
#include <chrono>
#include <random>
#include <functional>
 
using namespace std;
 
typedef long double ld;
 
#define int long long
 
const int N = 5e4;
const int K = 50;
 
int n, k;
int X[N];
int Y[N];
int C[N];
 
int ids[N];
pair<int, int> block[N];
vector<int> by_block[N];
 
int used[N];
 
vector<pair<int, int>> seen;
 
int gt(pair<int, int> v) {
	auto it = lower_bound(seen.begin(), seen.end(), v);
	if (it != seen.end() && *it == v) {
		return (int)(it - seen.begin());
	}
	return (int)-1;
};
 
int hv[N];

void dfs(int i, int &pnt, int mid_int) {
	assert(!used[i]);
	hv[pnt++] = C[i];
	used[i] = 1;
	for (int dx = -1; dx <= 1; dx++) {
		for (int dy = -1; dy <= 1; dy++) {
			int bl_id = gt({block[i].first + dx, block[i].second + dy});
			if (bl_id == -1) continue;
			for (auto other : by_block[bl_id]) {
				if (!used[other] && (X[i] - X[other]) * (X[i] - X[other]) + (Y[i] - Y[other]) * (Y[i] - Y[other]) <= mid_int) {
					dfs(other, pnt, mid_int);
				}
			}
		}
	}
}
 
int dp[N][K];
 
bool solve(int pnt) {
	for (int i = 0; i <= pnt; i++) {
		for (int j = 0; j < k; j++) {
			dp[i][j] = -N;
		}
	}
	dp[0][0] = 0;
	for (int i = 0; i < pnt; i++) {
		for (int j = 0; j < k; j++) {
			if (dp[i][j] == -N) continue;
			dp[i + 1][(j + hv[i]) % k] = max(dp[i + 1][(j + hv[i]) % k], dp[i][j] + 1); 
			dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
		}
	}
	return dp[pnt][0] > 0;
}
 
bool check(int mid_int) {
	// cerr << mid_int << endl;
	ld mid = sqrt((ld)mid_int);
	for (int i = 0; i < n; i++) {
		by_block[i].clear();
		ids[i] = i;
	}
	seen.clear();
	for (int i = 0; i < n; i++) {
		ids[i] = i;
		block[i] = {X[i] / mid, Y[i] / mid};
		seen.push_back(block[i]);
	}
	sort(ids, ids + n, [&](int i, int j) {
		return block[i] < block[j];
	});
	for (int i = 0; i < n; i++) {
		int r = i;
		while (r + 1 < n && block[ids[r]] == block[ids[r + 1]]) {
			r++;
		}
		if (r - i + 1 >= 4 * k) {
			return true;
		}
		i = r;
	}
	sort(seen.begin(), seen.end());
	seen.resize(unique(seen.begin(), seen.end()) - seen.begin());
	for (int i = 0; i < n; i++) {
		by_block[gt(block[i])].push_back(i);
	}
	int ans = 0;
	fill(used, used + n, 0);
	for (int i = 0; i < n; i++) {
		if (!used[i]) {
			int pnt  = 0;
			// cerr << "+ " << i << endl;
			dfs(i, pnt, mid_int);
			// cerr << "- " << i << endl;
			ans |= solve(pnt);
		}
	}
	return (bool)ans;
}
 
signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	cin >> n >> k;
	for (int i = 0; i < n; i++) {
		cin >> X[i] >> Y[i] >> C[i];
	}
	// cerr << check(14551915) << endl;
	// assert(n <= N);
	// cerr << X[n - 1] << ' ' << Y[n - 1] << ' ' << C[n - 1] << endl;
	int l = 0;
	int r = 4e18 + 239;
	while (r - l > 1) {
		int m = (r + l) >> 1;
		if (check(m)) {
			r = m;
		} else {
			l = m;
		}
	}
	cout << fixed << setprecision(3);
	cout << sqrt(r) << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 1536 KB Output is correct
2 Correct 5 ms 1536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 1536 KB Output is correct
2 Correct 6 ms 1536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 1536 KB Output is correct
2 Correct 20 ms 2048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 1536 KB Output is correct
2 Correct 25 ms 2116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 1536 KB Output is correct
2 Correct 16 ms 1920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 1536 KB Output is correct
2 Correct 21 ms 2048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 1536 KB Output is correct
2 Correct 22 ms 2176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 1536 KB Output is correct
2 Correct 21 ms 1920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 1536 KB Output is correct
2 Correct 716 ms 14624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 1536 KB Output is correct
2 Execution timed out 1084 ms 31408 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 1536 KB Output is correct
2 Execution timed out 1085 ms 31048 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 1536 KB Output is correct
2 Execution timed out 1080 ms 29676 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 1536 KB Output is correct
2 Execution timed out 1091 ms 31980 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 1536 KB Output is correct
2 Execution timed out 1080 ms 31956 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 1536 KB Output is correct
2 Execution timed out 1085 ms 29700 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 1536 KB Output is correct
2 Execution timed out 1094 ms 30956 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 1536 KB Output is correct
2 Execution timed out 1084 ms 17124 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 1536 KB Output is correct
2 Execution timed out 1094 ms 17112 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 1536 KB Output is correct
2 Execution timed out 1091 ms 31164 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 1536 KB Output is correct
2 Execution timed out 1091 ms 29360 KB Time limit exceeded