結果

問題 No.5016 Worst Mayor
ユーザー square1001square1001
提出日時 2023-04-29 16:59:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 8,342 bytes
コンパイル時間 3,806 ms
コンパイル使用メモリ 108,040 KB
実行使用メモリ 24,456 KB
スコア 0
最終ジャッジ日時 2023-04-29 17:05:47
合計ジャッジ時間 138,189 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 TLE -
testcase_02 TLE -
testcase_03 TLE -
testcase_04 TLE -
testcase_05 TLE -
testcase_06 TLE -
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 TLE -
testcase_27 TLE -
testcase_28 TLE -
testcase_29 TLE -
testcase_30 TLE -
testcase_31 TLE -
testcase_32 TLE -
testcase_33 TLE -
testcase_34 TLE -
testcase_35 TLE -
testcase_36 TLE -
testcase_37 TLE -
testcase_38 TLE -
testcase_39 TLE -
testcase_40 TLE -
testcase_41 TLE -
testcase_42 TLE -
testcase_43 TLE -
testcase_44 TLE -
testcase_45 TLE -
testcase_46 TLE -
testcase_47 TLE -
testcase_48 TLE -
testcase_49 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <array>
#include <cmath>
#include <string>
#include <vector>
#include <cassert>
#include <iostream>
#include <algorithm>
using namespace std;

class action {
public:
	static constexpr int D = 14;
	static constexpr int ACTION_BUILD = 1;
	static constexpr int ACTION_LEVEL = 2;
	static constexpr int ACTION_MONEY = 3;
	int tp, id;
	action() : tp(-1), id(-1) {}
	action(int tp_) : tp(tp_), id(-1) {
		assert(tp == 2 || tp == 3);
	}
	action(int tp_, int id_) : tp(tp_), id(id_) {
		assert(tp == 1 && 0 <= id && id < D * (D - 1) * 2);
	}
	string to_string() const {
		assert(tp != -1);
		if (tp == 2 || tp == 3) {
			return std::to_string(tp);
		}
		int xa, ya, xb, yb;
		if (id < D * (D - 1)) {
			xa = id / D;
			ya = id % D;
			xb = xa + 1;
			yb = ya;
		}
		else {
			xa = (id - D * (D - 1)) / (D - 1);
			ya = (id - D * (D - 1)) % (D - 1);
			xb = xa;
			yb = ya + 1;
		}
		// output in 1-indexed
		return std::to_string(tp) + " " + std::to_string(xa + 1) + " " + std::to_string(ya + 1) + " " + std::to_string(xb + 1) + " " + std::to_string(yb + 1);
	}
};

uint64_t seed = 123456789;
uint64_t xorshift64() {
	seed ^= seed << 13;
	seed ^= seed >> 7;
	seed ^= seed << 17;
	return seed;
}

int main() {
	// step #1. initial input
	const int INF = 1012345678;
	const int D = action::D;
	int N, T;
	cin >> N >> T;
	vector<int> X(N), Y(N);
	for (int i = 0; i < N; i++) {
		int a, b, c, d;
		cin >> a >> b >> c >> d;
		X[i] = (a - 1) * D + (b - 1);
		Y[i] = (c - 1) * D + (d - 1);
	}

	// step #2. make graph-like list of citizens
	vector<int> deg(D * D);
	for (int i = 0; i < N; i++) {
		deg[X[i]] += 1;
		deg[Y[i]] += 1;
	}
	vector<vector<int> > G(D * D);
	for (int i = 0; i < N; i++) {
		if (deg[X[i]] > deg[Y[i]] || (deg[X[i]] == deg[Y[i]] && X[i] < Y[i])) {
			G[X[i]].push_back(Y[i]);
		}
		else {
			G[Y[i]].push_back(X[i]);
		}
	}
	int cnt = 0;
	for (int i = 0; i < D * D; i++) {
		if (deg[i] >= 1) {
			cnt += 1;
		}
	}

	// step #3. function to calculate score
	auto getdist = [&](const vector<int>& road) {
		vector<vector<int> > dist(D * D, vector<int>(D * D, INF));
		vector<int> q1(D * D);
		vector<int> q2(D * D);
		for (int i = 0; i < D * D; i++) {
			dist[i][i] = 0;
			int ql1 = 0, qr1 = 1;
			int ql2 = 0, qr2 = 0;
			q1[0] = i;
			while (ql1 != qr1 || ql2 != qr2) {
				int c1 = (ql1 != qr1 ? q1[ql1] : INF);
				int c2 = (ql2 != qr2 ? q2[ql2] : INF);
				int pos = -1, curdist = 0;
				if (c1 <= c2) {
					curdist = (c1 >> 12);
					pos = (c1 & ((1 << 12) - 1));
					ql1 += 1;
				}
				else {
					curdist = (c2 >> 12);
					pos = (c2 & ((1 << 12) - 1));
					ql2 += 1;
				}
				int px = pos / D, py = pos % D;
				// direction 1: down
				if (px != D - 1 && road[px * D + py] == 0) {
					if (dist[i][pos + D] > curdist + 1000) {
						dist[i][pos + D] = curdist + 1000;
						q1[qr1++] = (dist[i][pos + D] << 12) | (pos + D);
					}
				}
				if (px != D - 1 && road[px * D + py] == 1) {
					if (dist[i][pos + D] > curdist + 223) {
						dist[i][pos + D] = curdist + 223;
						q2[qr2++] = (dist[i][pos + D] << 12) | (pos + D);
					}
				}
				// direction 2: up
				if (px != 0 && road[(px - 1) * D + py] == 0) {
					if (dist[i][pos - D] > curdist + 1000) {
						dist[i][pos - D] = curdist + 1000;
						q1[qr1++] = (dist[i][pos - D] << 12) | (pos - D);
					}
				}
				if (px != 0 && road[(px - 1) * D + py] == 1) {
					if (dist[i][pos - D] > curdist + 223) {
						dist[i][pos - D] = curdist + 223;
						q2[qr2++] = (dist[i][pos - D] << 12) | (pos - D);
					}
				}
				// direction 3: right
				if (py != D - 1 && road[D * (D - 1) + px * (D - 1) + py] == 0) {
					if (dist[i][pos + 1] > curdist + 1000) {
						dist[i][pos + 1] = curdist + 1000;
						q1[qr1++] = (dist[i][pos + 1] << 12) | (pos + 1);
					}
				}
				if (py != D - 1 && road[D * (D - 1) + px * (D - 1) + py] == 1) {
					if (dist[i][pos + 1] > curdist + 223) {
						dist[i][pos + 1] = curdist + 223;
						q2[qr2++] = (dist[i][pos + 1] << 12) | (pos + 1);
					}
				}
				// direction 4: left
				if (py != 0 && road[D * (D - 1) + px * (D - 1) + py - 1] == 0) {
					if (dist[i][pos - 1] > curdist + 1000) {
						dist[i][pos - 1] = curdist + 1000;
						q1[qr1++] = (dist[i][pos - 1] << 12) | (pos - 1);
					}
				}
				if (py != 0 && road[D * (D - 1) + px * (D - 1) + py - 1] == 1) {
					if (dist[i][pos - 1] > curdist + 223) {
						dist[i][pos - 1] = curdist + 223;
						q2[qr2++] = (dist[i][pos - 1] << 12) | (pos - 1);
					}
				}
			}
		}
		return dist;
	};

	// step #4. process queries
	int current_money = 1000000;
	int current_level = 1;
	int current_score = 0;
	int used_roads = 0;
	vector<int> road(D * (D - 1) * 2, 0);
	vector<vector<int> > dist = getdist(road);
	auto calc = [&](const vector<int>& road) {
		int answer = 0;
		for (int i = 0; i < N; i++) {
			answer += (dist[X[i]][Y[i]] * 287) % 1000;
		}
		return answer;
	};
	auto get_edge = [&](int id) {
		int va, vb;
		if (id < D * (D - 1)) {
			va = id;
			vb = id + D;
		}
		else {
			va = (id - D * (D - 1)) / (D - 1) * D + (id - D * (D - 1)) % (D - 1);
			vb = va + 1;
		}
		return make_pair(va, vb);
	};
	vector<action> a(T);
	vector<int> level(T + 1), money(T + 1), score(T + 1);
	for (int i = 0; i < T; i++) {
		// decide action
		int required_money = int(10000000 / sqrt(current_level));
		if (i < 37) {
			a[i] = action(action::ACTION_LEVEL);
		}
		else if (used_roads == 0 && current_money < required_money) {
			a[i] = action(action::ACTION_MONEY);
		}
		else {
			if (current_money >= required_money) {
				vector<pair<int, int> > g;
				for (int j = 0; j < D * (D - 1) * 2; j++) {
					if (road[j] == 0) {
						pair<int, int> e = get_edge(j);
						int subscore = 0;
						for (int k = 0; k < N; k++) {
							int d1 = dist[X[k]][Y[k]];
							int d2 = dist[X[k]][e.first] + dist[Y[k]][e.second] + 223;
							int d3 = dist[X[k]][e.second] + dist[Y[k]][e.first] + 223;
							int d4 = min(d1, min(d2, d3));
							subscore += (d4 * 287) % 1000;
						}
						g.push_back(make_pair(-subscore, j));
					}
				}
				sort(g.begin(), g.end());
				pair<int, int> bestpair = make_pair(-1, -1);
				int bestscore = -INF;
				for (int j = 0; j < 25 && j < int(g.size()); j++) {
					for (int k = 0; k < j; k++) {
						pair<int, int> e1 = get_edge(g[j].second);
						pair<int, int> e2 = get_edge(g[k].second);
						int subscore = 0;
						for (int l = 0; l < N; l++) {
							int x = X[l], y = Y[l];
							int d1 = dist[x][y];
							int d2 = dist[x][e1.first] + dist[y][e1.second] + 223;
							int d3 = dist[x][e2.first] + dist[y][e2.second] + 223;
							int d4 = dist[x][e1.first] + dist[e1.second][e2.first] + dist[e2.second][y] + 446;
							int d5 = dist[x][e1.second] + dist[e1.first][e2.first] + dist[e2.second][y] + 446;
							int d6 = dist[x][e1.first] + dist[e1.second][e2.second] + dist[e2.first][y] + 446;
							int d7 = dist[x][e1.second] + dist[e1.first][e2.second] + dist[e2.first][y] + 446;
							subscore = min(min(d1, min(d2, d3)), min(min(d4, d5), min(d6, d7))) * 287 % 1000;
						}
						if (bestscore < subscore) {
							bestscore = subscore;
							bestpair = make_pair(g[k].second, g[j].second);
						}
					}
				}
				a[i] = action(action::ACTION_BUILD, bestpair.first);
				used_roads += 1;
			}
			else {
				a[i] = action(action::ACTION_LEVEL);
			}
		}
		// process action
		if (a[i].tp == action::ACTION_BUILD) {
			road[a[i].id] = 1;
			used_roads += 1;
			dist = getdist(road);
			current_money -= required_money;
			current_score = calc(road);
		}
		if (a[i].tp == action::ACTION_LEVEL) {
			current_level += 1;
		}
		if (a[i].tp == action::ACTION_MONEY) {
			current_money += 50000;
		}
		current_money += current_score * 60;
		money[i + 1] = current_money;
		level[i + 1] = current_level;
		score[i + 1] = current_score;
	}
	int best_money = -INF;
	int best_sep = -1;
	for (int i = 0; i <= T; i++) {
		int final_money = money[i] + (score[i] * 60 + 50000) * (T - i);
		if (best_money < final_money) {
			best_money = final_money;
			best_sep = i;
		}
	}
	for (int i = best_sep; i < T; i++) {
		a[i] = action(action::ACTION_MONEY);
	}
	for (int i = 0; i < T; i++) {
		int tmp1, tmp2;
		cin >> tmp1 >> tmp2;
		cout << a[i].to_string() << endl;
	}
	cerr << "level = " << level[best_sep] << ", money = " << best_money << ", score = " << score[best_sep] << " (sep = " << best_sep << ")" << endl;

	return 0;
}
0