列表

详情


NC24444. [USACO 2016 Ope P]Landscaping

描述

Farmer John is building a nicely-landscaped garden, and needs to move a large amount of dirt in the process.
The garden consists of a sequence of N flowerbeds (1≤N≤100,000), where flowerbed i initially contains Ai units of dirt. Farmer John would like to re-landscape the garden so that each flowerbed i instead contains Bi units of dirt. The Ai's and Bi's are all integers in the range 0…10.

To landscape the garden, Farmer John has several options: he can purchase one unit of dirt and place it in a flowerbed of his choice for X units of money. He can remove one unit of dirt from a flowerbed of his choice and have it shipped away for Y units of money. He can also transport one unit of dirt from flowerbed i to flowerbed j at a cost of Z times |i−j|. Please compute the minimum total cost for Farmer John to complete his landscaping project.

输入描述

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;
} 

上一题