Submission #961975

#TimeUsernameProblemLanguageResultExecution timeMemory
961975KakarotCat Exercise (JOI23_ho_t4)C++98
0 / 100
1 ms348 KiB
#include <bits/stdc++.h> #define int int64_t using namespace std; void setIO() { cin.tie(0)->sync_with_stdio(0); } void solve() { //cout << "tc/death"; int n, idx; cin >> n; vector<pair<int, int>> tc(n); for(int i = 0; i < n; i++) { cin >> tc[i].first; tc[i].second = i; if(tc[i].first == n) idx = tc[i].second; } vector<vector<int>> adj(n); for(int i = 0; i < n-1; i++) { int u, v; cin >> u >> v; adj[--u].push_back(--v); adj[v].push_back(u); } priority_queue<pair<int, int>> pq; pq.push({n, idx}); int ans = 0; if(idx-1 > n-idx) { for(int i = 0; i < idx; i++) { pq.push(tc[i]); } while(!pq.empty()) { auto a = pq.top(); pq.pop(); if(pq.empty()) break; ans += abs(a.second-pq.top().second); } } else { for(int i = idx+1; i < n; i++) { pq.push(tc[i]); } while(!pq.empty()) { auto a = pq.top(); pq.pop(); if(pq.empty()) break; ans += abs(a.second-pq.top().second); } } cout << ans; } int32_t main() { setIO(); solve(); return 0; }

Compilation message (stderr)

Main.cpp: In function 'void solve()':
Main.cpp:29:26: warning: 'idx' may be used uninitialized in this function [-Wmaybe-uninitialized]
   29 |         for(int i = 0; i < idx; i++) {
      |                        ~~^~~~~
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...