QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#809285 | #4246. Cactus is Money | ucup-team2172# | WA | 2ms | 9980kb | C++23 | 3.9kb | 2024-12-11 13:48:49 | 2024-12-11 13:48:50 |
Judging History
answer
#pragma GCC optimize("Ofast", "O3", "inline", "unroll-loops")
#include <bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
#define fi first
#define se second
template <class T>
inline void read(T &x){
int ch = 0, f = 0; x = 0;
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
if(f) x = -x;
}
const int N = 300005;
vector<vector<int> > groups;
vector<int> e[N], sta, esta;
pair<int, int> es[N];
int mark[N], a[N], b[N], dfn[N], low[N], n, m, idx;
inline void dfs(int u){
dfn[u] = low[u] = ++idx;
sta.push_back(u);
for(auto ind : e[u]) if(!mark[ind]){
mark[ind] = 1;
esta.push_back(ind);
auto [x, y] = es[ind];
int v = u ^ x ^ y;
if(!dfn[v]){
dfs(v);
low[u] = min(low[u], low[v]);
if(low[v] >= dfn[u]){
while(1){
int z = sta.back();
sta.pop_back();
if(z == v) break;
}
vector<int> vec;
while(1){
int z = esta.back();
esta.pop_back();
vec.push_back(z);
if(z == ind) break;
}
groups.push_back(vec);
}
}
else low[u] = min(low[u], dfn[v]);
}
}
vector<pair<ll, ll> >gethull(vector<int> vec){
ll A = 0, B = 0;
vector<pair<ll, ll> > res;
if(vec.size() == 1){
res.push_back(make_pair(a[vec[0]], b[vec[0]]));
return res;
}
vector<pair<int, int> > tmp;
tmp.clear(), res.clear();
for(auto i : vec){
A += a[i];
B += b[i];
tmp.push_back(make_pair(a[i], b[i]));
}
sort(tmp.begin(), tmp.end());
reverse(tmp.begin(), tmp.end());
int maxb = -1;
for(auto [x, y] : tmp){
if(y > maxb){
maxb = y;
res.push_back(make_pair(A - x, B - y));
}
}
return res;
}
vector<pair<ll, ll> > merge(vector<pair<ll, ll> > a, vector<pair<ll, ll> > b){
vector<pair<ll, ll> > res;
for(int i = a.size() - 1; i >= 1; i--){
a[i].fi = a[i].fi - a[i - 1].fi;
a[i].se = a[i].se - a[i - 1].se;
}
for(int i = b.size() - 1; i >= 1; i--){
b[i].fi = b[i].fi - b[i - 1].fi;
b[i].se = b[i].se - b[i - 1].se;
}
res.push_back(make_pair(a[0].fi + b[0].fi, a[0].se + b[0].se));
int i = 1, j = 1;
while(i < a.size() && j < b.size()){
if(a[i].se * b[j].fi >= b[j].se * a[i].fi){
res.push_back(a[i++]);
}
else{
res.push_back(b[j++]);
}
}
while(i < a.size()) res.push_back(a[i++]);
while(j < b.size()) res.push_back(b[j++]);
for(int k = 1; k < res.size(); k++){
res[k].fi += res[k - 1].fi;
res[k].se += res[k - 1].se;
}
return res;
}
int main(){
read(n), read(m);
for(int i = 1, x, y; i <= m; i++){
read(x), read(y);
read(a[i]), read(b[i]);
es[i] = {x, y};
e[x].push_back(i);
e[y].push_back(i);
}
dfs(1);
priority_queue<pair<int, int> > pq;
vector<vector<pair<ll, ll> > > hulls;
for(auto vec : groups){
hulls.push_back(gethull(vec));
pq.push({-(int) hulls.back().size(), (int) hulls.size() - 1});
}
while((int) pq.size() > 1){
auto [x, u] = pq.top(); pq.pop();
auto [y, v] = pq.top(); pq.pop();
hulls[u] = merge(hulls[u], hulls[v]);
pq.push({-(int) hulls[u].size(), u});
}
auto[x, u] = pq.top();
ll ans = 7e18;
for(auto [A, B]: hulls[u]){
ans = min(ans, A * B);
}
printf("%lld\n", ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 9944kb
input:
3 3 1 2 0 1000 2 3 0 1000 3 1 1 1
output:
0
result:
ok 1 number(s): "0"
Test #2:
score: 0
Accepted
time: 0ms
memory: 7840kb
input:
5 5 1 2 666 10 2 3 555 1000 3 1 444 345 1 4 222 234 2 5 333 123
output:
1185480
result:
ok 1 number(s): "1185480"
Test #3:
score: 0
Accepted
time: 2ms
memory: 9944kb
input:
5 6 1 3 12316 13729 1 2 171 10309 5 1 47389 7369 2 4 43318 36867 1 4 30728 43835 5 3 24437 31639
output:
6732185824
result:
ok 1 number(s): "6732185824"
Test #4:
score: 0
Accepted
time: 0ms
memory: 9832kb
input:
6 6 5 2 36032 49949 4 5 8963 9741 3 4 4004 35098 4 1 14459 30350 4 6 39612 8568 1 5 8645 16098
output:
11617618224
result:
ok 1 number(s): "11617618224"
Test #5:
score: 0
Accepted
time: 2ms
memory: 9980kb
input:
6 6 6 1 1516 16290 3 5 47834 15228 5 1 14795 44496 2 6 21296 36977 6 3 42659 16631 4 3 9808 31313
output:
13124412318
result:
ok 1 number(s): "13124412318"
Test #6:
score: 0
Accepted
time: 0ms
memory: 7920kb
input:
7 7 1 7 42781 13704 7 3 48779 17985 6 4 27969 24986 4 7 33333 35700 5 7 4859 49960 6 2 23927 33500 6 1 11232 15327
output:
24803495714
result:
ok 1 number(s): "24803495714"
Test #7:
score: 0
Accepted
time: 0ms
memory: 9948kb
input:
10 11 7 3 43798 8096 5 7 36600 4126 2 6 20599 15893 9 6 7541 5621 4 9 22047 10608 5 10 21235 2700 1 10 19260 8862 4 3 22407 37504 5 1 12867 1738 1 8 48480 15236 2 5 43525 13679
output:
19944198324
result:
ok 1 number(s): "19944198324"
Test #8:
score: -100
Wrong Answer
time: 2ms
memory: 9880kb
input:
10 12 10 2 21765 14299 4 2 8316 29600 3 2 36018 33286 4 5 30741 46828 9 7 13354 18461 9 5 11896 14216 6 1 35705 34008 1 9 41496 21860 7 5 37709 34178 8 7 1595 27497 6 9 12139 37471 3 5 43310 12734
output:
43180611760
result:
wrong answer 1st numbers differ - expected: '39767313936', found: '43180611760'