BZOJ 1207: Whack-a-Mole
Problem Statement
1207: [HNOI2004]打鼹鼠
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3975 Solved: 1893
[Submit][Status][Discuss]
Description
鼹鼠是一种很喜欢挖洞的动物,但每过一定的时间,它还是喜欢把头探出到地面上来透透气的。根据这个特点阿Q编写了一个打鼹鼠的游戏:在一个n*n的网格中,在某些时刻鼹鼠会在某一个网格探出头来透透气。你可以控制一个机器人来打鼹鼠,如果i时刻鼹鼠在某个网格中出现,而机器人也处于同一网格的话,那么这个鼹鼠就会被机器人打死。而机器人每一时刻只能够移动一格或停留在原地不动。机器人的移动是指从当前所处的网格移向相邻的网格,即从坐标为(i,j)的网格移向(i-1, j),(i+1, j),(i,j-1),(i,j+1)四个网格,机器人不能走出整个n*n的网格。游戏开始时,你可以自由选定机器人的初始位置。现在你知道在一段时间内,鼹鼠出现的时间和地点,希望你编写一个程序使机器人在这一段时间内打死尽可能多的鼹鼠。
Input
第一行为n(n<=1000), m(m<=10000),其中m表示在这一段时间内出现的鼹鼠的个数,接下来的m行每行有三个数据time,x,y表示有一只鼹鼠在游戏开始后time个时刻,在第x行第y个网格里出现了一只鼹鼠。Time按递增的顺序给出。注意同一时刻可能出现多只鼹鼠,但同一时刻同一地点只可能出现一只鼹鼠。
Output
仅包含一个正整数,表示被打死鼹鼠的最大数目
Sample Input
2 2
1 1 1
2 2 2
1 1 1
2 2 2
Sample Output
1
My Translation
Moles appear on a 2-D grid on certain points at certain times. A robot knows when the moles will emerge and where they will emerge, and starts at the most optimal location possible. The robot can only move one cell up, down, left, or right in one unit of time, but it can stay there to whack a mole. How many moles can it whack?
Limits
Grid size is at most 1000 by 1000
No more than 10,000 moles
Things to Realize
- Instead of mimicking the robot, moving on a 2-D grid (as if it was in depth-first search), store the x-location and the y-location of the mole in a one-dimensional array.
- Because of the above, the grid size is unimportant - 1000 is by far able to fit in an "int" without overflow.
- The moles we whack is a sequence, and we want the longest possible sequence - hmm. LONGEST possible SEQUENCE? We can use LIS!
But for LIS, there's always something that determines whether an element can go next to that element. What is that for this?
In order for, say mole J, to be next to mole I in the whacked sequence, then the distance between I and J has to be no more than their difference in time they appear.
From that point on, it's roughly the same a Longest Increasing Subsequence.
My Code
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
int t[10005], x[10005], y[10005], b[10005];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int _, m; cin >> _ >> m;
for(int i=0; i<m; i++) { cin >> t[i] >> x[i] >> y[i]; b[i] = 1; }
int best = 1;
for(int ind=m-1; ind>=0; ind--)
{
for(int j=ind+1; j<m; j++)
if(abs(x[ind] - x[j]) + abs(y[ind] - y[j]) <= abs(t[ind] - t[j]))
b[ind] = max(b[ind], b[j] + 1);
best = max(best, b[ind]);
}
cout << best << endl;
}
Comments
Post a Comment