P3084 [USACO13OPEN]照片Photo dp
题意:
有n个区间,每个区间只能有一个斑点奶牛,问最多有几个斑点奶牛。
思路:
首先要处理出每个点的$L[i], R[i]$。
$L[i]$表示$L[i]~i-1$之间一定有一个点。i也是选中的。
$R[i]$表示$R[i]~i$之间只能有i这个点是选中的。
通过处理出$L[i]$和$R[i]$,$dp[i]$ = 最大的$L[i]$到$R[i]$间$dp值 + 1$。这里用线段树优化,也可以用优先队列。
#include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <stack> #include <cmath> #include <queue> #include <list> #include <map> #include <set> #include <cassert> using namespace std; #define lson (l, mid, rt << 1) #define rson (mid + 1, r, rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << "\n"; #define pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; //typedef __int128 bll; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef pair<int, pii> p3; typedef pair<ll, int> pli; //priority_queue<int> q;//这是一个大根堆q //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q #define fi first #define se second //#define endl '\n' #define OKC \ ios::sync_with_stdio(false); \ cin.tie(0) #define FT(A, B, C) for (int A = B; A <= C; ++A) //用来压行 #define REP(i, j, k) for (int i = j; i < k; ++i) #define max3(a, b, c) max(max(a, b), c); #define min3(a, b, c) min(min(a, b), c); //priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e9 + 9; const double esp = 1e-8; const double PI = acos(-1.0); const double PHI = 0.61803399; //黄金分割点 const double tPHI = 0.38196601; template <typename T> inline T read(T &x) { x = 0; int f = 0; char ch = getchar(); while (ch < '0' || ch > '9') f |= (ch == '-'), ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x = f ? -x : x; } /*-----------------------showtime----------------------*/ const int maxn = 200009; int R[maxn], L[maxn]; int mx[maxn * 4]; int dp[maxn]; int query(int L, int R, int l, int r, int rt) { if (l >= L && r <= R) { return mx[rt]; } int mid = (l + r) >> 1; int x = -inf; if (mid >= L) x = max(x, query(L, R, l, mid, rt << 1)); if (mid < R) x = max(x, query(L, R, mid + 1, r, rt << 1 | 1)); return x; } void update(int p, int c, int l, int r, int rt) { if (l == r) { mx[rt] = c; return; } int mid = (l + r) >> 1; if (mid >= p) update(p, c, l, mid, rt << 1); else update(p, c, mid + 1, r, rt << 1 | 1); mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]); } int main() { int n, m; scanf("%d%d", &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); L[y + 1] = max(L[y + 1], x); R[y] = min(R[y], x - 1); } memset(mx, -inf, sizeof(mx)); for (int i = 1; i <= n + 1; i++) L[i] = max(L[i - 1], L[i]); for (int i = n; i >= 1; i--) R[i] = min(R[i + 1], R[i]); update(1, 0, 1, n + 2, 1); for (int i = 1; i <= n + 1; i++) { if (L[i] <= R[i]) dp[i] = query(L[i] + 1, R[i] + 1, 1, n + 2, 1) + 1; else dp[i] = -inf; update(i + 1, dp[i], 1, n + 2, 1); } // for(int i=1; i<=n+1; i++){ // cout<<L[i]<<" -- "<<R[i]<<endl; // } if (dp[n + 1] <= 1) puts("-1"); else printf("%d\n", dp[n + 1] - 1); return 0; }
skr