Submission #836511

# Submission time Handle Problem Language Result Execution time Memory
836511 2023-08-24T12:05:42 Z unnick City Mapping (NOI18_citymapping) C++14
57 / 100
39 ms 18000 KB
#include "citymapping.h"

//#include <iostream>
#include <functional>
#include <algorithm>
#include <numeric>
#include <vector>

using namespace std;

#define ll long long

typedef struct {
	int to;
	ll d;
} edge_t;

void find_roads(int N, int Q, int A[], int B[], int W[]) {
	if (Q <= 12000) {
		int start;
		{
			ll maxd = 0;
			for (int i = 2; i <= N; i++) {
				ll d = get_distance(1, i);
				if (d > maxd) {
					maxd = d;
					start = i;
				}
			}
		}
		vector<ll> dists(N);
		dists[start-1] = 0;
		vector<int> ids(N);
		iota(ids.begin(), ids.end(), 0);
		for (int i = 1; i <= N; i++) {
			if (i == start) continue;
			dists[i-1] = get_distance(start,i);
		}
		sort(ids.begin(), ids.end(), [&](int a, int b) {
			return dists[a] < dists[b];
		});
		for (int i = 0; i < N-1; i++) {
			A[i] = ids[i]+1;
			B[i] = ids[i+1]+1;
			W[i] = dists[ids[i+1]] - dists[ids[i]];
		}
	} else {
		vector<ll> dists(N*N, -1);
		auto ds = [&](int i_, int j_){
			i_--;
			j_--;
			int i = min(i_, j_);
			int j = max(i_, j_);
			if (dists[i+j*N] == -1) {
				dists[i+j*N] = get_distance(i+1, j+1);
			}
			return dists[i+j*N];
		};
		int out_idx = 0;
		auto output = [&](int x, int y, int dist) {
			A[out_idx] = x;
			B[out_idx] = y;
			W[out_idx++] = dist;
		};
		// for (int i = 1; i <= N; i++) {
		// 	for (int j = i+1; j <= N; j++) {
		// 		// if (get_distance(i,j) == 1) {
		// 		// 	A[k] = i;
		// 		// 	B[k] = j;
		// 		// 	W[k++] = 1;
		// 		// }
		// 		dists[i+j*N] = dists[j+i*N] = get_distance(i,j);
		// 	}
		// }

		std::function<void(vector<edge_t>&, int, int)> rec = [&](vector<edge_t> &edges, int x, int a) -> void {
			if (edges.size() == 0) return;
			if (a == 2) {
				edge_t edge = edges[0];
				output(x, edge.to, edge.d);
				for (int i = 1; i < edges.size(); i++) {
					edges[i-1] = edges[i];
					edges[i-1].d -= edge.d;
				}
				edges.pop_back();
				rec(edges, edge.to, 0);
				return;
			}
			vector<edge_t> sub;
			vector<edge_t> rest;
			int y = edges[0].to;
			output(x, y, edges[0].d);
			for (edge_t edge : edges) {
				if (edge.to == x || edge.to == y) continue;
				ll dist = ds(y, edge.to);
				if (dist < edge.d) {
					sub.push_back({.to = edge.to, .d = ds(y, edge.to)});
				} else {
					rest.push_back(edge);
				}
			}
			sort(sub.begin(), sub.end(), [](edge_t a, edge_t b) {
				return a.d < b.d;
			});
			rec(sub, y, 1);
			rec(rest, x, a+1);
		};
		vector<edge_t> edges;
		for (int i = 2; i <= N; i++) {
			ll dist = ds(1, i);
			edges.push_back({.to = i, .d = dist});
		}
		sort(edges.begin(), edges.end(), [](edge_t a, edge_t b) {
			return a.d < b.d;
		});
		rec(edges, 1, 0);
	}
}

Compilation message

citymapping.cpp: In lambda function:
citymapping.cpp:81:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge_t>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |     for (int i = 1; i < edges.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
citymapping.cpp: In function 'void find_roads(int, int, int*, int*, int*)':
citymapping.cpp:37:29: warning: 'start' may be used uninitialized in this function [-Wmaybe-uninitialized]
   37 |    dists[i-1] = get_distance(start,i);
      |                 ~~~~~~~~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 20 ms 11476 KB Correct: 251481 out of 500000 queries used.
2 Correct 21 ms 13264 KB Correct: 235836 out of 500000 queries used.
3 Correct 5 ms 8404 KB Correct: 19306 out of 500000 queries used.
4 Correct 5 ms 8404 KB Correct: 17603 out of 500000 queries used.
5 Correct 6 ms 8660 KB Correct: 41603 out of 500000 queries used.
# Verdict Execution time Memory Grader output
1 Correct 20 ms 11476 KB Correct: 251481 out of 500000 queries used.
2 Correct 21 ms 13264 KB Correct: 235836 out of 500000 queries used.
3 Correct 5 ms 8404 KB Correct: 19306 out of 500000 queries used.
4 Correct 5 ms 8404 KB Correct: 17603 out of 500000 queries used.
5 Correct 6 ms 8660 KB Correct: 41603 out of 500000 queries used.
6 Correct 39 ms 18000 KB Correct: 479862 out of 500000 queries used.
7 Correct 20 ms 12884 KB Correct: 219373 out of 500000 queries used.
8 Correct 5 ms 8532 KB Correct: 20818 out of 500000 queries used.
9 Correct 5 ms 8404 KB Correct: 16260 out of 500000 queries used.
10 Correct 6 ms 8532 KB Correct: 35315 out of 500000 queries used.
# Verdict Execution time Memory Grader output
1 Correct 1 ms 592 KB Correct: 1980 out of 12000 queries used.
2 Correct 2 ms 468 KB Correct: 1984 out of 12000 queries used.
3 Correct 1 ms 468 KB Correct: 1998 out of 12000 queries used.
4 Correct 1 ms 468 KB Correct: 1984 out of 12000 queries used.
5 Correct 1 ms 468 KB Correct: 1980 out of 12000 queries used.
# Verdict Execution time Memory Grader output
1 Correct 1 ms 592 KB Correct: 1980 out of 12000 queries used.
2 Correct 2 ms 468 KB Correct: 1984 out of 12000 queries used.
3 Correct 1 ms 468 KB Correct: 1998 out of 12000 queries used.
4 Correct 1 ms 468 KB Correct: 1984 out of 12000 queries used.
5 Correct 1 ms 468 KB Correct: 1980 out of 12000 queries used.
6 Correct 1 ms 468 KB Correct: 1994 out of 12000 queries used.
7 Correct 1 ms 468 KB Correct: 1990 out of 12000 queries used.
8 Correct 1 ms 468 KB Correct: 1998 out of 12000 queries used.
9 Correct 1 ms 592 KB Correct: 1992 out of 12000 queries used.
10 Correct 1 ms 468 KB Correct: 1986 out of 12000 queries used.
# Verdict Execution time Memory Grader output
1 Correct 20 ms 11476 KB Correct: 251481 out of 500000 queries used.
2 Correct 21 ms 13264 KB Correct: 235836 out of 500000 queries used.
3 Correct 5 ms 8404 KB Correct: 19306 out of 500000 queries used.
4 Correct 5 ms 8404 KB Correct: 17603 out of 500000 queries used.
5 Correct 6 ms 8660 KB Correct: 41603 out of 500000 queries used.
6 Correct 39 ms 18000 KB Correct: 479862 out of 500000 queries used.
7 Correct 20 ms 12884 KB Correct: 219373 out of 500000 queries used.
8 Correct 5 ms 8532 KB Correct: 20818 out of 500000 queries used.
9 Correct 5 ms 8404 KB Correct: 16260 out of 500000 queries used.
10 Correct 6 ms 8532 KB Correct: 35315 out of 500000 queries used.
11 Correct 1 ms 592 KB Correct: 1980 out of 12000 queries used.
12 Correct 2 ms 468 KB Correct: 1984 out of 12000 queries used.
13 Correct 1 ms 468 KB Correct: 1998 out of 12000 queries used.
14 Correct 1 ms 468 KB Correct: 1984 out of 12000 queries used.
15 Correct 1 ms 468 KB Correct: 1980 out of 12000 queries used.
16 Correct 1 ms 468 KB Correct: 1994 out of 12000 queries used.
17 Correct 1 ms 468 KB Correct: 1990 out of 12000 queries used.
18 Correct 1 ms 468 KB Correct: 1998 out of 12000 queries used.
19 Correct 1 ms 592 KB Correct: 1992 out of 12000 queries used.
20 Correct 1 ms 468 KB Correct: 1986 out of 12000 queries used.
21 Incorrect 5 ms 8660 KB Too many calls to get_distance().
22 Halted 0 ms 0 KB -