#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <array>
#include <cassert>
#include <bitset>
using namespace std;
using LL = long long;
namespace std {
template <>
class hash<std::pair<int, int>> {
public:
size_t operator()(const std::pair<int, int>& x) const {
//return hash<int>()(x.first) ^ hash<int>()(x.second);
return (size_t)(x.second ^ x.first + 0x9e3779b9 + (x.second << 6) + (x.second >> 2));
}
};
}
//Vは比較可能な値にする
template <typename V>
class Graph {
private:
//unordered_map<V, vector<pair<V, LL>>>edge;
vector<pair<pair<V, V>, LL>>raw;
vector<V>vset;
public:
Graph() {
raw.reserve(30000 * 5);
vset.reserve(30000 * 5);
}
//from->toへcostの辺を張る
void addEdge(const V& from, const V& to, const LL& cost) {
//edge[from].push_back(make_pair(to, cost));
raw.push_back(make_pair(make_pair(from, to), cost));
vset.push_back(from);
vset.push_back(to);
}
//v1とv2間にcostの辺を張る
void addUndirectedEdge(const V& v1, const V& v2, const LL& cost) {
//edge[v1].push_back(make_pair(v2, cost));
//edge[v2].push_back(make_pair(v1, cost));
raw.push_back(make_pair(make_pair(v1, v2), cost));
raw.push_back(make_pair(make_pair(v2, v1), cost));
vset.push_back(v1);
vset.push_back(v2);
}
//最短を求める
LL dijkstra(V start, V finish) {
sort(vset.begin(), vset.end());
vset.erase(unique(vset.begin(), vset.end()), vset.end());
int vnum = vset.size();
vector<LL>disti(vnum, LLONG_MAX / 9);
unordered_map<V, int>inv;
for (int i = 0; i < vnum; ++i) {
inv[vset[i]] = i;
}
vector<vector<pair<int, LL>>>edgei(vnum);
for (auto& e : raw) {
edgei[inv[e.first.first]].push_back(make_pair(inv[e.first.second], e.second));
}
//dist[start] = 0;
//priority_queue<pair<LL, V>, vector<pair<LL, V>>, greater<pair<LL, V>>>que;
disti[inv[start]] = 0;
priority_queue<pair<LL, int>, vector<pair<LL, int>>, greater<pair<LL, int>>>que;
que.push(make_pair(0LL, inv[start]));
while (!que.empty()) {
auto q = que.top();
que.pop();
int now = q.second;
LL dis = q.first;
if (disti[now] < dis) continue;
for (auto e : edgei[now]) {
int nxt = e.first;
LL cost = e.second;
if (/*!disti.count(nxt) ||*/ disti[nxt] > disti[now] + cost) {
disti[nxt] = disti[now] + cost;
que.push(make_pair(disti[nxt], nxt));
}
}
}
for (int i = 0; i < vnum; ++i) {
//dist[vset[i]] = disti[i];
if (vset[i] == finish) {
return disti[i];
}
}
abort();
}
};
int N, M;
int mceil(int a, int b) {
if (a == 0)return 0;
return (a - 1) / b + 1;
}
int main(void)
{
//cin >> N >> M;
scanf("%d %d", &N, &M);
using P = pair<int, int>;
unordered_map<P, vector<int>>mp;
int st = -1;
int enb = -1, enp = -1, enr = -1;
for (int i = 0; i < M; ++i) {
int b, p;
//cin >> b >> p;
scanf("%d %d", &b, &p);
if (i == 0)st = b;
if (i == 1) {
enb = b;
enp = p;
enr = b % p;
}
int rem = b % p;
mp[make_pair(p, rem)].push_back(b);
}
Graph<P>G;
LL cntdebug = 0;
for (auto elm : mp) {
int p = elm.first.first;
int rem = elm.first.second;
auto& v = elm.second;
int len = mceil(N - rem, p);
vector<LL>dist(len, INT_MAX);
for (int x : v)dist[x / p] = 0;
/*for (int i = 1; i < len; ++i) {
dist[i] = min(dist[i - 1] + 1, dist[i]);
}
for (int i = len - 1; i > 0; --i) {
dist[i - 1] = min(dist[i] + 1, dist[i - 1]);
}*/
for (int i = 0; i < len; ++i) {
if (dist[i] < INT_MAX)G.addEdge(P(0, i * p + rem), P(p, i * p + rem), dist[i]);
G.addEdge(P(p, i * p + rem), P(0, i * p + rem), 0LL);
}
for (int i = 1; i < len; ++i) {
G.addUndirectedEdge(P(p, (i - 1)*p + rem), P(p, i*p + rem), 1LL);
}
cntdebug += len;
}
//cout << cntdebug << endl;
int len = mceil(N - enr, enp);
G.addEdge(P(0, (enb / enp) * enp + enr), P(N, (enb / enp) * enp + enr), 0LL);
P endv{};
for (int i = 0; i < len; ++i) {
if (abs(i - (enb / enp)) == 0)
endv = P(N, i * enp + enr);
}
LL ans = G.dijkstra(P(0, st), endv);
if (ans >= LLONG_MAX / 9) {
//cout << -1 << endl;
printf("-1\n");
}
else {
//cout << ans << endl;
printf("%lld\n", ans);
}
return 0;
}
Compilation message
skyscraper.cpp: In member function 'std::size_t std::hash<std::pair<int, int> >::operator()(const std::pair<int, int>&) const':
skyscraper.cpp:36:70: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
return (size_t)(x.second ^ x.first + 0x9e3779b9 + (x.second << 6) + (x.second >> 2));
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:123:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &M);
~~~~~^~~~~~~~~~~~~~~~~
skyscraper.cpp:131:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &b, &p);
~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
432 KB |
Output is correct |
4 |
Correct |
2 ms |
508 KB |
Output is correct |
5 |
Correct |
2 ms |
652 KB |
Output is correct |
6 |
Correct |
2 ms |
652 KB |
Output is correct |
7 |
Correct |
2 ms |
652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
652 KB |
Output is correct |
2 |
Correct |
2 ms |
680 KB |
Output is correct |
3 |
Correct |
2 ms |
680 KB |
Output is correct |
4 |
Correct |
2 ms |
680 KB |
Output is correct |
5 |
Correct |
2 ms |
680 KB |
Output is correct |
6 |
Correct |
2 ms |
680 KB |
Output is correct |
7 |
Correct |
2 ms |
680 KB |
Output is correct |
8 |
Correct |
2 ms |
680 KB |
Output is correct |
9 |
Correct |
2 ms |
680 KB |
Output is correct |
10 |
Correct |
4 ms |
1020 KB |
Output is correct |
11 |
Correct |
8 ms |
1916 KB |
Output is correct |
12 |
Correct |
3 ms |
1916 KB |
Output is correct |
13 |
Correct |
2 ms |
1916 KB |
Output is correct |
14 |
Correct |
9 ms |
2300 KB |
Output is correct |
15 |
Correct |
9 ms |
2300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2300 KB |
Output is correct |
2 |
Correct |
2 ms |
2300 KB |
Output is correct |
3 |
Correct |
2 ms |
2300 KB |
Output is correct |
4 |
Correct |
2 ms |
2300 KB |
Output is correct |
5 |
Correct |
2 ms |
2300 KB |
Output is correct |
6 |
Correct |
2 ms |
2300 KB |
Output is correct |
7 |
Correct |
2 ms |
2300 KB |
Output is correct |
8 |
Correct |
2 ms |
2300 KB |
Output is correct |
9 |
Correct |
2 ms |
2300 KB |
Output is correct |
10 |
Correct |
4 ms |
2300 KB |
Output is correct |
11 |
Correct |
8 ms |
2300 KB |
Output is correct |
12 |
Correct |
3 ms |
2300 KB |
Output is correct |
13 |
Correct |
2 ms |
2300 KB |
Output is correct |
14 |
Correct |
9 ms |
2300 KB |
Output is correct |
15 |
Correct |
9 ms |
2432 KB |
Output is correct |
16 |
Correct |
6 ms |
2432 KB |
Output is correct |
17 |
Correct |
26 ms |
4860 KB |
Output is correct |
18 |
Correct |
10 ms |
4860 KB |
Output is correct |
19 |
Correct |
8 ms |
4860 KB |
Output is correct |
20 |
Correct |
5 ms |
4860 KB |
Output is correct |
21 |
Correct |
7 ms |
4860 KB |
Output is correct |
22 |
Correct |
9 ms |
4860 KB |
Output is correct |
23 |
Correct |
11 ms |
4860 KB |
Output is correct |
24 |
Correct |
19 ms |
4860 KB |
Output is correct |
25 |
Correct |
13 ms |
4860 KB |
Output is correct |
26 |
Correct |
12 ms |
4860 KB |
Output is correct |
27 |
Correct |
10 ms |
4860 KB |
Output is correct |
28 |
Correct |
34 ms |
6912 KB |
Output is correct |
29 |
Correct |
98 ms |
17444 KB |
Output is correct |
30 |
Correct |
26 ms |
17444 KB |
Output is correct |
31 |
Correct |
45 ms |
17444 KB |
Output is correct |
32 |
Correct |
32 ms |
17444 KB |
Output is correct |
33 |
Correct |
194 ms |
33524 KB |
Output is correct |
34 |
Correct |
198 ms |
33644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
33644 KB |
Output is correct |
2 |
Correct |
2 ms |
33644 KB |
Output is correct |
3 |
Correct |
2 ms |
33644 KB |
Output is correct |
4 |
Correct |
2 ms |
33644 KB |
Output is correct |
5 |
Correct |
2 ms |
33644 KB |
Output is correct |
6 |
Correct |
2 ms |
33644 KB |
Output is correct |
7 |
Correct |
2 ms |
33644 KB |
Output is correct |
8 |
Correct |
2 ms |
33644 KB |
Output is correct |
9 |
Correct |
2 ms |
33644 KB |
Output is correct |
10 |
Correct |
5 ms |
33644 KB |
Output is correct |
11 |
Correct |
8 ms |
33644 KB |
Output is correct |
12 |
Correct |
3 ms |
33644 KB |
Output is correct |
13 |
Correct |
2 ms |
33644 KB |
Output is correct |
14 |
Correct |
10 ms |
33644 KB |
Output is correct |
15 |
Correct |
11 ms |
33644 KB |
Output is correct |
16 |
Correct |
6 ms |
33644 KB |
Output is correct |
17 |
Correct |
22 ms |
33644 KB |
Output is correct |
18 |
Correct |
10 ms |
33644 KB |
Output is correct |
19 |
Correct |
9 ms |
33644 KB |
Output is correct |
20 |
Correct |
5 ms |
33644 KB |
Output is correct |
21 |
Correct |
5 ms |
33644 KB |
Output is correct |
22 |
Correct |
8 ms |
33644 KB |
Output is correct |
23 |
Correct |
11 ms |
33644 KB |
Output is correct |
24 |
Correct |
20 ms |
33644 KB |
Output is correct |
25 |
Correct |
13 ms |
33644 KB |
Output is correct |
26 |
Correct |
15 ms |
33644 KB |
Output is correct |
27 |
Correct |
10 ms |
33644 KB |
Output is correct |
28 |
Correct |
31 ms |
33644 KB |
Output is correct |
29 |
Correct |
91 ms |
33644 KB |
Output is correct |
30 |
Correct |
28 ms |
33644 KB |
Output is correct |
31 |
Correct |
48 ms |
33644 KB |
Output is correct |
32 |
Correct |
32 ms |
33644 KB |
Output is correct |
33 |
Correct |
204 ms |
33704 KB |
Output is correct |
34 |
Correct |
209 ms |
33704 KB |
Output is correct |
35 |
Correct |
221 ms |
33704 KB |
Output is correct |
36 |
Correct |
30 ms |
33704 KB |
Output is correct |
37 |
Correct |
436 ms |
48676 KB |
Output is correct |
38 |
Correct |
384 ms |
48676 KB |
Output is correct |
39 |
Correct |
384 ms |
48676 KB |
Output is correct |
40 |
Correct |
374 ms |
48676 KB |
Output is correct |
41 |
Correct |
372 ms |
48676 KB |
Output is correct |
42 |
Correct |
18 ms |
48676 KB |
Output is correct |
43 |
Correct |
16 ms |
48676 KB |
Output is correct |
44 |
Correct |
12 ms |
48676 KB |
Output is correct |
45 |
Execution timed out |
1081 ms |
131072 KB |
Time limit exceeded |
46 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
131072 KB |
Output is correct |
2 |
Correct |
2 ms |
131072 KB |
Output is correct |
3 |
Correct |
2 ms |
131072 KB |
Output is correct |
4 |
Correct |
2 ms |
131072 KB |
Output is correct |
5 |
Correct |
2 ms |
131072 KB |
Output is correct |
6 |
Correct |
2 ms |
131072 KB |
Output is correct |
7 |
Correct |
2 ms |
131072 KB |
Output is correct |
8 |
Correct |
2 ms |
131072 KB |
Output is correct |
9 |
Correct |
2 ms |
131072 KB |
Output is correct |
10 |
Correct |
4 ms |
131072 KB |
Output is correct |
11 |
Correct |
8 ms |
131072 KB |
Output is correct |
12 |
Correct |
3 ms |
131072 KB |
Output is correct |
13 |
Correct |
2 ms |
131072 KB |
Output is correct |
14 |
Correct |
10 ms |
131072 KB |
Output is correct |
15 |
Correct |
9 ms |
131072 KB |
Output is correct |
16 |
Correct |
6 ms |
131072 KB |
Output is correct |
17 |
Correct |
22 ms |
131072 KB |
Output is correct |
18 |
Correct |
11 ms |
131072 KB |
Output is correct |
19 |
Correct |
7 ms |
131072 KB |
Output is correct |
20 |
Correct |
5 ms |
131072 KB |
Output is correct |
21 |
Correct |
5 ms |
131072 KB |
Output is correct |
22 |
Correct |
8 ms |
131072 KB |
Output is correct |
23 |
Correct |
15 ms |
131072 KB |
Output is correct |
24 |
Correct |
20 ms |
131072 KB |
Output is correct |
25 |
Correct |
12 ms |
131072 KB |
Output is correct |
26 |
Correct |
11 ms |
131072 KB |
Output is correct |
27 |
Correct |
10 ms |
131072 KB |
Output is correct |
28 |
Correct |
33 ms |
131072 KB |
Output is correct |
29 |
Correct |
89 ms |
131072 KB |
Output is correct |
30 |
Correct |
27 ms |
131072 KB |
Output is correct |
31 |
Correct |
56 ms |
131072 KB |
Output is correct |
32 |
Correct |
34 ms |
131072 KB |
Output is correct |
33 |
Correct |
207 ms |
131072 KB |
Output is correct |
34 |
Correct |
231 ms |
131072 KB |
Output is correct |
35 |
Correct |
229 ms |
131072 KB |
Output is correct |
36 |
Correct |
30 ms |
131072 KB |
Output is correct |
37 |
Correct |
398 ms |
131072 KB |
Output is correct |
38 |
Correct |
398 ms |
131072 KB |
Output is correct |
39 |
Correct |
390 ms |
131072 KB |
Output is correct |
40 |
Correct |
375 ms |
131072 KB |
Output is correct |
41 |
Correct |
379 ms |
131072 KB |
Output is correct |
42 |
Correct |
24 ms |
131072 KB |
Output is correct |
43 |
Correct |
24 ms |
131072 KB |
Output is correct |
44 |
Correct |
11 ms |
131072 KB |
Output is correct |
45 |
Execution timed out |
1096 ms |
134364 KB |
Time limit exceeded |
46 |
Halted |
0 ms |
0 KB |
- |