Submission #636522

#TimeUsernameProblemLanguageResultExecution timeMemory
636522ghostwriterJakarta Skyscrapers (APIO15_skyscraper)C++14
57 / 100
1093 ms82484 KiB
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#endif
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
    Tran The Bao
    CTL - Da Lat
    Cay ngay cay dem nhung deo duoc cong nhan
*/
struct Node {
	int b, p, pos;
	Node() {}
	Node(int b, int p, int pos) : b(b), p(p), pos(pos) {}
};
const int oo = 1e9;
const int M = 7500001;
const int N = 30001;
int n, m, b[N], p[N], f[N], d[M], c[M], rs = oo;
vi stat[N], a[N];
deque<Node> q;
map<pi, int> cnt, cnt1;
int getp(pi x) {
	int ans = (x.st > 0? f[x.st - 1] : 0);
	ans += lb(all(stat[x.st]), x.nd) - stat[x.st].begin() + 1;
	return ans;
}
signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    // freopen(file".inp", "r", stdin);
    // freopen(file".out", "w", stdout);
    cin >> n >> m;
    FOR(i, 0, m - 1) {
    	cin >> b[i] >> p[i];
    	if (!cnt[{b[i] % p[i], p[i]}]) {
    		int bn = b[i], pn = p[i];
    		bn %= pn;
    		FOR(j, bn, n - 1) {
    			stat[j].pb(pn);
    			j += pn - 1;
    		}
    		cnt[{b[i] % p[i], p[i]}] = 1;
    	}
    	if (!cnt1[{b[i], p[i]}]) {
    		a[b[i]].pb(p[i]);
    		cnt1[{b[i], p[i]}] = 1;
    	}
    }
    FOR(i, 0, n - 1) {
    	sort(all(stat[i]));
    	sort(all(a[i]));
    	f[i] = (i > 0? f[i - 1] : 0) + sz(stat[i]);
    }
    FOR(i, 0, M - 1) d[i] = oo;
    int rpos = getp({b[0], p[0]});
    d[rpos] = 0;
    q.pb(Node(b[0], p[0], rpos));
    WHILE(!q.empty()) {
    	Node cur = q.front();
    	q._pf();
    	int b = cur.b, p = cur.p, pos = cur.pos;
    	int du = d[pos];
    	if (c[pos]) continue;
    	c[pos] = 1;
    	if (b - p >= 0) {
    		int posv = getp({b - p, p});
    		if (d[posv] > du + 1) {
				d[posv] = du + 1;
				q.pb(Node(b - p, p, posv));
			}
    	}
    	if (b + p < n) {
    		int posv = getp({b + p, p});
    		if (d[posv] > du + 1) {
	    		d[posv] = du + 1;
	    		q.pb(Node(b + p, p, posv));
    		}
    	}
    	if (!a[b].empty()) {
    		if (a[b].back() < p) {
    			int p1 = a[b][0];
    			int posv = getp({b, p1});
    			if (d[posv] > du) {
    				d[posv] = du;
    				q.pf(Node(b, p1, posv));
    			}
    		}
    		else {
    			int pos = lb(all(a[b]), p) - a[b].begin();
				int p1 = a[b][0];
    			int posv = getp({b, p1});
    			if (d[posv] > du) {
    				d[posv] = du;
    				q.pf(Node(b, p1, posv));
    			}
				if (pos < sz(a[b]) - 1) {
					int p1 = a[b][pos + 1];
	    			int posv = getp({b, p1});
	    			if (d[posv] > du) {
	    				d[posv] = du;
	    				q.pf(Node(b, p1, posv));
	    			}
				}
    		}
    	}
    }
    FOR(i, 1, N - 1) {
    	if (stat[b[1]].back() < i) continue;
    	int pos1 = lb(all(stat[b[1]]), i) - stat[b[1]].begin();
    	if (stat[b[1]][pos1] != p[1]) continue;
    	int pos = getp({b[1], i});
    	rs = min(rs, d[pos]);
    }
    cout << (rs < oo? rs : -1);
    return 0;
}
/*
5 3
0 2
1 1
4 1
*/

Compilation message (stderr)

skyscraper.cpp: In function 'int main()':
skyscraper.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
skyscraper.cpp:58:5: note: in expansion of macro 'FOR'
   58 |     FOR(i, 0, m - 1) {
      |     ^~~
skyscraper.cpp:24:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
skyscraper.cpp:63:7: note: in expansion of macro 'FOR'
   63 |       FOR(j, bn, n - 1) {
      |       ^~~
skyscraper.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
skyscraper.cpp:74:5: note: in expansion of macro 'FOR'
   74 |     FOR(i, 0, n - 1) {
      |     ^~~
skyscraper.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
skyscraper.cpp:79:5: note: in expansion of macro 'FOR'
   79 |     FOR(i, 0, M - 1) d[i] = oo;
      |     ^~~
skyscraper.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
skyscraper.cpp:132:5: note: in expansion of macro 'FOR'
  132 |     FOR(i, 1, N - 1) {
      |     ^~~
#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...