Title Description: Given the coordinates of the n birds in 2D space, the shooter shoots them, the coordinates of the birds required to shoot are getting bigger and larger, i.e. for the first and the i+1 birds, the requirements are fulfilled: xi<=xi+1 && yi <= yi+1. How many birds can be shot at most.
Idea: Sort all points by x-coordinate, with the same x-coordinate, sorted by y-coordinate. The X-direction can meet the limit, the Y-direction of the longest non-descending sub-sequence can be. Due to the large amount of data, it is necessary to take NLOGN optimization algorithm.
1#include <algorithm>2#include <iostream>3#include <cstdio>4 using namespacestd;5 6 Const intN =100000;7 intS[n];8 inttop;9 Ten structNode One { A intx, y; - } Node[n]; - the BOOLCMP (Node A, Node B) - { - if(a.x! = b.x)returnA.x <b.x; - returnA.y <b.y; + } - + intMain () A { at intT; -scanf"%d", &t); - while(t-- ) - { - intN; -scanf"%d", &n); in for(inti =0; I < n; i++ ) - { toscanf"%d%d", &node[i].x, &node[i].y); + } -Sort (node, node +N, CMP); thetop =0; *s[top++] = node[0].y; $ for(inti =1; I < n; i++ )Panax Notoginseng { - if(Node[i].y >= S[top-1] ) the { +s[top++] =node[i].y; A } the Else + { - intTMP = Upper_bound (s, S + Top, NODE[I].Y)-s; $S[TMP] =node[i].y; $ } - } -printf"%d\n", top); the } - return 0;Wuyi}
The LIS algorithm for NLOGN is not quite understandable and can refer to another article on my blog.
Nlogn solution of Toj 4071 longest non-descending sub-sequence