이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,v.size()) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<x.F<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define Add(x,y) x=(x+y)%mod
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
#define int ll
const int N=1005,inf=2e9;
int n,m,ans,a[N],b[N],dp[N][N];
bool cmp(pair<int,int> a,pair<int,int> b){
return a.F*b.S<b.F*a.S;
}
int solve(vector<int> A,vector<int> B){
int n=A.size(),m=B.size(),ida=0,idb=0,res=0;
f_(i,n-2,0) minm(A[i],A[i+1]);
f_(i,m-2,0) minm(B[i],B[i+1]);
while(ida+1<n || idb+1<m){
pair<int,int> mn={inf,1};
int b=0;
f(i,ida+1,n) if(cmp({A[i]-A[ida],i-ida},mn)) mn={A[i]-A[ida],i-ida};
f(i,idb+1,m) if(cmp({B[i]-B[idb],i-idb},mn)) b=1;
if(b==0){
ida++;
res+=B[idb];
}
else{
idb++;
res+=A[ida];
}
}
return res;
}
int32_t main(){
ios:: sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin>>n>>m;
if(n>1000 || m>1000) assert(0);
int ida=1,idb=1;
f(i,1,n+1){
cin>>a[i];
if(a[i]<a[ida]) ida=i;
}
f(i,1,m+1){
cin>>b[i];
if(b[i]<b[idb]) idb=i;
}
vector<int> A,B;
f_(i,ida,1) A.pb(a[i]);
f_(i,idb,1) B.pb(b[i]);
ans+=solve(A,B);
A.clear();
B.clear();
f(i,ida,n+1) A.pb(a[i]);
f(i,idb,m+1) B.pb(b[i]);
ans+=solve(A,B);
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |