#include <bits/stdc++.h>
#define fi first
#define se second
#define all(v) v.begin() , v.end()
#define sz(v) int(v.size())
#define unq(v) sort(all(v)); v.resize(unique(all(v)) - v.begin());
using namespace std;
typedef long long ll;
typedef pair<int , int> ii;
typedef pair<long long , int> lli;
const int maxN = int(2e5)+7;
const int maxM = int(4e5)+7;
const ll inf = ll(1e18)+7;
int n , m , a[maxN];
ii E[maxM];
vector<int> g[maxN];
namespace sub1{
bool check(){
if (m != n - 1) return 0;
for (int i = 1 ; i <= m ; i++){
if (E[i].fi != i || E[i].se != i + 1) return 0;
}
return 1;
}
void solve(){
int p = -1;
for (int i = 1 ; i <= n ; i++){
if (p == -1 || a[p] < a[i]) p = i;
}
ll ans = 0;
int pre = 0 , suf = 0;
for (int i = 2 ; i <= p ; i++){
if (pre == a[p]){
ans++;
}
else{
ans += 1ll * max(a[i] - pre , 1);
pre = max(pre + 1 , a[i]);
}
}
for (int i = n - 1 ; i >= p ; i--){
if (suf == a[p]){
ans++;
}
else{
ans += 1ll * max(a[i] - suf , 1);
suf = max(suf + 1 , a[i]);
}
}
cout << ans << "\n";
}
}
namespace sub2{
ll d[2][maxN];
int h[2][maxN];
void dijkstra(int id , int st){
for (int i = 1 ; i <= n ; i++){
d[id][i] = inf;
h[id][i] = INT_MAX;
}
d[id][st] = 0;
h[id][st] = 0;
priority_queue<lli , vector<lli> , greater<lli>> pq;
pq.push({d[id][st] , st});
while (pq.empty() == 0){
auto it = pq.top(); pq.pop();
int u = it.se;
if (it.fi != d[id][u]) continue;
for (int v : g[u]){
int w = max(a[v] - h[id][u] , 1);
if (d[id][v] == d[id][u] + 1ll * w){
h[id][v] = min(h[id][v] , max(a[v] , h[id][u] + 1));
}
if (d[id][v] > d[id][u] + 1ll * w){
d[id][v] = d[id][u] + 1ll * w;
h[id][v] = max(a[v] , h[id][u] + 1);
pq.push({d[id][v] , v});
}
}
}
}
void solve(){
dijkstra(0 , 1);
dijkstra(1 , n);
ll ans = inf;
for (int i = 1 ; i <= n ; i++){
if (h[0][i] == h[1][i]){
ans = min(ans , d[0][i] + d[1][i]);
}
for (int j : g[i]){
if (h[0][i] == h[1][j]){
ans = min(ans , d[0][i] + d[1][j] + 1ll);
}
}
}
cout << ans << "\n";
}
}
void solve(){
cin >> n >> m;
for (int i = 1 ; i <= n ; i++) cin >> a[i];
for (int i = 1 ; i <= m ; i++){
int u , v;
cin >> u >> v;
E[i] = {u , v};
g[u].push_back(v);
g[v].push_back(u);
}
//if (sub1::check()) return sub1::solve();
return sub2::solve();
}
#define name "A"
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(name".INP" , "r")){
freopen(name".INP" , "r" , stdin);
freopen(name".OUT" , "w" , stdout);
}
int t = 1; //cin >> t;
while (t--) solve();
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:127:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
127 | freopen(name".INP" , "r" , stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:128:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
128 | freopen(name".OUT" , "w" , stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10576 KB |
Output is correct |
2 |
Correct |
50 ms |
19948 KB |
Output is correct |
3 |
Correct |
51 ms |
19760 KB |
Output is correct |
4 |
Correct |
57 ms |
19896 KB |
Output is correct |
5 |
Correct |
56 ms |
19784 KB |
Output is correct |
6 |
Correct |
56 ms |
19784 KB |
Output is correct |
7 |
Correct |
59 ms |
19740 KB |
Output is correct |
8 |
Correct |
3 ms |
10744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10576 KB |
Output is correct |
2 |
Correct |
2 ms |
10576 KB |
Output is correct |
3 |
Correct |
3 ms |
10576 KB |
Output is correct |
4 |
Correct |
2 ms |
10728 KB |
Output is correct |
5 |
Correct |
2 ms |
10740 KB |
Output is correct |
6 |
Correct |
4 ms |
10576 KB |
Output is correct |
7 |
Correct |
3 ms |
10576 KB |
Output is correct |
8 |
Correct |
3 ms |
10576 KB |
Output is correct |
9 |
Correct |
3 ms |
10576 KB |
Output is correct |
10 |
Correct |
3 ms |
10832 KB |
Output is correct |
11 |
Correct |
3 ms |
10832 KB |
Output is correct |
12 |
Correct |
3 ms |
10832 KB |
Output is correct |
13 |
Correct |
3 ms |
10832 KB |
Output is correct |
14 |
Correct |
3 ms |
10832 KB |
Output is correct |
15 |
Correct |
4 ms |
10920 KB |
Output is correct |
16 |
Correct |
3 ms |
10832 KB |
Output is correct |
17 |
Correct |
3 ms |
10908 KB |
Output is correct |
18 |
Correct |
2 ms |
10576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10576 KB |
Output is correct |
2 |
Correct |
2 ms |
10576 KB |
Output is correct |
3 |
Correct |
3 ms |
10576 KB |
Output is correct |
4 |
Correct |
2 ms |
10728 KB |
Output is correct |
5 |
Correct |
2 ms |
10740 KB |
Output is correct |
6 |
Correct |
4 ms |
10576 KB |
Output is correct |
7 |
Correct |
3 ms |
10576 KB |
Output is correct |
8 |
Correct |
3 ms |
10576 KB |
Output is correct |
9 |
Correct |
3 ms |
10576 KB |
Output is correct |
10 |
Correct |
3 ms |
10832 KB |
Output is correct |
11 |
Correct |
3 ms |
10832 KB |
Output is correct |
12 |
Correct |
3 ms |
10832 KB |
Output is correct |
13 |
Correct |
3 ms |
10832 KB |
Output is correct |
14 |
Correct |
3 ms |
10832 KB |
Output is correct |
15 |
Correct |
4 ms |
10920 KB |
Output is correct |
16 |
Correct |
3 ms |
10832 KB |
Output is correct |
17 |
Correct |
3 ms |
10908 KB |
Output is correct |
18 |
Correct |
2 ms |
10576 KB |
Output is correct |
19 |
Correct |
4 ms |
10832 KB |
Output is correct |
20 |
Correct |
3 ms |
10832 KB |
Output is correct |
21 |
Correct |
4 ms |
10832 KB |
Output is correct |
22 |
Correct |
3 ms |
10832 KB |
Output is correct |
23 |
Correct |
3 ms |
10832 KB |
Output is correct |
24 |
Correct |
3 ms |
10776 KB |
Output is correct |
25 |
Correct |
3 ms |
10832 KB |
Output is correct |
26 |
Correct |
4 ms |
10832 KB |
Output is correct |
27 |
Correct |
3 ms |
10832 KB |
Output is correct |
28 |
Correct |
3 ms |
10832 KB |
Output is correct |
29 |
Correct |
4 ms |
10832 KB |
Output is correct |
30 |
Correct |
3 ms |
10832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10576 KB |
Output is correct |
2 |
Correct |
50 ms |
19948 KB |
Output is correct |
3 |
Correct |
51 ms |
19760 KB |
Output is correct |
4 |
Correct |
57 ms |
19896 KB |
Output is correct |
5 |
Correct |
56 ms |
19784 KB |
Output is correct |
6 |
Correct |
56 ms |
19784 KB |
Output is correct |
7 |
Correct |
59 ms |
19740 KB |
Output is correct |
8 |
Correct |
3 ms |
10744 KB |
Output is correct |
9 |
Correct |
2 ms |
10576 KB |
Output is correct |
10 |
Correct |
2 ms |
10576 KB |
Output is correct |
11 |
Correct |
3 ms |
10576 KB |
Output is correct |
12 |
Correct |
2 ms |
10728 KB |
Output is correct |
13 |
Correct |
2 ms |
10740 KB |
Output is correct |
14 |
Correct |
4 ms |
10576 KB |
Output is correct |
15 |
Correct |
3 ms |
10576 KB |
Output is correct |
16 |
Correct |
3 ms |
10576 KB |
Output is correct |
17 |
Correct |
3 ms |
10576 KB |
Output is correct |
18 |
Correct |
3 ms |
10832 KB |
Output is correct |
19 |
Correct |
3 ms |
10832 KB |
Output is correct |
20 |
Correct |
3 ms |
10832 KB |
Output is correct |
21 |
Correct |
3 ms |
10832 KB |
Output is correct |
22 |
Correct |
3 ms |
10832 KB |
Output is correct |
23 |
Correct |
4 ms |
10920 KB |
Output is correct |
24 |
Correct |
3 ms |
10832 KB |
Output is correct |
25 |
Correct |
3 ms |
10908 KB |
Output is correct |
26 |
Correct |
2 ms |
10576 KB |
Output is correct |
27 |
Correct |
4 ms |
10832 KB |
Output is correct |
28 |
Correct |
3 ms |
10832 KB |
Output is correct |
29 |
Correct |
4 ms |
10832 KB |
Output is correct |
30 |
Correct |
3 ms |
10832 KB |
Output is correct |
31 |
Correct |
3 ms |
10832 KB |
Output is correct |
32 |
Correct |
3 ms |
10776 KB |
Output is correct |
33 |
Correct |
3 ms |
10832 KB |
Output is correct |
34 |
Correct |
4 ms |
10832 KB |
Output is correct |
35 |
Correct |
3 ms |
10832 KB |
Output is correct |
36 |
Correct |
3 ms |
10832 KB |
Output is correct |
37 |
Correct |
4 ms |
10832 KB |
Output is correct |
38 |
Correct |
3 ms |
10832 KB |
Output is correct |
39 |
Correct |
130 ms |
24148 KB |
Output is correct |
40 |
Correct |
107 ms |
23112 KB |
Output is correct |
41 |
Correct |
109 ms |
23368 KB |
Output is correct |
42 |
Correct |
100 ms |
23356 KB |
Output is correct |
43 |
Correct |
89 ms |
23112 KB |
Output is correct |
44 |
Correct |
87 ms |
23372 KB |
Output is correct |
45 |
Correct |
86 ms |
23112 KB |
Output is correct |
46 |
Correct |
94 ms |
23120 KB |
Output is correct |
47 |
Correct |
100 ms |
23376 KB |
Output is correct |
48 |
Correct |
87 ms |
23368 KB |
Output is correct |
49 |
Correct |
110 ms |
24132 KB |
Output is correct |
50 |
Correct |
99 ms |
23808 KB |
Output is correct |
51 |
Correct |
99 ms |
23740 KB |
Output is correct |
52 |
Correct |
195 ms |
27432 KB |
Output is correct |
53 |
Correct |
162 ms |
26668 KB |
Output is correct |
54 |
Correct |
153 ms |
26672 KB |
Output is correct |
55 |
Correct |
232 ms |
26904 KB |
Output is correct |
56 |
Correct |
213 ms |
30504 KB |
Output is correct |
57 |
Correct |
193 ms |
30652 KB |
Output is correct |
58 |
Correct |
211 ms |
31448 KB |
Output is correct |
59 |
Correct |
240 ms |
31556 KB |
Output is correct |
60 |
Correct |
247 ms |
31952 KB |
Output is correct |
61 |
Correct |
267 ms |
31164 KB |
Output is correct |
62 |
Correct |
216 ms |
30648 KB |
Output is correct |
63 |
Correct |
191 ms |
30648 KB |
Output is correct |
64 |
Correct |
248 ms |
30480 KB |
Output is correct |
65 |
Correct |
190 ms |
30548 KB |
Output is correct |
66 |
Correct |
246 ms |
30612 KB |
Output is correct |
67 |
Correct |
260 ms |
30816 KB |
Output is correct |