///***LTT***///
/// ->NHAT QUOC GIA<- ///
#include<bits/stdc++.h>
//#pragma GCC optimize ("O3")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("popcnt")
//#define int long long
#define endl "\n"
#define F first
#define S second
#define pb push_back
using namespace std;
vector <int> lg2;
//void MAKE_LOG_ARR(int n){
// lg2.resize(n + 3);
// for (int i = 1;i <= n;++i){
// lg2[i] = __lg(i);
// }
//}
const long long oo = 1e9+7;
const int N = 2 * 1e5 + 10;
int n, a[N], m;
vector <int> adj[N];
priority_queue <pair <long long, int>> pq;
namespace sub2{
long long dis[N];
void solve(){
long long ans = oo * oo;
for (int i = 1;i <= n;++i){
for (int j = 1;j <= n;++j){
dis[j] = oo * oo;
}
dis[1] = 0;
pq.push({0,1});
while (!pq.empty()){
int u = pq.top().S;
long long d = -pq.top().F;
pq.pop();
for (int v : adj[u]){
if (a[v] > a[i]) continue;
int nxt = (d < a[v] ? a[v] : d + 1);
if (nxt < dis[v]){
dis[v] = nxt;
pq.push({-dis[v], v});
}
}
}
long long res = dis[i];
for (int j = 1;j <= n;++j){
dis[j] = oo * oo;
}
dis[n] = 0;
pq.push({0,n});
while (!pq.empty()){
int u = pq.top().S;
long long d = -pq.top().F;
pq.pop();
for (int v : adj[u]){
if (a[v] > a[i]) continue;
int nxt = (d < a[v] ? a[v] : d + 1);
if (nxt < dis[v]){
dis[v] = nxt;
pq.push({-dis[v], v});
}
}
}
res += dis[i];
ans = min(ans, res);
}
cout << ans;
}
};
namespace sub1{
long long dis[N], dis2[N];
void solve(){
int mx = 1;
for (int i = 2;i <= n;++i){
if (a[mx] < a[i]){
mx = i;
}
}
for (int i = 2;i <= n;++i){
int nxt = (dis[i - 1] < a[i] ? a[i] : dis[i - 1] + 1);
dis[i] = nxt;
}
for (int i = n - 1;i >= 1;--i){
int nxt = (dis2[i + 1] < a[i] ? a[i] : dis2[i + 1] + 1);
dis2[i] = nxt;
}
long long ans = oo * oo;
ans = min(ans, dis[mx] + dis2[mx]);
cout << ans;
return;
}
};
namespace ac{
long long d1[N], d2[N];
void solve(){
for (int i = 1;i <= n;++i){
d1[i] = oo * oo;
}
for (int i = 1;i <= n;++i){
d2[i] = oo * oo;
}
d1[1] = 0;
d2[n] = 0;
pq.push({0,1});
while (!pq.empty()){
int u = pq.top().S;
long long d = -pq.top().F;
pq.pop();
if (d > d1[u]) continue;
for (int v : adj[u]){
int nxt = (d < a[v] ? a[v] : d + 1);
if (nxt < d1[v]){
d1[v] = nxt;
pq.push({-d1[v], v});
}
}
}
pq.push({0,n});
while (!pq.empty()){
int u = pq.top().S;
long long d = -pq.top().F;
pq.pop();
if (d > d2[u]) continue;
for (int v : adj[u]){
int nxt = (d < a[v] ? a[v] : d + 1);
if (nxt < d2[v]){
d2[v] = nxt;
pq.push({-d2[v], v});
}
}
}
long long ans = oo * oo;
for (int i = 1;i <= n;++i){
ans = min(ans, max(d1[i], d2[i]) * 2);
for (int v : adj[i]){
ans = min(ans, max(d1[i], d2[v]) * 2 + 1);
}
}
cout << ans;
return;
}
};
void solve(){
cin >> n >> m;
for (int i = 1;i <= n;++i){
cin >> a[i];
}
bool cksub1 = true;
for (int i = 1;i <= m;++i){
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
if (v != u + 1){
cksub1 = false;
}
}
if (m == n - 1 and cksub1){
sub1::solve();
return;
}
if (n <= 2000 and m <= 4000){
sub2::solve();
return;
}
ac::solve();
return;
}
int main(){
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
cout.tie(NULL);
if (fopen("ltt.inp", "r")){
freopen("ltt.inp", "r", stdin);
freopen("ltt.out", "w", stdout);
}
// int t;
// cin >> t;
// while(t--){
solve();
// }
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:178:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
178 | freopen("ltt.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:179:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
179 | freopen("ltt.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6480 KB |
Output is correct |
2 |
Correct |
45 ms |
17668 KB |
Output is correct |
3 |
Correct |
47 ms |
17480 KB |
Output is correct |
4 |
Correct |
48 ms |
17628 KB |
Output is correct |
5 |
Correct |
46 ms |
17736 KB |
Output is correct |
6 |
Correct |
54 ms |
17664 KB |
Output is correct |
7 |
Correct |
49 ms |
17536 KB |
Output is correct |
8 |
Correct |
2 ms |
6480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6480 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8528 KB |
Output is correct |
4 |
Correct |
2 ms |
8528 KB |
Output is correct |
5 |
Correct |
2 ms |
8528 KB |
Output is correct |
6 |
Correct |
12 ms |
8788 KB |
Output is correct |
7 |
Correct |
78 ms |
8784 KB |
Output is correct |
8 |
Correct |
31 ms |
8784 KB |
Output is correct |
9 |
Correct |
197 ms |
8784 KB |
Output is correct |
10 |
Correct |
47 ms |
8784 KB |
Output is correct |
11 |
Correct |
213 ms |
8784 KB |
Output is correct |
12 |
Correct |
69 ms |
8784 KB |
Output is correct |
13 |
Correct |
250 ms |
8792 KB |
Output is correct |
14 |
Correct |
312 ms |
8800 KB |
Output is correct |
15 |
Correct |
410 ms |
8784 KB |
Output is correct |
16 |
Correct |
389 ms |
8784 KB |
Output is correct |
17 |
Correct |
388 ms |
8784 KB |
Output is correct |
18 |
Correct |
2 ms |
6480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6480 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8528 KB |
Output is correct |
4 |
Correct |
2 ms |
8528 KB |
Output is correct |
5 |
Correct |
2 ms |
8528 KB |
Output is correct |
6 |
Correct |
12 ms |
8788 KB |
Output is correct |
7 |
Correct |
78 ms |
8784 KB |
Output is correct |
8 |
Correct |
31 ms |
8784 KB |
Output is correct |
9 |
Correct |
197 ms |
8784 KB |
Output is correct |
10 |
Correct |
47 ms |
8784 KB |
Output is correct |
11 |
Correct |
213 ms |
8784 KB |
Output is correct |
12 |
Correct |
69 ms |
8784 KB |
Output is correct |
13 |
Correct |
250 ms |
8792 KB |
Output is correct |
14 |
Correct |
312 ms |
8800 KB |
Output is correct |
15 |
Correct |
410 ms |
8784 KB |
Output is correct |
16 |
Correct |
389 ms |
8784 KB |
Output is correct |
17 |
Correct |
388 ms |
8784 KB |
Output is correct |
18 |
Correct |
2 ms |
6480 KB |
Output is correct |
19 |
Correct |
58 ms |
8784 KB |
Output is correct |
20 |
Correct |
21 ms |
8784 KB |
Output is correct |
21 |
Correct |
79 ms |
8784 KB |
Output is correct |
22 |
Correct |
144 ms |
8784 KB |
Output is correct |
23 |
Correct |
25 ms |
8952 KB |
Output is correct |
24 |
Correct |
98 ms |
8784 KB |
Output is correct |
25 |
Correct |
179 ms |
8784 KB |
Output is correct |
26 |
Correct |
277 ms |
8784 KB |
Output is correct |
27 |
Correct |
312 ms |
8784 KB |
Output is correct |
28 |
Correct |
255 ms |
8832 KB |
Output is correct |
29 |
Correct |
345 ms |
8784 KB |
Output is correct |
30 |
Correct |
336 ms |
8784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6480 KB |
Output is correct |
2 |
Correct |
45 ms |
17668 KB |
Output is correct |
3 |
Correct |
47 ms |
17480 KB |
Output is correct |
4 |
Correct |
48 ms |
17628 KB |
Output is correct |
5 |
Correct |
46 ms |
17736 KB |
Output is correct |
6 |
Correct |
54 ms |
17664 KB |
Output is correct |
7 |
Correct |
49 ms |
17536 KB |
Output is correct |
8 |
Correct |
2 ms |
6480 KB |
Output is correct |
9 |
Correct |
2 ms |
6480 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
2 ms |
8528 KB |
Output is correct |
12 |
Correct |
2 ms |
8528 KB |
Output is correct |
13 |
Correct |
2 ms |
8528 KB |
Output is correct |
14 |
Correct |
12 ms |
8788 KB |
Output is correct |
15 |
Correct |
78 ms |
8784 KB |
Output is correct |
16 |
Correct |
31 ms |
8784 KB |
Output is correct |
17 |
Correct |
197 ms |
8784 KB |
Output is correct |
18 |
Correct |
47 ms |
8784 KB |
Output is correct |
19 |
Correct |
213 ms |
8784 KB |
Output is correct |
20 |
Correct |
69 ms |
8784 KB |
Output is correct |
21 |
Correct |
250 ms |
8792 KB |
Output is correct |
22 |
Correct |
312 ms |
8800 KB |
Output is correct |
23 |
Correct |
410 ms |
8784 KB |
Output is correct |
24 |
Correct |
389 ms |
8784 KB |
Output is correct |
25 |
Correct |
388 ms |
8784 KB |
Output is correct |
26 |
Correct |
2 ms |
6480 KB |
Output is correct |
27 |
Correct |
58 ms |
8784 KB |
Output is correct |
28 |
Correct |
21 ms |
8784 KB |
Output is correct |
29 |
Correct |
79 ms |
8784 KB |
Output is correct |
30 |
Correct |
144 ms |
8784 KB |
Output is correct |
31 |
Correct |
25 ms |
8952 KB |
Output is correct |
32 |
Correct |
98 ms |
8784 KB |
Output is correct |
33 |
Correct |
179 ms |
8784 KB |
Output is correct |
34 |
Correct |
277 ms |
8784 KB |
Output is correct |
35 |
Correct |
312 ms |
8784 KB |
Output is correct |
36 |
Correct |
255 ms |
8832 KB |
Output is correct |
37 |
Correct |
345 ms |
8784 KB |
Output is correct |
38 |
Correct |
336 ms |
8784 KB |
Output is correct |
39 |
Correct |
96 ms |
17896 KB |
Output is correct |
40 |
Correct |
79 ms |
18000 KB |
Output is correct |
41 |
Correct |
77 ms |
17992 KB |
Output is correct |
42 |
Correct |
92 ms |
17992 KB |
Output is correct |
43 |
Correct |
77 ms |
17996 KB |
Output is correct |
44 |
Correct |
77 ms |
17892 KB |
Output is correct |
45 |
Correct |
87 ms |
18000 KB |
Output is correct |
46 |
Correct |
83 ms |
17992 KB |
Output is correct |
47 |
Correct |
85 ms |
18024 KB |
Output is correct |
48 |
Correct |
94 ms |
17992 KB |
Output is correct |
49 |
Correct |
120 ms |
18004 KB |
Output is correct |
50 |
Correct |
108 ms |
17860 KB |
Output is correct |
51 |
Correct |
96 ms |
18024 KB |
Output is correct |
52 |
Correct |
154 ms |
19664 KB |
Output is correct |
53 |
Correct |
137 ms |
19648 KB |
Output is correct |
54 |
Correct |
154 ms |
19700 KB |
Output is correct |
55 |
Correct |
148 ms |
19648 KB |
Output is correct |
56 |
Correct |
176 ms |
23108 KB |
Output is correct |
57 |
Correct |
194 ms |
23188 KB |
Output is correct |
58 |
Correct |
181 ms |
23152 KB |
Output is correct |
59 |
Correct |
181 ms |
23228 KB |
Output is correct |
60 |
Correct |
170 ms |
23228 KB |
Output is correct |
61 |
Correct |
201 ms |
23144 KB |
Output is correct |
62 |
Correct |
169 ms |
23228 KB |
Output is correct |
63 |
Correct |
159 ms |
23080 KB |
Output is correct |
64 |
Correct |
177 ms |
23228 KB |
Output is correct |
65 |
Correct |
163 ms |
23228 KB |
Output is correct |
66 |
Correct |
181 ms |
23228 KB |
Output is correct |
67 |
Correct |
174 ms |
23228 KB |
Output is correct |