이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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);
}
}
}
}
ll D[maxN];
int H[maxN];
void solve(){
dijkstra(0 , 1);
dijkstra(1 , n);
priority_queue<lli , vector<lli> , greater<lli>> pq;
for (int i = 1 ; i <= n ; i++){
D[i] = d[0][i];
H[i] = h[0][i];
if (D[i] != inf){
pq.push({D[i] , i});
}
}
while (pq.empty() == 0){
auto it = pq.top(); pq.pop();
int u = it.se;
for (int v : g[u]){
if (H[u] < a[v]) continue;
if (D[v] == D[u] + 1){
H[v] = min(H[v] , H[u]);
}
if (D[v] > D[u] + 1){
D[v] = D[u] + 1;
H[v] = H[u];
pq.push({D[v] , v});
}
}
}
ll ans = inf;
for (int i = 1 ; i <= n ; i++){
ans = min(ans , D[i] + d[1][i] + 1ll * abs(H[i] - h[1][i]));
}
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;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
Main.cpp:145:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
145 | freopen(name".INP" , "r" , stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:146:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
146 | freopen(name".OUT" , "w" , stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |