Question link: Portal
Question:
Description: Farmer John has decided to assemble a panoramic photo of a lineup of his n cows (1 <= n <= 200,000), which, as always, are conveniently numbered from 1 .. n. accordingly, he snapped M (1 <= m <= 100,000) photos, each covering a contiguous range of cows: photo I contains cows a_ I through B _ I inclusive. the photos collectively may not necessarily cover every single cow. after taking his photos, FJ n Otices a very interesting phenomenon: each photo he took contains exactly one cow with spots! FJ was aware that he had some number of spotted cows in his herd, but he had never actually counted them. based on his photos, please determine the maximum possible number of spotted cows that cocould exist in his herd. output-1 if there is no possible assignment of spots to cows consistent with FJ's photographic results. input/Output Format: * Line 1: two integers n and M. * lines 2 .. m + 1: line I + 1 contains A_ I and B _ I. output Format: * Line 1: the maximum possible number of spotted cows on FJ's farm, or-1 if there is no possible solution. input and Output sample input sample #3 1 4 2 5 3 4 output sample # description there are 5 cows and 3 photos. the first photo contains cows 1 through 4, etc. from the last photo, we know that either cow 3 or cow 4 must be spotted. by choosing either of these, we satisfy the first two photos as well.
View code
Ideas:
If you want to place the ox in the position I, the former ox should be placed between [Li, Ri], and the Li and RI can be transferred according to the input.
When reading X, Y, update Ly + 1 with X, update ry with X-1.
After reading, scan from the back, update Li with li-1; then scan from the back, update RI with RI + 1.
Then you can run DP, F [I] = max {f [J] | Li ≤ j ≤ Ri}
Status:
F [I] indicates the maximum number of cows placed at the I position.
State transition equation:
F [I] = max {f [J] | Li ≤ j ≤ Ri}
#include <bits/stdc++.h>using namespace std;const int MAX_N = 2e5 + 5;#define tomax(a, b) a = a>b?a:b#define tomin(a, b) a = a<b?a:bint N, M, l[MAX_N], r[MAX_N];int f[MAX_N];int main(){// freopen("testdata.in", "r", stdin); cin >> N >> M; for (int i = 1; i <= N+1; i++) r[i] = i-1; for (int i = 1; i <= M; i++) { int x, y; scanf("%d%d", &x, &y); tomin(r[y], x-1); tomax(l[y+1], x); } for (int i = 2; i <= N+1; i++) tomax(l[i], l[i-1]); for (int i = N; i >= 1; i--) tomin(r[i], r[i+1]); memset(f, -1, sizeof f); f[0] = 0; for (int i = 1; i <= N+1; i++) for (int j = l[i]; j <= r[i]; j++) if(f[j] != -1) tomax(f[i], f[j] + (i!=N+1 ? 1 : 0)); cout << f[N+1] << endl; return 0;}/*5 31 42 41 1*/
View code
It was originally a glance at the problem, and after understanding the ideas, the preparation was not optimized.ViolenceT, the result is directly AC, returnHow fast is a thief?-. =
However, this write should be able to be stuck by the big data of two cows:
200000 21 100000100001 200000
View code
OfferMonotonous queue OptimizationPositive Solution:
#include <bits/stdc++.h>using namespace std;const int MAX_N = 2e5 + 5;#define tomax(a, b) a = a>b?a:b#define tomin(a, b) a = a<b?a:bint N, M, l[MAX_N], r[MAX_N];int h, t, q[MAX_N], f[MAX_N];int main(){ cin >> N >> M; memset(f, 0, sizeof f); for (int i = 1; i <= N+1; i++) r[i] = i-1; for (int i = 1; i <= M; i++) { int x, y; scanf("%d%d", &x, &y); tomin(r[y], x-1); tomax(l[y+1], x); } for (int i = 2; i <= N+1; i++) tomax(l[i], l[i-1]); for (int i = N; i >= 1; i--) tomin(r[i], r[i+1]); int j = 1; h = 1, t = 0, q[++t] = 0; for (int i = 1; i <= N+1; i++) { while (j <= N && j <= r[i]) { if (f[j] == -1) { ++j; continue; } while (h <= t && f[q[t]] <= f[j]) --t; q[++t] = j; ++j; } while (h <= t && q[h] < l[i]) ++h; if (h <= t) f[i] = f[q[h]] + (i!=N+1 ? 1 : 0); else f[i] = -1; } cout << f[N+1] << endl; return 0;}
View code
P3084 [usaco 13open] Photo photo (DP + monotonous queue optimization)