【P2339 [USACO04OPEN]Turning in Homework G】题解

题目链接

先按作业的提交地点排序。

\(dp(l, r, 0/1)\) 为还剩 \([l, r]\) 的作业没交,且下一步交 \(l(0), r(1)\) 的最小步数。

显然:

\[dp(l, r, 0)=\min(\max(dp(l-1, r, 0)+|a_{l-1}-a_l|, \,t_l),\, \max(dp(l, r+1, 1)+|a_{r+1}-a_l|,\, t_l)) \]

\[dp(l, r, 1)=\min(\max(dp(l-1, r, 0)+|a_{l-1}-a_r|,\, t_r),\, \max(dp(l, r+1, 1)+|a_{r+1}-a_r|,\, t_r)) \]

要从大区间向小区间推。

Code

// Problem: P2339 [USACO04OPEN]Turning in Homework G
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P2339
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
//#define M
//#define mo
#define N 1010
struct node
{
	int x, t; 
}d[N]; 
int n, m, i, j, k; 
int c, h, b; 
int dp[N][N][2]; 
int a[N], t[N]; 
int l, r, ans; 

bool cmp(node x, node y)
{
	return x.x<y.x; 
}

signed main()
{
//	freopen("tiaoshi.in", "r", stdin); 
//	freopen("tiaoshi.out", "w", stdout); 
	memset(dp, 0x3f, sizeof(dp)); 
	c=read(); h=read(); b=read(); 
	for(i=1; i<=c; ++i) d[i].x=read(), d[i].t=read(); 
	sort(d+1, d+c+1, cmp); 
	for(i=1; i<=c; ++i) a[i]=d[i].x, t[i]=d[i].t; 
	// for(i=1; i<=c; ++i) printf("%lld %lld\n", a[i], t[i]); 
	dp[1][c][0]=max(a[1], t[1]); 
	dp[1][c][1]=max(a[c], t[c]); 
	// printf("dp[1][%lld][0]=%lld\n", c, dp[1][c][0]); 
	// printf("dp[1][%lld][1]=%lld\n", c, dp[1][c][1]); 
	for(k=c-1; k>=1; --k)
		for(l=1, r=l+k-1; r<=c; ++l, ++r)
		{
			dp[l][r][0]=min(max(dp[l-1][r][0]+abs(a[l-1]-a[l]), t[l]), 
							max(dp[l][r+1][1]+abs(a[r+1]-a[l]), t[l])); 
			dp[l][r][1]=min(max(dp[l-1][r][0]+abs(a[l-1]-a[r]), t[r]),
							max(dp[l][r+1][1]+abs(a[r+1]-a[r]), t[r])); 
			// printf("dp[%lld][%lld][0]=%lld\n", l, r, dp[l][r][0]); 
			// printf("dp[%lld][%lld][1]=%lld\n", l, r, dp[l][r][1]); 
		}
	ans=0x3f3f3f3f3f3f3f3f; 
	for(i=1; i<=c; ++i) ans=min(ans, min(dp[i][i][0], dp[i][i][1])+abs(a[i]-b)); 
	printf("%lld", ans); 
	return 0; 
}

posted @ 2021-11-17 23:06  zhangtingxi  阅读(69)  评论(0编辑  收藏  举报