Submission #1162128

#TimeUsernameProblemLanguageResultExecution timeMemory
1162128tminhJakarta Skyscrapers (APIO15_skyscraper)C++20
0 / 100
65 ms141380 KiB
#include "bits/stdc++.h"
using namespace std;

#define ll long long
#define endl '\n'
#define fi first
#define se second
#define vall(a) (a).begin(), (a).end()
#define sze(a) (ll)a.size()
#define pii pair<ll, ll>

#define pb push_back
#define pf push_front


const ll mod = 1e9 + 7;
const signed N = 30005;
const ll oo = 1e18;
ll n, m, p[N], b[N], d[N][200], base;

struct node {
	ll idx, p, c;
	bool operator<(const node &other) const {
		return c > other.c;
	}
};

vector<node> adj[N][200];

ll dijkstra() {
	for (ll i = 0; i < n; ++i) {
		for (ll j = 0; j <= base; ++j) d[i][j] = oo;
	}
	priority_queue<node> q;
	d[b[0]][0] = 0;
	q.push({b[0], 0, d[b[0]][0]});
	while(!q.empty()) {
		auto [idx, p, c] = q.top();
		q.pop();
		if (d[idx][p] != c) continue;
		for (auto [pos, jump, w] : adj[idx][p]) {
			if (d[pos][jump] > d[idx][p] + w) {
				d[pos][jump] = d[idx][p] + w;
				q.push({pos, jump, d[pos][jump]});
			}
		}
		if (d[idx][0] > d[idx][p]) {
			d[idx][0] = d[idx][p];
			q.push({idx, 0, d[idx][0]});
		}
		if (p != 0) {
			if (idx + p < n && d[idx + p][p] > d[idx][p] + 1) {
				d[idx + p][p] = d[idx][p] + 1;
				q.push({idx + p, p, d[idx + p][p]});
			}
			if (idx - p >= 0 && d[idx - p][p] > d[idx][p] + 1) {
				d[idx - p][p] = d[idx][p] + 1;
				q.push({idx - p, p, d[idx - p][p]});
			}
		}
	}
	return d[b[1]][0];
}

void output() {
	for (ll i = 0; i < m; ++i) {
		if (p[i] > base) {
			for (ll j = 1; b[i] + p[i] * j < n; ++j) adj[b[i]][0].pb({b[i] + p[i] * j, 0, j});
			for (ll j = 1; b[i] - p[i] * j >= 0; ++j) adj[b[i]][0].pb({b[i] - p[i] * j, 0, j});
		} else {
			adj[b[i]][p[i]].pb({b[i], 0, 0});
			adj[b[i]][0].pb({b[i], p[i], 0});
		}
	}
	cout << dijkstra();
    return;
}

void input() {
	cin >> n >> m;
	base = sqrtl(n);
	for (ll i = 0; i < m; ++i) cin >> b[i] >> p[i];
    output();
    return;
}

signed main () {
    if(fopen("", "r")) {
        freopen("", "r", stdin);
        freopen("", "w", stdout);
    }
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);

    input();
    return 0;
}

Compilation message (stderr)

skyscraper.cpp: In function 'int main()':
skyscraper.cpp:89:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   89 |         freopen("", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~
skyscraper.cpp:90:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   90 |         freopen("", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...