# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
570675 | AGE | Simfonija (COCI19_simfonija) | C++14 | 471 ms | 3084 KiB |
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 F first
#define S second
#define pb push_back
using namespace std;
const int N=1e6,M=2e3;
int a[100005],b[100005];
main()
{
int n,m;
cin>>n>>m;
map<int,int>mp;
for(int i=0;i<n;i++)
cin>>a[i];
for(int i=0;i<n;i++)
cin>>b[i];
for(int i=0;i<n;i++)
mp[a[i]-b[i]]++;
int neg=0,pos=0;
for(auto x:mp){
if(x.F<0)
neg+=x.S;
else pos+=x.S;
}
int ans=0,anss=1e18;
for(int i=0;i<n;i++)
ans+=abs(a[i]-b[i]);
anss=min(abs(ans),anss);
for(int j=-3e6;j>=1;j--){
ans-=pos;
ans+=neg;
anss=min(anss,abs(ans));
neg+=mp[-j];
pos-=mp[-j];
if(mp[-j]==0)
mp.erase(-j);
}
ans=0,pos=0,neg=0;
for(int i=0;i<n;i++)
ans+=abs(a[i]-b[i]);
for(auto x:mp)
if(x.F<0)
neg+=x.S;
else pos+=x.S;
for(int j=1;j<=3e6;j++){
ans-=neg;
ans+=pos;
anss=min(anss,abs(ans));
neg-=mp[-j];
pos+=mp[-j];
if(mp[-j]==0)
mp.erase(-j);
}
cout<<anss<<endl;
return 0;
}
Compilation message (stderr)
# | 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... |
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |