[BZOJ1207] [HNOI2004] hit Mole (DP)

Source: Internet
Author: User

Description

The mole is a kind of animal that likes digging a hole very much, but every time, it still likes to poke his head out to the ground to breathe. According to this feature, Ah Q wrote a game of hitting the mole: in a n*n grid, at some point the mole would try to breathe through a grid. You can control a robot to hit a mole, and if I moment the mole appears in a grid and the robot is in the same grid, the mole will be killed by the robot. And the robot can only move one cell at a time or stay where it is. The movement of a robot is to move from the current grid to the adjacent mesh, that is, from the coordinates (I,J) of the grid (I-1, J), (I+1, J), (I,j-1), (i,j+1) Four meshes, the robot cannot walk out of the entire n*n grid. At the beginning of the game, you are free to select the initial position of the robot. Now you know the time and place where the mole appeared during a period of time, hoping you would write a program that would allow the robot to kill as many moles in this period of time.

Input

The first behavior n (n<=1000), M (m<=10000), where m represents the number of moles appearing in this period of time, and the next m row has three data per row time,x,y indicates that there is a mole at the beginning of the game time, A mole appeared in the grid of the first X row. Time is given in ascending order. Note that there may be many moles at the same time, but only one mole may appear at the same time in the same place.

Output

Contains only a positive integer representing the maximum number of moles killed

Sample Input2 2
1 1 1
2 2 2
Sample Output1HINTSourceSolution

Using a method like Lis, F[i] said that if I were to kill the Gopher, the total number of mice killed.

O (n^2) water over. There seems to be an O (NLOGN) approach but it doesn't.

1#include <bits/stdc++.h>2 using namespacestd;3 intt[10005], x[10005], y[10005], f[10005];4  5 intDisintIintj)6 {7     returnABS (X[i]-x[j]) + ABS (Y[i]-y[j]);8 }9  Ten intMain () One { A     intN, M; -CIN >> N >>m; -      for(inti =1; I <= m; i++) theCIN >> T[i] >> X[i] >>Y[i]; -      for(inti =1; I <= m; i++) -     { -F[i] =1; +          for(intj =1; J < I; J + +) -             if(Dis (i, j) <= ABS (T[i]-T[j])) +F[i] = max (F[i], f[j] +1); A     } atcout << *max_element (f +1, F + M +1) <<Endl; -     return 0; -}
View Code

[BZOJ1207] [HNOI2004] hit Mole (DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.