NC24444. [USACO 2016 Ope P]Landscaping
描述
输入描述
The first line of input contains N, X, Y, and Z (0≤X,Y≤108;0≤Z≤1000). Line i+1 contains the integers Ai and Bi.
输出描述
Please print the minimum total cost FJ needs to spend on landscaping.
示例1
输入:
4 100 200 1 1 4 2 3 3 2 4 0
输出:
210
说明:
Note that this problem has been asked in a previous USACO contest, at the silver level; however, the limits in the present version have been raised considerably, so one should not expect many points from the solution to the previous, easier version.C++14(g++5.4) 解法, 执行用时: 53ms, 内存消耗: 1476K, 提交时间: 2020-04-26 10:48:37
#include<bits/stdc++.h> #define Ll long long using namespace std; const Ll N=1e5+5; Ll n,x,y,ans,m1,m2,z; priority_queue<Ll>Q1,Q2; int main() { scanf("%lld%lld%lld%lld",&n,&x,&y,&z); for(Ll j=1;j<=n;j++){ scanf("%lld%lld",&m1,&m2); if(m1<m2) for(Ll i=1;i<=m2-m1;i++) if(Q1.empty()||j*z-Q1.top()>=x){ans+=x;Q2.push(x+j*z);} else{Ll v=Q1.top();Q1.pop();ans+=j*z-v;Q2.push(j*z*2-v);} else for(Ll i=1;i<=m1-m2;i++) if(Q2.empty()||j*z-Q2.top()>=y){ans+=y;Q1.push(y+j*z);} else{Ll v=Q2.top();Q2.pop();ans+=j*z-v;Q1.push(j*z*2-v);} } printf("%lld",ans); }
C++(clang++11) 解法, 执行用时: 52ms, 内存消耗: 1656K, 提交时间: 2021-04-01 20:25:14
#include<iostream> #include<queue> #define ll long long using namespace std; int n,x,y,z; ll p[100005]; priority_queue<ll,vector<ll> >p1,p2; ll ans=0; int main(){ int a,b; cin>>n>>x>>y>>z; for(int i=1;i<=n;i++){ cin>>a>>b; if(a==b) continue; else if(a<b){ for(int j=1;j<=b-a;j++){ p[i]=x; if(p1.size()){ p[i]=min(p[i],i*z-p1.top()); p1.pop(); } ans+=p[i]; p2.push(p[i]+i*z); } } else if(a>b){ for(int j=1;j<=a-b;j++){ p[i]=y; if(p2.size()){ p[i]=min(p[i],i*z-p2.top()); p2.pop(); } ans+=p[i]; p1.push(p[i]+i*z); } } } cout<<ans<<endl; return 0; }