【题解】[USACO] 照片Photo

【题解】[USACO] 照片Photo


【题目大意】

在\(1\)~\(N\)的序列上有\(M\)个区间,使得这\(M\)个小区间每个覆盖了且仅覆盖了一个点,求最多点数,如果无解,输出\(-1\)。

【分析】

刚开始做的时候我是懵的。。。不管三七二十一开始想差分约束系统,无奈没那么好的思维,没想出来。。。
一翻题解,,\(WOC!!!\)这是什么神仙\(DP?\)真的是学到了。。。

我们设\(f[i]\)表示序列的第\(i\)位是一个点时,序列\(1\)~\(i\)的最多点数。

那么\(dp\)方程就很好推了:设\(j\)<\(i\),即有\(f[i]=max(f[j]+1, f[i])\)。

现在我们需要解决的是\(j\)的范围,也就是\(f[i]\)能从哪些状态中转移过来。

我们想,如果第\(i\)位是一个点,那么所有包含这个点的区间都不能再有点,也就是说,\(j\)必须小于包含第\(i\)位这个点的区间中最小的左端点
同样的,每个区间上必须有一个点,所以,\(j\)必须大于或等于不包含第\(i\)位这个点的区间中最大的左端点

至于怎么求,大家可以\(YY\)了。

我们会惊奇地发现\(DP\)方程满足单调性,于是单调队列走起。

细节见代码。

至于差分约束的做法嘛,,,以后会补的,,,咕咕咕

【code】

//#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("O2")
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX = 2000000 + 5;
inline int read(){
    int f = 1, x = 0;char ch;
    do { ch = getchar(); if (ch == '-') f = -1; } while (ch < '0'||ch>'9');
    do {x = x*10+ch-'0'; ch = getchar(); } while (ch >= '0' && ch <= '9');
    return f*x;
}
int n, m, l[MAX], r[MAX], f[MAX], ans, q[MAX];
int main(){
//  freopen("aaa.in", "r", stdin);
//  freopen("aaa.out", "w", stdout);
    int n = read(), m = read();
    for (int i = 1;i <= n + 1; ++i) r[i] = i - 1;
    for (int i = 1;i <= m; ++i) {
        int x = read(), y = read();
        r[y] = min(r[y], x - 1);
        l[y + 1] = max(l[y + 1], x);
    }
    for (int i = n; i > 0; --i) r[i] = min(r[i], r[i + 1]);
    for (int i = 2; i < n + 2; ++i) l[i] = max(l[i], l[i - 1]);
    int j = 1, h = 1, t = 1; q[1] = 0;
    for (int i = 1;i <= n + 1; ++i) {
        while (j <= r[i] && j <= n) {
            if (f[j] == -1) {
                ++j;
                continue;
            }
            while (f[j] > f[q[t]] && h <= t) --t;
            q[++t] = j;
            ++j;
        }
        while (q[h] < l[i] && h <= t) ++h;
        if (h <= t) f[i] = f[q[h]] + (i != n+1 ? 1 : 0);
        else f[i] = -1;
    }
    printf("%d", f[n + 1]);
    return 0;
}

原文地址:https://www.cnblogs.com/silentEAG/p/10804519.html

时间: 2024-11-11 10:26:17

【题解】[USACO] 照片Photo的相关文章

[USACO13OPEN]照片Photo [动态规划 单调队列]

[USACO13OPEN]照片Photo 这题好烧脑... #include<bits/stdc++.h> using namespace std; #define ll long long #define rg register #define Max(x,y) ((x)>(y)?(x):(y)) #define Min(x,y) ((x)>(y)?(y):(x)) const int N=2e5+5,M=1e5+5,inf=0x3f3f3f3f,P=19650827; int

[USACO13OPEN]照片Photo

题目描述 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 r

P3084 [USACO13OPEN]照片Photo (dp+单调队列优化)

题目链接:传送门 题目: 题目描述 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

【DP专题】——[USACO13OPEN]照片Photo

这道题是道非常好的动规题,不难但思考过程受益很大. [9/25日更新:鸽了俩个月回来补档了] 题目描述 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 <

[题解]USACO 1.3 Ski Course Design

Ski Course Design Farmer John has N hills on his farm (1 <= N <= 1,000), each with an integer elevation in the range 0 .. 100. In the winter, since there is abundant snow on these hills, FJ routinely operates a ski training camp. Unfortunately, FJ h

[题解]USACO 1.3 Wormholes

Wormholes Farmer John's hobby of conducting high-energy physics experiments on weekends has backfired, causing N wormholes (2 <= N <= 12, N even) to materialize on his farm, each located at a distinct point on the 2D map of his farm (the x,y coordin

P3084 [USACO13OPEN]照片Photo

题目描述 农夫约翰决定给站在一条线上的N(1 <= N <= 200,000)头奶牛制作一张全家福照片,N头奶牛编号1到N. 于是约翰拍摄了M(1 <= M <= 100,000)张照片,每张照片都覆盖了连续一段奶牛:第i张照片中包含了编号a_i 到 b_i的奶牛.但是这些照片不一定把每一只奶牛都拍了进去. 在拍完照片后,约翰发现了一个有趣的事情:每张照片中都有一只身上带有斑点的奶牛.约翰意识到他的牛群中有一些斑点奶牛,但他从来没有统计过它们的数量. 根据照片,请你帮约翰估算在他的

PAT甲题题解-1109. Group Photo (25)-(模拟拍照排队)

题意:n个人,要拍成k行排队,每行 n/k人,多余的都在最后一排. 从第一排到最后一排个子是逐渐增高的,即后一排最低的个子要>=前一排的所有人 每排排列规则如下: 1.中间m/2+1为该排最高: 2.其他人按各自降序顺序,轮流排到中间最高的左边和右边: 举个例子 190 188 186 175 170 - - 190 - - - 188 190 - - - 188 190 186 - 175 188 190 186 - 175 188 190 186 170 3.当个子一样高时,名字按字典序顺序

Luugu 3084 [USACO13OPEN]照片Photo

很神仙的dp...假装自己看懂了,以后回来复习复习... 设$f_{i}$表示从$1$到$i$,且$i$这个点必放的最小代价. 一个区间有两个限制条件:至少放一个,至多放一个. 因为一个区间至多要放一个,所以所有包含这个点的区间都不能再放,设$r_{i}$表示包含这个点的区间中最小的左端点$ - 1$. 因为一个区间至少要放一个,所以不能有区间中一个都不放,设$l_{i}$表示整个区间在当前点之前的最大的左端点. 这样子就有了转移方程:$f_{i} = max(f_{j}) + 1$  $(l_