답안 #549082

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
549082 2022-04-15T05:56:23 Z LucaDantas Railway Trip (JOI17_railway_trip) C++17
30 / 100
1390 ms 524288 KB
#include <bits/stdc++.h>
using namespace std;

constexpr int maxn = 1e5+10;

int a[maxn], l[maxn][2], r[maxn][2], dist[2][maxn], mark[maxn], seen[maxn], cnt;

vector<int> pos[105];

int d(int x, int y, int k) {
	// se x==y ele retorna -1 e é pra retornar isso mesmo
	if(x > y) swap(x, y);
	return lower_bound(pos[k].begin(), pos[k].end(), y) - lower_bound(pos[k].begin(), pos[k].end(), x) - 1;
}

vector<int> reached[2][105];

void dijkstra(int s, int k) {
	++cnt;
	dist[k][s] = 0;
	seen[s] = cnt;
	priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
	q.push({0, s});
	while(q.size()) {
		int u = q.top().second; q.pop();
		if(mark[u] == cnt) continue;
		mark[u] = cnt;
		reached[k][a[u]].push_back(u);

		int d = dist[k][u];
		for(auto [v, w] : {make_pair(l[u][0], l[u][1]), make_pair(r[u][0], r[u][1])}) {
			if(seen[v] < cnt || dist[k][v] > d + w) {
				seen[v] = cnt;
				dist[k][v] = d+w;
				q.push({d+w, v});
			}
		}
	}
}

int main() {
	int n, k, q; scanf("%d %d %d", &n, &k, &q);
	for(int i = 0; i < n; i++) {
		scanf("%d", a+i);
		for(int j = 0; j <= a[i]; j++)
			pos[j].push_back(i);
	}

	for(int i = 0; i < n; i++) {
		if(a[i] == k) { l[i][0] = i; l[i][1] = 0; continue; }
		l[i][0] = i-1; l[i][1] = 1;
		while(a[l[i][0]] <= a[i]) {
			l[i][1] = (a[l[i][0]] == a[i] ? l[l[i][0]][1] : 0) + 1;
			l[i][0] = l[l[i][0]][0];
		}
	}
	
	for(int i = n-1; i >= 0; i--) {
		if(a[i] == k) { r[i][0] = i, r[i][1] = 0; continue; }
		r[i][0] = i+1; r[i][1] = 1;
		while(a[r[i][0]] <= a[i]) {
			r[i][1] = (a[r[i][0]] == a[i] ? r[r[i][0]][1] : 0) + 1;
			r[i][0] = r[r[i][0]][0];
		}
	}

	while(q--) {
		int x, y; scanf("%d %d", &x, &y);
		--x, --y;

		dijkstra(x, 0); dijkstra(y, 1);

		int ans = 0x3f3f3f3f;

		for(int lvl = 1; lvl <= k; lvl++) {
			for(int x : reached[0][lvl]) for(int y : reached[1][lvl])
				ans = min(ans, dist[0][x] + dist[1][y] + d(x, y, lvl));
			reached[0][lvl].clear(), reached[1][lvl].clear();
		}

		printf("%d\n", ans);
	}
}

Compilation message

railway_trip.cpp: In function 'int main()':
railway_trip.cpp:42:20: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |  int n, k, q; scanf("%d %d %d", &n, &k, &q);
      |               ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
railway_trip.cpp:44:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   44 |   scanf("%d", a+i);
      |   ~~~~~^~~~~~~~~~~
railway_trip.cpp:68:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |   int x, y; scanf("%d %d", &x, &y);
      |             ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 352 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 320 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 316 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 8 ms 6484 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 234 ms 9148 KB Output is correct
2 Correct 218 ms 8440 KB Output is correct
3 Correct 235 ms 9704 KB Output is correct
4 Correct 225 ms 9940 KB Output is correct
5 Correct 232 ms 10104 KB Output is correct
6 Correct 230 ms 10604 KB Output is correct
7 Correct 246 ms 10780 KB Output is correct
8 Correct 121 ms 6844 KB Output is correct
9 Correct 130 ms 6700 KB Output is correct
10 Correct 354 ms 6572 KB Output is correct
11 Correct 373 ms 6604 KB Output is correct
12 Correct 394 ms 6732 KB Output is correct
13 Correct 371 ms 6640 KB Output is correct
14 Correct 399 ms 6680 KB Output is correct
15 Correct 589 ms 6676 KB Output is correct
16 Correct 586 ms 8428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1390 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -