CDOJ 1047 Alice's birthday

15年校赛A题

题目链接

Alice's birthday


题意

一条长为n的路,Bob要从一个端点走到另一个端点,途中有m个服务站,每个服务站提供两种服务可供选择

1.瞬间向前前进一个单位的距离

2.使Bob走过一个距离的要花的时间减1

问Bob在每个服务站如何选择可以使自己最快到达路的另一端。


input

The first line contains three integers n, m and t, indicating the length of the road, the number of service stations, the time that Bob's car needs to move one unit length initially.
The second line contains m integers pi, indicating the distance from Bob's home to the ith station.
It is guaranteed that 1≤n≤10^9,0≤m≤10^5,1≤t<2^31,1≤pi−1<pi<n;


output

Print the answer in one line.


题解

智障首先想到了DP!费劲心思优化之后得到了一个空间时间都是O(m2)的DP,显然是MLE和TLE一起来

后来发现可以贪心,因为假如要选择服务2,晚选不可能优于早选

接下来枚举选择服务2的次数就好


#include<bits/stdc++.h>

using namespace std;
const int maxn=1e5;
typedef long long LL;

LL dis[maxn+5];

int main(void)
{
	#ifdef ex
	freopen ("in.txt","r",stdin);
	#endif
	
	LL n,m,t;
	scanf("%lld%lld%lld",&n,&m,&t);
	
	for (int i=1;i<=m;++i)
	{
		scanf("%lld",&dis[i]);
	}
	dis[m+1]=n;
	dis[0]=0;
	
	LL sum=t*(n-m);
	LL ans=sum;
	for (int i=1;i<=m;++i)
	{
		LL t1=(n-dis[i]-(m-i+1))*t;
		LL t2=(n-dis[i]-(m-i))*(t-1);
		sum-=(t1-t2);
		--t;
		ans=min(ans,sum);
		if (t==0) break;
	}
	cout<<ans<<endl;
	//cout<<sum<<' '<<ans<<endl;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值