結果

問題 No.5016 Worst Mayor
ユーザー e869120e869120
提出日時 2023-04-28 20:51:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 843 ms / 2,000 ms
コード長 5,772 bytes
コンパイル時間 1,036 ms
コンパイル使用メモリ 95,176 KB
実行使用メモリ 26,068 KB
スコア 16,750,312,112
平均クエリ数 400.00
最終ジャッジ日時 2023-04-29 12:31:58
合計ジャッジ時間 46,820 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 840 ms
25,908 KB
testcase_01 AC 834 ms
25,484 KB
testcase_02 AC 831 ms
25,692 KB
testcase_03 AC 842 ms
25,376 KB
testcase_04 AC 839 ms
25,688 KB
testcase_05 AC 838 ms
25,640 KB
testcase_06 AC 841 ms
25,448 KB
testcase_07 AC 837 ms
25,764 KB
testcase_08 AC 839 ms
25,536 KB
testcase_09 AC 839 ms
25,404 KB
testcase_10 AC 839 ms
25,540 KB
testcase_11 AC 838 ms
25,852 KB
testcase_12 AC 839 ms
25,832 KB
testcase_13 AC 841 ms
25,656 KB
testcase_14 AC 839 ms
25,500 KB
testcase_15 AC 838 ms
25,276 KB
testcase_16 AC 835 ms
25,520 KB
testcase_17 AC 839 ms
25,680 KB
testcase_18 AC 840 ms
25,848 KB
testcase_19 AC 837 ms
25,756 KB
testcase_20 AC 832 ms
25,492 KB
testcase_21 AC 836 ms
25,944 KB
testcase_22 AC 839 ms
25,560 KB
testcase_23 AC 841 ms
25,332 KB
testcase_24 AC 842 ms
25,664 KB
testcase_25 AC 836 ms
25,436 KB
testcase_26 AC 839 ms
25,272 KB
testcase_27 AC 838 ms
25,512 KB
testcase_28 AC 838 ms
25,844 KB
testcase_29 AC 838 ms
25,332 KB
testcase_30 AC 840 ms
25,552 KB
testcase_31 AC 843 ms
25,848 KB
testcase_32 AC 837 ms
25,600 KB
testcase_33 AC 831 ms
25,272 KB
testcase_34 AC 841 ms
25,436 KB
testcase_35 AC 841 ms
25,412 KB
testcase_36 AC 839 ms
25,888 KB
testcase_37 AC 841 ms
25,312 KB
testcase_38 AC 840 ms
25,644 KB
testcase_39 AC 841 ms
25,284 KB
testcase_40 AC 841 ms
25,736 KB
testcase_41 AC 838 ms
25,776 KB
testcase_42 AC 842 ms
25,900 KB
testcase_43 AC 835 ms
25,980 KB
testcase_44 AC 840 ms
25,664 KB
testcase_45 AC 837 ms
25,396 KB
testcase_46 AC 835 ms
25,440 KB
testcase_47 AC 832 ms
26,068 KB
testcase_48 AC 836 ms
25,504 KB
testcase_49 AC 841 ms
25,836 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <tuple>
#include <cmath>
#include <algorithm>
using namespace std;

// Input Information
int DEBUG = 1;
int N, T;
int A[3009], B[3009], C[3009], D[3009];
int CurrentMoney, DebugMoney = 1000000;
int CurrentCorps, DebugCorps = 1;

// Other Variables
int Build[1009];
int Money[2000009];
int Cost[109][109];
int D1[11][12];
int D2[12][11];

// Initialize Money
void Initialize() {
	for (int i = 0; i <= 1000; i++) {
		for (int j = 0; j < 1000; j++) {
			Money[1000 * i + 223 * j] = 60 * j;
		}
	}
	for (int i = 1; i <= 1000; i++) {
		Build[i] = (int)(10000000.0 / sqrt(1.0 * i));
	}
}

// Get Total Earned Money
int GetEarnedMoney() {
	for (int i = 0; i < 100; i++) {
		for (int j = 0; j < 100; j++) Cost[i][j] = 1000000;
		Cost[i][i] = 0;
	}

	// Initialize Cost
	for (int i = 0; i < 9; i++) {
		for (int j = 0; j < 10; j++) {
			int a1 = (i + 0) * 10 + (j + 0);
			int a2 = (i + 1) * 10 + (j + 0);
			if (D1[i][j] == 1) {
				Cost[a1][a2] = 223;
				Cost[a2][a1] = 223;
			}
			else {
				Cost[a1][a2] = 1000;
				Cost[a2][a1] = 1000;
			}
		}
	}
	for (int i = 0; i < 10; i++) {
		for (int j = 0; j < 9; j++) {
			int a1 = (i + 0) * 10 + (j + 0);
			int a2 = (i + 0) * 10 + (j + 1);
			if (D2[i][j] == 1) {
				Cost[a1][a2] = 223;
				Cost[a2][a1] = 223;
			}
			else {
				Cost[a1][a2] = 1000;
				Cost[a2][a1] = 1000;
			}
		}
	}

	// Warshall-Floyd
	for (int k = 0; k < 100; k++) {
		for (int i = 0; i < 100; i++) {
			for (int j = 0; j < 100; j++) Cost[i][j] = min(Cost[i][j], Cost[i][k] + Cost[k][j]);
		}
	}
	int sum = 0;
	for (int i = 1; i <= N; i++) {
		int pos1 = (A[i] - 1) * 10 + (B[i] - 1);
		int pos2 = (C[i] - 1) * 10 + (D[i] - 1);
		sum += Money[Cost[pos1][pos2]];
	}
	return sum;
}

// Set Roads
// Mode = 0: Build Roads on left
// Mode = 1: Build Roads on right
// Mode = 2: Build Roads on up
// Mode = 3: Build Roads on down
void SetRoads(vector<int> vec, int Mode) {
	for (int i = 0; i < 9; i++) {
		for (int j = 0; j < 10; j++) D1[i][j] = 0;
	}
	for (int i = 0; i < 10; i++) {
		for (int j = 0; j < 9; j++) D2[i][j] = 0;
	}
	for (int i = 0; i < vec.size(); i++) {
		for (int j = 0; j < 9; j++) {
			if (Mode == 0 || Mode == 1) D2[vec[i]][j] = 1;
			if (Mode == 2 || Mode == 3) D1[j][vec[i]] = 1;
		}
	}
	for (int i = 0; i < 9; i++) {
		if (Mode == 0) D1[i][0] = 1;
		if (Mode == 1) D1[i][9] = 1;
		if (Mode == 2) D2[0][i] = 1;
		if (Mode == 3) D2[9][i] = 1;
	}
}

void Ask(int a1, int a2, int a3, int a4, int a5) {
	if (DEBUG == 1) {
		if (a1 == 1) cout << a1 << " " << a2 << " " << a3 << " " << a4 << " " << a5 << endl;
		if (a1 == 2) cout << a1 << endl;
		if (a1 == 3) cout << a1 << endl;
	}
	if (DEBUG == 2) {
		if (a1 == 1) {
			if (a2 > a4) { swap(a2, a4); swap(a3, a5); }
			if (a3 > a5) { swap(a2, a4); swap(a3, a5); }
			if (a3 == a5) D1[a2 - 1][a3 - 1] = 1;
			if (a2 == a4) D2[a2 - 1][a3 - 1] = 1;
			DebugMoney -= Build[DebugCorps];
		}
		if (a1 == 2) {
			DebugCorps += 1;
		}
		if (a1 == 3) {
			DebugMoney += 50000;
		}
		DebugMoney += GetEarnedMoney();
	}
}

int main() {
	// Input
	Initialize();
	cin >> N >> T;
	for (int i = 1; i <= N; i++) cin >> A[i] >> B[i] >> C[i] >> D[i];

	// Brute Force Rectangles
	int Maximum = 0;
	pair<int, int> MaxID = make_pair(-1, -1);
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < (1 << 10); j++) {
			vector<int> vec;
			for (int k = 0; k < 10; k++) {
				if ((j & (1 << k)) != 0) vec.push_back(k);
			}
			if (vec.size() >= 4) continue;
			
			// Get Score
			SetRoads(vec, i);
			int Score = GetEarnedMoney();
			if (Maximum < Score) {
				Maximum = Score;
				MaxID = make_pair(i, j);
			}
		}
	}

	// Sort Lines (by distance from center)
	vector<pair<int, int>> Lines;
	for (int i = 0; i < 10; i++) {
		if ((MaxID.second & (1 << i)) == 0) continue;
		Lines.push_back(make_pair(abs(9 - 2 * i), i));
	}
	sort(Lines.begin(), Lines.end());

	// Calculate the Order of Adding Edges
	vector<tuple<int, int, int, int>> Edges;
	for (int i = 0; i < Lines.size(); i++) {
		int to = Lines[i].second;
		int ord[9] = { 4,5,3,6,2,7,1,8,0 };
		for (int j = 0; j < 9; j++) {
			if (MaxID.first == 0 || MaxID.first == 1) Edges.push_back(make_tuple(to, ord[j], to, ord[j] + 1));
			if (MaxID.first == 2 || MaxID.first == 3) Edges.push_back(make_tuple(ord[j], to, ord[j] + 1, to));
		}
		if (i == 0) {
			for (int j = 0; j < 9; j++) {
				if (MaxID.first == 0) Edges.push_back(make_tuple(ord[j], 0, ord[j] + 1, 0));
				if (MaxID.first == 1) Edges.push_back(make_tuple(ord[j], 9, ord[j] + 1, 9));
				if (MaxID.first == 2) Edges.push_back(make_tuple(0, ord[j], 0, ord[j] + 1));
				if (MaxID.first == 3) Edges.push_back(make_tuple(9, ord[j], 9, ord[j] + 1));
			}
		}
	}
	for (int i = 0; i < 9; i++) {
		for (int j = 0; j < 10; j++) D1[i][j] = 0;
	}
	for (int i = 0; i < 10; i++) {
		for (int j = 0; j < 9; j++) D2[i][j] = 0;
	}

	// Answer Query
	int BuiltRoads = 0;
	for (int i = 1; i <= T; i++) {
		if (DEBUG == 1) {
			cin >> CurrentMoney >> CurrentCorps;
			if (CurrentMoney == -1 && CurrentCorps == -1) break;
		}
		if (DEBUG == 2) {
			CurrentMoney = DebugMoney;
			CurrentCorps = DebugCorps;
		}
		fprintf(stderr, "Turn =% 4d, Money=% 10d, Corps =% 3d, Roads =% 3d/%d\n", i, CurrentMoney, CurrentCorps, BuiltRoads, (int)Edges.size());

		// Get Acts
		if (i <= 50) {
			Ask(2, -1, -1, -1, -1);
		}
		else if (BuiltRoads == (int)Edges.size()) {
			Ask(3, -1, -1, -1, -1);
		}
		else if (BuiltRoads >= 3 && CurrentMoney < Build[CurrentCorps]) {
			Ask(2, -1, -1, -1, -1);
		}
		else if (BuiltRoads <= 2 && CurrentMoney < Build[CurrentCorps]) {
			Ask(3, -1, -1, -1, -1);
		}
		else {
			Ask(1, get<0>(Edges[BuiltRoads]) + 1, get<1>(Edges[BuiltRoads]) + 1, get<2>(Edges[BuiltRoads]) + 1, get<3>(Edges[BuiltRoads]) + 1);
			BuiltRoads += 1;
		}
	}
	return 0;
}
0