Submission #636428

#TimeUsernameProblemLanguageResultExecution timeMemory
636428ghostwriterJakarta Skyscrapers (APIO15_skyscraper)C++14
57 / 100
1079 ms31804 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
*/
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
const int oo = 1e9;
const int N = 30001;
int n, m, b[N], p[N], rs = oo;
vi a[N];
map<pi, int> d;
deque<pi> q;
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];
    	a[b[i]].pb(p[i]);
    }
    FOR(i, 0, n - 1) sort(all(a[i]));
    d[{b[0], p[0]}] = 0;
    q.pb({b[0], p[0]});
    WHILE(!q.empty()) {
    	pi cur = q.front();
    	q._pf();
    	int b = cur.st, p = cur.nd;
    	int du = d[{b, p}];
    	if (b - p >= 0 && !d.count({b - p, p})) {
			d[{b - p, p}] = du + 1;
			q.pb({b - p, p}); 
    	}
    	if (b + p < n && !d.count({b + p, p})) {
    		d[{b + p, p}] = du + 1;
    		q.pb({b + p, p}); 
    	}
    	if (!a[b].empty()) {
    		if (a[b].back() < p) {
    			int p1 = a[b][0];
    			if (!d.count({b, p1}) || d[{b, p1}] > du) {
    				d[{b, p1}] = du;
    				q.pf({b, p1});
    			}
    		}
    		else {
    			int pos = lb(all(a[b]), p) - a[b].begin();
    			if (a[b][pos] != p) {
    				int p1 = a[b][0];
	    			if (!d.count({b, p1}) || d[{b, p1}] > du) {
	    				d[{b, p1}] = du;
	    				q.pf({b, p1});
	    			}
    			}
    			else {
    				if (pos > 0) {
    					int p1 = a[b][pos - 1];
		    			if (!d.count({b, p1}) || d[{b, p1}] > du) {
		    				d[{b, p1}] = du;
		    				q.pf({b, p1});
		    			}
    				}
    				if (pos < sz(a[b]) - 1) {
    					int p1 = a[b][pos + 1];
		    			if (!d.count({b, p1}) || d[{b, p1}] > du) {
		    				d[{b, p1}] = du;
		    				q.pf({b, p1});
		    			}	
    				}
    			}
    		}
    	}
    }
    FOR(i, 1, N - 1) {
    	if (!d.count({b[1], i})) continue;
    	rs = min(rs, d[{b[1], i}]);
    }
    cout << (rs < oo? rs : -1);
    return 0;
}
/*
5 3
0 2
1 1
4 1
*/

Compilation message (stderr)

skyscraper.cpp:37: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
   37 | #pragma GCC optimization ("O3")
      | 
skyscraper.cpp:38: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
   38 | #pragma GCC optimization ("unroll-loops")
      | 
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:50:5: note: in expansion of macro 'FOR'
   50 |     FOR(i, 0, m - 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:54:5: note: in expansion of macro 'FOR'
   54 |     FOR(i, 0, n - 1) sort(all(a[i]));
      |     ^~~
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:106:5: note: in expansion of macro 'FOR'
  106 |     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...