USACO 2018 FEBURARY CONTEST :SILVER T1

USACO 2018 February Contest, Silver

Problem 1. Rest Stops

Farmer John and his personal trainer Bessie are hiking up Mount Vancowver. For their purposes (and yours), the mountain can be represented as a long straight trail of length LL meters (1≤L≤1061≤L≤106). Farmer John will hike the trail at a constant travel rate of rFrF seconds per meter (1≤rF≤1061≤rF≤106). Since he is working on his stamina, he will not take any rest stops along the way.

Bessie, however, is allowed to take rest stops, where she might find some tasty grass. Of course, she cannot stop just anywhere! There are NN rest stops along the trail (1≤N≤1051≤N≤105); the ii-th stop is xixi meters from the start of the trail (0<xi<L0<xi<L) and has a tastiness value cici (1≤ci≤1061≤ci≤106). If Bessie rests at stop ii for tt seconds, she receives ci⋅tci⋅t tastiness units.

When not at a rest stop, Bessie will be hiking at a fixed travel rate of rBrB seconds per meter (1≤rB≤1061≤rB≤106). Since Bessie is young and fit, rBrB is strictly less than rFrF.

Bessie would like to maximize her consumption of tasty grass. But she is worried about Farmer John; she thinks that if at any point along the hike she is behind Farmer John on the trail, he might lose all motivation to continue!

Help Bessie find the maximum total tastiness units she can obtain while making sure that Farmer John completes the hike.

INPUT FORMAT (file reststops.in):

The first line of input contains four integers: LL, NN, rFrF, and rBrB. The next NN lines describe the rest stops. For each ii between 11and NN, the i+1i+1-st line contains two integers xixi and cici, describing the position of the ii-th rest stop and the tastiness of the grass there.

It is guaranteed that rF>rBrF>rB, and 0<x1<⋯<xN<L0<x1<⋯<xN<L. Note that rFrF and rBrB are given in seconds per meter!

OUTPUT FORMAT (file reststops.out):

A single integer: the maximum total tastiness units Bessie can obtain.

SAMPLE INPUT:

10 2 4 3
7 2
8 1

SAMPLE OUTPUT:

15

In this example, it is optimal for Bessie to stop for 77 seconds at the x=7x=7 rest stop (acquiring 1414 tastiness units) and then stop for an additional 11 second at the x=8x=8 rest stop (acquiring 11 more tastiness unit, for a total of 1515 tastiness units).

Problem credits: Dhruv Rohatgi

官方翻译:

Farmer John和他的私人教练Bessie正在徒步攀登温哥牛山。基于他们的目的(也是你的目的),这座山可以用一条长为LL米(1≤L≤1061≤L≤106)的长直路径表示。Farmer John会沿着这条路径以每米rFrF秒(1≤rF≤1061≤rF≤106)的固定速度攀登。由于他正在训练他的耐力,他在途中不会进行任何的休息。

然而Bessie可以在休息站休息,在那里她能够找到一些美味的嫩草。当然,她也不能在任何地方都休息!在路径上总共有NN个休息站(1≤N≤1051≤N≤105);第ii个休息站距离路径的起点xixi米(0<xi<L0<xi<L),美味值为cici(1≤ci≤1061≤ci≤106)。如果Bessie在休息站ii休息了tt秒,她能够得到ci⋅tci⋅t个美味单位。

不在休息站的时候,Bessie会以每米rBrB秒(1≤rB≤1061≤rB≤106)的固定速度攀登。由于Bessie年轻而健康,rBrB严格小于rFrF。

Bessie想要吃到最多的美味嫩草。然而她也担心Farmer John;她认为如果在任何时候她位于Farmer John身后,Farmer John可能就会失去前进的动力了!

帮助Bessie求出,在确保Farmer John能够完成登山的情况下,她能够获得的最多的美味单位。

输入格式(文件名:reststops.in):

输入的第一行包含四个整数:LL,NN,rFrF,以及rBrB。下面NN行描述了休息站。对于11至NN之间的每一个ii,第i+1i+1行包含了两个整数xixi和cici,描述了第ii个休息站的位置和那里的草的美味值。

输入保证rF>rBrF>rB,并且0<x1<⋯<xN<L0<x1<⋯<xN<L。注意rFrF和rBrB的单位为秒每米!

输出格式(文件名:reststops.out):

输出一个整数:Bessie可以获得的最多的美味单位。

输入样例:

10 2 4 3
7 2
8 1

输出样例:

15

在这个样例中,Bessie的最佳方案是在位于x=7x=7的休息站停留77秒(获得1414个美味单位),再在位于x=8x=8的休息站停留11秒(再获得11个美味单位,总共是1515个美味单位)。

供题:Dhruv Rohatgi

具体思路:

奶牛走到当前位置后面所有站中价值最大的站台时,就一直吃草,等人来后,继续向前走,重复上述步骤(很容易想到此思路)

注意:速度单位为秒每米,所以以米为循环变量,到达L时停止

最难处理的是怎样求当前位置后的站台中最大的那个位置,如果边走边求,n log n显然超时

所以必须先预处理出来,我们定义一个结构体,里面放了站台的位置,价值和后面中最大价值站台的升序号

用sort以位置为关键字排序,倒叙查找,定义一个maxcmp表示当前站台至最后站台中最大价值站台的序号,不断更新,存到之前站台的next中。

别忘了long long !!!

usaco提交格式需要用noip格式,下面代码中注释掉了:

#include<stdio.h>
#include<algorithm>
using namespace std;
struct edge{
	int pos,v,next;
}a[100100];
bool cmp(edge a,edge b)
{
	return a.pos<b.pos;
}
long long int ans,l,n,va,vb;
int main()
{
	freopen("reststops.in","r",stdin);
	freopen("reststops.out","w",stdout);
	scanf("%lld%lld%lld%lld",&l,&n,&va,&vb);
	for(int i=1;i<=n;i++)
		scanf("%lld%lld",&a[i].pos,&a[i].v);
	sort(a+1,a+n+1,cmp);
	int maxcmp=0-1;
	for(int i=n;i>=1;i--)
	{
		a[i].next=maxcmp;
		if(a[maxcmp].v<=a[i].v)maxcmp=i;
	}
	long long int t1=0,t2=0,now=maxcmp;
	for(int i=1;i<=l;i++)
	{
		t1+=va;
		t2+=vb;
		if(i==a[now].pos)
		{
			ans+=(t1-t2)*a[now].v;
			t1=t2;
			now=a[now].next;
		}
	}
	printf("%lld",ans);
	fclose(stdin);
	fclose(stdout);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/BR66666/article/details/86610011
今日推荐