// author : anhtun_hi , nqg
// 25-26.12.2024
#include <bits/stdc++.h>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define reset(h, v) memset(h, v, sizeof h)
#define Forv(i, a) for(auto& i : a)
#define For(i, a, b) for(int i = a; i <= b; ++i)
#define Ford(i, a, b) for(int i = a; i >= b; --i)
#define c_bit(i) __builtin_popcountll(i)
#define Bit(mask, i) ((mask >> i) & 1LL)
#define onbit(mask, i) ((mask) bitor (1LL << i))
#define offbit(mask, i) ((mask) &~ (1LL << i))
#define Log2(x) (63 - __builtin_clzll(x))
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
using namespace std;
using ll = long long;
using ii = pair<ll, ll>;
using db = long double;
using ull = unsigned long long;
namespace std {
template <typename T, int D>
struct _vector : public vector <_vector <T, D - 1>> {
static_assert(D >= 1, "Dimension must be positive!");
template <typename... Args>
_vector(int n = 0, Args... args) : vector <_vector <T, D - 1>> (n, _vector <T, D - 1> (args...)) {}
};
template <typename T> struct _vector <T, 1> : public vector <T> {
_vector(int n = 0, const T& val = T()) : vector <T> (n, val) {}
};
template <class A, class B> bool minimize(A &a, B b){return a > b ? a = b, true : false;}
template <class A, class B> bool maximize(A &a, B b){return a < b ? a = b, true : false;}
}
const int dx[] = {0, 0, +1, -1}, dy[] = {-1, +1, 0, 0}, LOG = 20, base = 311, inf = 1e9 + 5;
const int MAXN = 1e6 + 5;
const ll mod = 1e9 + 7;
const ll oo = 1e18;
//#define int long long
int n, d[MAXN], pos[MAXN];
array<ll, 3> E[MAXN];
struct disjoint_set{
int par[MAXN];
void init() {
For(i, 1, n) par[i] = -1;
}
int find(int u) {
return (par[u] < 0) ? u : (par[u] = find(par[u]));
}
bool merge(int u, int v) {
u = find(u), v = find(v);
if (u == v) return false;
if (par[u] > par[v]) swap(u, v);
par[u] += par[v], par[v] = u;
return true;
}
} dsu;
void Solve() {
cin >> n;
For(i, 1, n) cin >> d[i];
sort(d + 1, d + n + 1);
priority_queue<ii, vector<ii>, greater<ii>> pq;
For(i, 1, n){
pos[i] = i + 1;
if (pos[i] <= n) pq.push({d[i] + d[pos[i]], i});
}
int cntEdge = 0;
while(pq.size() && TIME <= 0.9){
auto [val, i] = pq.top(); pq.pop();
E[++cntEdge] = {val, i, pos[i]};
++pos[i];
if (pos[i] <= n) pq.push({d[i] + d[pos[i]], i});
}
sort(E + 1, E + cntEdge + 1);
int cnt = 0; ll ans = 0;
dsu.init();
For(i, 1, cntEdge){
if (cnt == n - 1) break;
auto [w, u, v] = E[i];
// cerr << u << ' ' << v << '\n';
if (dsu.merge(u, v)){
++cnt;
ans += w;
}
}
cout << ans << '\n';
}
signed main() {
cin.tie(0) -> sync_with_stdio(0);
if(fopen("a.inp", "r")) {
freopen("a.inp", "r", stdin);
freopen("a.out", "w", stdout);
}
int t = 1;
// cin >> t;
while(t --) Solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
Main.cpp:99:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | freopen("a.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
Main.cpp:100:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
100 | freopen("a.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... |