Luogu P2339 [USACO04OPEN]Turning in Homework G

题目描述

https://www.luogu.com.cn/problem/P2339

Solution

首先能够发现没有提交的作业一定是一个区间

我们令f[i][j][0/1]表示区间[i,j]的作业还没有交,0/1表示现在位于左端点还是右端点

时间复杂度 $O(n^2)$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define maxn 1010
#define cn const node
#define INF 1000000000
using namespace std;

int n, m, t;

struct node {
int p, t;

friend bool operator < (cn &u, cn &v) { return u.p < v.p; }
} a[maxn];

int f[maxn][maxn][2];

int main() {
cin >> n >> m >> t;
for (int i = 1; i <= n; ++i) cin >> a[i].p >> a[i].t;
sort(a + 1, a + n +1); memset(f, 10, sizeof f);
f[1][n][0] = max(a[1].p, a[1].t); f[1][n][1] = max(a[n].p, a[n].t);
for (int l = n - 1; l; --l)
for (int i = 1; i + l - 1 <= n; ++i) {
int j = i + l - 1;
f[i][j][0] = min(max(f[i - 1][j][0] + a[i].p - a[i - 1].p, a[i].t),
max(f[i][j + 1][1] + a[j + 1].p - a[i].p, a[i].t));
f[i][j][1] = min(max(f[i - 1][j][0] + a[j].p - a[i - 1].p, a[j].t),
max(f[i][j + 1][1] + a[j + 1].p - a[j].p, a[j].t));
}
int ans = INF;
for (int i = 1; i <= n; ++i) ans = min(ans, f[i][i][0] + abs(a[i].p - t));
cout << ans << endl;
return 0;
}