Submission #960807

#TimeUsernameProblemLanguageResultExecution timeMemory
960807_rain_Jakarta Skyscrapers (APIO15_skyscraper)C++14
36 / 100
186 ms54100 KiB
/** author : _RAIN_ **/ #include<bits/stdc++.h> using namespace std; using i64 = long long; using ui64 = unsigned long long; #define MASK(x) ((i64)(1) << (x)) #define BIT(mask , x) (((mask) >> (x)) & (1)) template<class T> bool maximize(T &a , T b) {if (a < b) return a = b , true; else return false;} template<class T> bool minimize(T &a , T b) {if (a > b) return a = b , true; else return false;} template<class T> T gcd(T x , T y) {while (y) swap(y , x %= y); return x;} template<class T> T lcm(T x , T y) {return (x * y) / gcd(x , y);} const int maxn = 3e4; int b[maxn + 2] , p[maxn + 2] , idx[maxn + 2]; int n , numperson; namespace subtask1 { bool check() { return n <= (int)2e3 && numperson <= (int)2e3; } const int N = (int)2e3; int cost[N + 2][N + 2]; int d[N + 2] ; vector<int> g[N + 2]; void main_code(void) { memset(cost , 0x3f , sizeof cost); memset(d , 0x3f , sizeof d); for (int i = 1; i <= numperson; ++i) { for (int j = 1; j <= numperson; ++j) { if (b[i] != b[j]) { int dist = abs(b[i] - b[j]); if (dist % p[i] == 0) { g[b[i]].emplace_back(b[j]); minimize(cost[b[i]][b[j]] , dist / p[i]); } if (dist % p[j] == 0) { g[b[j]].emplace_back(b[i]); minimize(cost[b[j]][b[i]] , dist / p[j]); } } } } priority_queue<pair<i64 ,int> , vector<pair<i64 , int>> , greater<pair<i64 , int>>> q; d[b[1]] = 0; q.emplace(d[b[1]] , b[1]); while (q.size()) { int u = q.top().second; int val = q.top().first; q.pop(); if (val != d[u]) continue; for (int& v : g[u]) { if (minimize(d[v] , d[u] + cost[u][v])) q.emplace(d[v] , v); } } cout << (d[b[2]] == d[n + 1] ? -1 : d[b[2]]); } } namespace subtask2 { bool check(void) { return true; } const int N = (int)2e3; vector<int> g[N + 2]; vector<int> divisor[N + 2]; int cost[N + 2][N + 2]; bool ok[N + 2][N + 2]; int d[N + 2]; void prepare(void) { for (int i = 1; i <= numperson; ++i) ok[b[i]][p[i]] = true; for (int i = 1; i <= trunc(sqrt(N)); ++i) { divisor[i * i].emplace_back(i); for (int j = i + 1; j <= N / i; ++j) { divisor[i * j].emplace_back(i); divisor[i * j].emplace_back(j); } } for (int i = 1; i <= N; ++i) sort(divisor[i].begin() , divisor[i].end()); return; } void main_code(void) { prepare(); memset(cost , 0x3f , sizeof cost); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i != j) { int dist = abs(i - j); for (auto& v : divisor[dist]) { if (ok[i][v]) { g[i].emplace_back(j); cost[i][j] = dist / v; break; } } for (auto& v : divisor[dist]) { if (ok[j][v]) { g[j].emplace_back(i); cost[j][i] = dist / v; break; } } } } } memset(d , 0x3f , sizeof d); priority_queue<pair<i64 ,int> , vector<pair<i64 , int>> , greater<pair<i64 , int>>> q; d[b[1]] = 0; q.emplace(d[b[1]] , b[1]); while (q.size()) { int u = q.top().second; int val = q.top().first; q.pop(); if (val != d[u]) continue; for (int& v : g[u]) { if (minimize(d[v] , d[u] + cost[u][v])) q.emplace(d[v] , v); } } cout << (d[b[2]] == d[n + 1] ? -1 : d[b[2]]); } } int32_t main(void) { cin.tie(nullptr)->sync_with_stdio(false); const string name = "main"; if (fopen((name + ".inp").c_str() , "r")) { (void)!freopen((name + ".inp").c_str() , "r" , stdin); (void)!freopen((name + ".out").c_str() , "w+", stdout); } cin >> n >> numperson; for (int i = 1; i <= numperson; ++i) cin >> b[i] >> p[i]; if (subtask1::check()) return subtask1::main_code() , 0; if (subtask2::check()) return subtask2::main_code() , 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...