P1545 [USACO04DEC] Dividing the Path G 题解

最近开始快刷蓝紫黑了,做完会写题解交上来。

题目传送门

题意

一条长为 \(L(1 \le L \le 10^6 , 2 | L)\) 的线段上,给出 \(N(1 \le N \le 10^3)\) 个可能相交的子段 \([S_i,E_i]\),现要求用若干个长度在 \([2a,2b]\) 中且为偶数的小线段覆盖整个大线段。

要求:

  • 整个大线段都要被覆盖。
  • 用来覆盖的小线段不相交。
  • 每个子段能且仅能被唯一一个小线段覆盖。
  • 小线段的范围不能超出大线段。

求出最少用来覆盖的小线段的个数。

分析

这道题暴力 \(O(L^2)\) 是好写的,设 \(dp_i\) 表示覆盖 \([0,i]\) 所需的小线段个数(显然 \(i\) 必须为某个子段的端点),答案明显为 \(dp_L\) ,边界也是显而易见的:\(dp_0=0\) 。

转移有如下式子:

\[dp_i=\min_{j=i-2b,2|j}^{i-2a}(dp_j+1)
\]

然后交了一发就过了。

虽然数据较水,但我们仍需要考虑正解。

转移方程中,取最小值里的 \(+1\) 可以提出来,所以我们实际上是在求 \(\min dp_j\) 。

我们其实会很容易地发现这其实就是在一段区间里取最小值。

每次转移完当前点的值,我们都会把它当作下次转移时的区间里的一部分。

提炼一下:区间取 \(\min\),单点修改。

线段树可以很方便的处理掉,复杂度 \(O(L\log L)\) 。

AC Code
#include<bits/stdc++.h>
#define int long long
#define rep(I,A,B) for(int I=(A);I<=(B);++I)
#define per(I,A,B) for(int I=(A);I>=(B);--I)
#define el puts("")
#define Yuki return
#define daisuki 0
#define debug puts("SKIRK")
#define none 'N'
#define ed '\n'
#define pc putchar
using namespace std;
using pii=pair<int,int>;
//fastIO start
template<typename T>
void read(T &x){
x=0;bool f=0;char c=getchar();
for(;!isdigit(c);c=getchar())if(c=='-')f=1;
for(;isdigit(c);c=getchar())x=(x<<3)+(x<<1)+(c^48);
if(f)x=-x;
}
template<typename T,typename ...Args>
void read(T &x,Args &...args){
read(x),read(args...);
}
template<typename T>
void write(T x,char ch=' '){
if(x<0)putchar('-'),x=-x;
static short st[70],tp;
do st[++tp]=x%10,x/=10;while(x);
while(tp)putchar(st[tp--]|48);
if(ch!=none)putchar(ch);
}
void writes(string str){
rep(i,0,str.size()-1)putchar(str[i]);
}
void writec(char chs){
putchar(chs);
}
//fastIO end
const int N=1e3+5;
const int L=1e6+5;
const int inf=0x3f3f3f3f;
struct edge{
int s,e;
}p[N];
int n,l,a,b;
bool vis[L];
int dp[L];
struct node{
int l,r,mn;
}tr[L<<2];
void build(int p,int l,int r){
tr[p].l=l;
tr[p].r=r;
tr[p].mn=inf;
if(l==r)return ;
int mid=l+r>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r);
}
int query(int p,int L,int R){
int l=tr[p].l;
int r=tr[p].r;
if(r<L||l>R)return inf;
if(L<=l&&r<=R)return tr[p].mn;
return min(query(p<<1,L,R),query(p<<1|1,L,R));
}
void modify(int p,int L,int v){
int l=tr[p].l;
int r=tr[p].r;
if(r<L||l>L)return ;
if(l==r)return tr[p].mn=v,void();
modify(p<<1,L,v);
modify(p<<1|1,L,v);
tr[p].mn=min(tr[p<<1].mn,tr[p<<1|1].mn);
}
signed main(){
read(n,l,a,b);
rep(i,1,n){
read(p[i].s,p[i].e);
rep(j,p[i].s+1,p[i].e-1)vis[j]=1;
}
memset(dp,0x3f,sizeof(dp));
dp[0]=0;
build(1,0,l);
modify(1,0,0);
for(int i=2;i<=l;i+=2){
if(vis[i])continue;
dp[i]=query(1,max(i-2*b,0ll),i-2*a)+1;
modify(1,i,dp[i]);
// rep(j,a,b){ //暴力转移
// int k=i-j*2;
// if(k<0)continue;
// Min(dp[i],dp[k]+1);
// }
}
if(dp[l]>=inf)dp[l]=-1;
write(dp[l]);
Yuki daisuki;//Yuki真的是太可爱了捏
}
//by oSomiwww

P1545 [USACO04DEC] Dividing the Path G 题解的更多相关文章

  1. poj 2373 Dividing the Path

    Dividing the Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2858   Accepted: 1064 ...

  2. POJ 2373 Dividing the Path(DP + 单调队列)

    POJ 2373 Dividing the Path 描述 农夫约翰的牛发现,在他的田里沿着山脊生长的三叶草是特别好的.为了给三叶草浇水,农夫约翰在山脊上安装了喷水器. 为了使安装更容易,每个喷头必须 ...

  3. poj2373 Dividing the Path

    Dividing the Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5060   Accepted: 1782 ...

  4. 洛谷P3104 Counting Friends G 题解

    题目 [USACO14MAR]Counting Friends G 题解 这道题我们可以将 \((n+1)\) 个边依次去掉,然后分别判断去掉后是否能满足.注意到一点, \(n\) 个奶牛的朋友之和必 ...

  5. 洛谷P2115 Sabotage G 题解

    题目 [USACO14MAR]Sabotage G 题解 本蒟蒻又来了,这道题可以用二分答案来解决.我们可以设答案最小平均产奶量为 \(x \ (x \in[1,10000])\) .然后二分搜索 \ ...

  6. POJ 2373 Dividing the Path (单调队列优化DP)题解

    思路: 设dp[i]为覆盖i所用的最小数量,那么dp[i] = min(dp[k] + 1),其中i - 2b <= k <= i -2a,所以可以手动开一个单调递增的队列,队首元素就是k ...

  7. USACO07NOV Cow Relays G 题解

    题目 For their physical fitness program, \(N (2 ≤ N ≤ 1,000,000)\) cows have decided to run a relay ra ...

  8. P2882 Face The Right Way G 题解

    题目 Farmer John has arranged his N \((1 ≤ N ≤ 5,000)\) cows in a row and many of them are facing forw ...

  9. Hdoj 2454.Degree Sequence of Graph G 题解

    Problem Description Wang Haiyang is a strong and optimistic Chinese youngster. Although born and bro ...

  10. Dividing the Path POJ - 2373 dp

    题意:你有无数个长度可变的区间d  满足 2a<=d<=2b且为偶数. 现在要你用这些区间填满一条长为L(L<1e6且保证是偶数)的长线段. 满足以下要求: 1.可变区间之间不能有 ...

随机推荐

  1. 有100块石头, A和B交替拿, 每人一次拿1-5块, 如果A先拿, 第一次拿几块能保证最后能赢

    简介 其实这题可以分成 A | B A | B A ...| B A A第一次拿取x个 然后, 剩下B A 每次拿去一定要大于6等于6个. 如果 B A 拿取6个的话, 推出 第一次 拿取4个. 如果 ...

  2. JAVA基础-4-.数据类型--九五小庞

                  练习代码:     1 public class Demo1 { 2 public static void main(String[] args) { 3 System.o ...

  3. jsonb 为什么会影响 System.Text.Json

    我在将一个属性映射到 jsonb 类型时遇到这样一个问题 -- 我有一个抽象基类 BaseClass 和一个派生类 DerivedClass: [JsonDerivedType(typeof(Deri ...

  4. unity资源提取工具AssetStudio

    最新版本下载地址: https://ci.appveyor.com/project/Perfare/assetstudio/branch/master/artifacts 下载地址(2020年3月20 ...

  5. P6638 「JYLOI Round 1」常规

    容易把问题转换为求前缀和.设 \(p\) 为当前最大的下标使得 \(a_p \leq x\),则容易得到答案: \[\text{ans} = \sum_{i = 1}^{p}\left\lfloor\ ...

  6. etcd 和 MongoDB 的混沌(故障注入)测试方法

    最近在对一些自建的数据库 driver/client 基础库的健壮性做混沌(故障)测试, 去验证了解业务的故障处理机制和恢复时长. 主要涉及到了 MongoDB 和 etcd 这两个基础组件. 本文会 ...

  7. 【with xht】环球影城一日游游记

    已征取 xht 意见: day-1 大暴雨,飞机场跑道被雷劈了,航班取消. 第二天的机票价格飞升. 没改签,直接退票买了新的机票.因为怕又出问题. day0 新机票没出问题,还提前降落了. 在机场怒打 ...

  8. artifactory 上传下载命令

    下载单个文件到本地: # 下载远程a.txt 到本地a.txt jf rt dl "aa/bb/a.txt" "a.txt" --url="$ARTI ...

  9. Hololens2 开发问题存档

    1.安装环境 1. 下载Visual Studio Installer,然后安装2022版本的vs社区版,然后点击修改. 2. 下载Visual Studio Installer,然后安装2022版本 ...

  10. echarts 案例大全(传送门)

    传送门一: http://ppchart.com/#/ 传送门二: http://analysis.datains.cn/finance-admin/index.html#/chartLib/all ...