This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
using namespace std;
typedef pair<int,int> ii;
typedef pair<long double, ii> iii;
int n,m;
int a[100005], b[100005];
set<int> x,y;
priority_queue<iii, vector<iii>, greater<iii> > pq1, pq2;
main(){
scanf("%lld%lld",&n,&m);
for (int i = 0; i < n; i++){
scanf("%lld",&a[i]);
}
for (int j = 0; j < m; j++){
scanf("%lld",&b[j]);
}
for (int i = 0; i < n; i++){
x.insert(i);
}
for (int j = 0; j < m; j++){
y.insert(j);
}
for (int i = 0; i < n-1; i++){
pq1.push({a[i]-a[i+1],{i,i+1}});
}
for (int j = 0; j < m-1; j++){
pq2.push({b[j]-b[j+1],{j,j+1}});
}
int ans = 0;
while (pq1.size() || pq2.size()){
if (pq2.empty() || (!pq1.empty() && pq1.top().fi < pq2.top().fi)){
iii cur = pq1.top(); pq1.pop();
int l = cur.se.fi, r = cur.se.se;
if (x.find(l) == x.end() || x.find(r) == x.end()) continue;
x.erase(r);
auto it = x.lower_bound(r);
if (it == x.end()){
///printf("adding H edge %d->%d, length %d and cost %d\n",l,r,r-l,b[*y.rbegin()]);
ans += b[*y.rbegin()]*(r-l);
}
else{
int nr = *it;
long double nd = (long double)(a[l]-a[nr])/(long double)(nr-l);
pq1.push({nd, {l,nr}});
}
}
else {
iii cur = pq2.top(); pq2.pop();
int l = cur.se.fi, r = cur.se.se;
if (y.find(l) == y.end() || y.find(r) == y.end()) continue;
y.erase(r);
auto it = y.lower_bound(r);
if (it == y.end()){
///printf("adding W edge %d->%d, length %d and cost %d\n",l,r,r-l,a[*x.rbegin()]);
ans += a[*x.rbegin()]*(r-l);
}
else{
int nr = *it;
long double nd = (long double)(b[l]-b[nr])/(long double)(nr-l);
pq2.push({nd, {l,nr}});
}
}
}
printf("%lld",ans);
}
Compilation message (stderr)
kyoto.cpp:12:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
12 | main(){
| ^~~~
kyoto.cpp: In function 'int main()':
kyoto.cpp:13:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | scanf("%lld%lld",&n,&m);
| ~~~~~^~~~~~~~~~~~~~~~~~
kyoto.cpp:15:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | scanf("%lld",&a[i]);
| ~~~~~^~~~~~~~~~~~~~
kyoto.cpp:18:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | scanf("%lld",&b[j]);
| ~~~~~^~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |