-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path1517.cpp
59 lines (50 loc) · 1.25 KB
/
1517.cpp
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
dynamic programming > longest increasing subsequence (LIS)
difficulty: medium
date: 23/Jan/2020
by: @brpapa
*/
#include <iostream>
#include <cmath>
#include <vector>
#define INF (int)0x7f7f7f7f
using namespace std;
struct T {
int x, y, t;
T() {}
T(int x, int y, int t) : x(x), y(y), t(t) {}
};
vector<T> v;
vector<int> memo;
int lis(int j) {
int &ans = memo[j];
if (ans != -1) return ans;
if (j == 0) return 0;
ans = -INF; // pois, se não conter a posição inicial v[0], a lis é inválida
for (int i = 0; i < j; i++)
if (v[j].t - v[i].t >= max(abs(v[i].x-v[j].x), abs(v[i].y-v[j].y)))
// tempo é suficiente p se deslocar de v[i] à v[j]
ans = max(ans, 1 + lis(i));
return ans;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
while (true) {
int N, M, K; cin >> N >> M >> K;
if (!N && !M && !K) break;
v.resize(K+1);
memo.assign(K+1, -1);
int x, y, t;
for (int i = 1; i < v.size(); i++) {
cin >> x >> y >> t;
v[i] = T(x, y, t);
}
cin >> x >> y;
v[0] = T(x, y, 0);
int ans = 0;
for (int i = 0; i < v.size(); i++)
ans = max(ans, lis(i));
cout << ans << endl;
}
return 0;
}