Question: Ultraviolet A-10599 robots (II) (LIS)
A matrix of N * m, some of the above grids have garbage. Now, a robot is required to start from a grid of 1, 1, and go to the right or down to reach the grids of N * m, collect the most garbage along the way. Now, we need to output the maximum number of garbage entries that can be cleared and the number of such routes to be cleared. Then, we need to output the one with the smallest Lexicographic Order.
Solution: At first, I thought it was a simple DP. The result output showed that there were a lot more paths, so I found that there was a problem with understanding the meaning of the question. It only calculated the difference of garbage, you do not need to consider which grid to use. Because it is required to go from to N * m, and can only go to the right or bottom. In this case, the garbage numbers found along the way must increase progressively, in this way, the problem is converted to finding the longest incremental sequence in a series of spam, but here the incremental sequence is not enough, because it cannot go to the left, therefore, you have to determine whether there is a column greater than or equal to the previous column. Because it will eventually reach N * m, so manually add garbage at this point. If there is no garbage at this point, do not add it. The final record shows which one should be selected before the I garbage to make Lis the longest. DP [I] = DP [k] + 0 | 1 (check whether there is garbage in this location );
Code:
# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; const int n = 105; const int maxn = 10010; int rec [N] [N]; int f [maxn], num [maxn], s [maxn], Y [maxn], d [maxn], p [maxn]; int n, m; void Init (Int & CNT) {CNT = 0; For (INT I = 1; I <= N; I ++) // sort the garbage from small to large for (Int J = 1; j <= m; j ++) {If (REC [I] [J] | (I = N & J = m) {s [CNT] = (I-1) * m + J; y [CNT] = J; d [CNT ++] = rec [I] [J] ;}} void Printf_ans (int cnt) {If (CNT =-1) return; printf_ans (P [CNT]); If (d [CNT]) printf ("% d ", s [CNT]);} int main () {int A, B, CNT, CAS = 0; while (scanf ("% d", & N, & M) & n! =-1 & M! =-1) {memset (REC, 0, sizeof (REC); While (scanf ("% d", & A, & B), A & B) {rec [a] [B] = 1;} Init (CNT); For (INT I = 0; I <CNT; I ++) {f [I] = 0; // select the maximum number of spam that can be picked up by the I-th garbage collection (the number of spam points has not been added) num [I] = 1; // select the maximum number of spam routes obtained from the first spam. P [I] =-1 ;}for (INT I = 0; I <CNT; I ++) {for (Int J = I-1; j> = 0; j --) {// to ensure the minimum Lexicographic Order if (Y [J] <= Y [I]) {// column coordinate if (F [J]> F [I]) {f [I] = f [J]; P [I] = J; num [I] = num [J];} else if (F [J] = f [I]) {num [I] + = num [J]; P [I] = J ;}}f [I] + = d [I]; // Add the garbage count} printf ("case # % d: % d ", ++ cas, F [CNT-1], num [CNT-1]); printf_ans (CNT-1 ); printf ("\ n");} return 0 ;}
Uvs-10599 robots (II) (LIS)