제출 #874544

#제출 시각아이디문제언어결과실행 시간메모리
874544arashMLGJakarta Skyscrapers (APIO15_skyscraper)C++17
100 / 100
286 ms202580 KiB
#include<bits/stdc++.h>
#ifdef LOCAL
#include "Essentials/algo/debug.h"
#else
#define debug(...) 69
#endif
using namespace std;
 
typedef long long     ll;
typedef pair<int,int> pii;
 
const int N = 3e4 + 2;
const int M = 3e4+ 2;
const ll inf = 1e18;
 
#define F           first
#define S           second
#define pb          push_back
#define kill(x)     cout<<x<<endl, exit(0);
#define all(x)      x.begin(),x.end()
#define sz(x)       (int)x.size()
#define lc          (v << 1)
#define rc          ((v<<1) |1)
//#define int         long long

 
int n,m;
vector<int> here[N];
bool vis[N];
bitset<N> mark[M],mk[M];
deque<pair<pii,int>> dq;
//int dist[M][N];
int b[M],p[M];
int ans = -1;
 
void bfs() {
	mark[0][b[0]] = 1;
	dq.push_front({{0,b[0]},0});
	while(sz(dq)) {
		auto [sex,d] = dq.front(); dq.pop_front();
		auto [id,pos] = sex;
		//debug((int)id,(int)pos);
		assert(id >= 0 &&id < m);
		assert(pos >=0 && pos < n);
		if(id == 1) {
			kill(d);
		}
		mk[p[id]][b[id]] = 1;
		if(!vis[pos]) {
			for(int x : here[pos]) {
				if(!mark[x][pos]) {
					mark[x][pos] = 1;
					if(p[x] != p[id] || x == 1)
						dq.push_front({{x,pos},d});
				}
			}
			vis[pos] = true;
		}
		// front
		if(pos+ p[id] < n && !mark[id][pos + p[id]] && !mk[p[id]][pos + p[id]]) {
			mark[id][pos + p[id]] = true;
			mk[p[id]][pos + p[id]] = true;
			//dist[id][pos + p[id]] = dist[id][pos] + 1;
			dq.push_back({{id,pos+p[id]},d+1});
		}
		// back
		if(pos- p[id] >=0 && !mark[id][pos - p[id]] && !mk[p[id]][pos - p[id]]) {
			mark[id][pos - p[id]] = true;
			mk[p[id]][pos - p[id]]= true;
			//dist[id][pos - p[id]] = dist[id][pos] + 1;
			dq.push_back({{id,pos-p[id]},d+1});
		}
	}
}
 
int32_t main() {
    cin.tie(nullptr)->sync_with_stdio(false);
	cin>>n>>m;
	for(int i = 0 ; i <m; i++) {
		cin>>b[i]>>p[i];
		here[b[i]].pb(i);
	}
	bfs();
	kill(-1);
	return 0;
}
#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...