题目描述

  平面上有\(n\)个点,你要用一些矩形覆盖这些点,要求:

  • 每个矩形的下边界为\(y=0\)
  • 每个矩形的大小不大于\(s\)

  问你最少要用几个矩形。

  \(n\leq 100,1\leq y\leq s\)

题解

  先把坐标离散化。

  猜(zheng)一个结论:最优解中任意两个矩形的横坐标只可能是相离或包含,不可能是相交。证明略。

  考虑区间DP。

  设\(f_{l,r,h}\)为覆盖横坐标\(l\sim r\),纵坐标\(>h\)的所有矩形需要的最少次数。

  枚举\(l,r,h\),有两种转移:

  • 找到一个横坐标\(i\),使得没有任意一个矩形穿过\(i\)。枚举\(i\)分治即可。
  • 放一个横坐标为\(l\sim r\)的矩形,把高度设为上限。

  对于每一个\(h\),这一层的转移是\(O(n^3)\)的,到下一层的转移是\(O(n^2\log n)\)的,所以总时间复杂度就是\(O(n^4)\)。

  用记忆化搜索可以跑得飞快。

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<utility>
using namespace std;
typedef pair<int,int> pii;
int n,s;
pii a[110];
int f[110][110][110];
int xx[110];
int yy[110];
int m1,m2;
int d[110];
int gao(int x)
{
return x?s/x:0x3fffffff;
}
int gao(int l,int r,int h)
{
int &s=f[h][l][r];
if(~s)
return s;
while(l<=r&&d[l]<=h)
l++;
while(l<=r&&d[r]<=h)
r--;
if(l>r)
return s=0;
int i;
s=0x7fffffff;
for(i=l;i<r;i++)
s=min(s,gao(l,i,h)+gao(i+1,r,h));
int hh=gao(xx[r]-xx[l]);
if(hh<=yy[h])
return s;
int v=upper_bound(yy+1,yy+m2+1,hh)-yy-1;
s=min(s,gao(l,r,v)+1);
return s;
}
void solve()
{
scanf("%d%d",&n,&s);
int i;
for(i=1;i<=n;i++)
{
scanf("%d%d",&a[i].first,&a[i].second);
xx[i]=a[i].first;
yy[i]=a[i].second;
}
sort(xx+1,xx+n+1);
sort(yy+1,yy+n+1);
m1=unique(xx+1,xx+n+1)-xx-1;
m2=unique(yy+1,yy+n+1)-yy-1;
memset(f,-1,sizeof f);
for(i=1;i<=m1;i++)
d[i]=0;
for(i=1;i<=n;i++)
{
a[i].first=lower_bound(xx+1,xx+m1+1,a[i].first)-xx;
a[i].second=lower_bound(yy+1,yy+m2+1,a[i].second)-yy;
d[a[i].first]=max(d[a[i].first],a[i].second);
}
int ans=gao(1,m1,0);
printf("%d\n",ans);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("b.in","r",stdin);
freopen("b.out","w",stdout);
#endif
int t;
scanf("%d",&t);
while(t--)
solve();
return 0;
}

【XSY2693】景中人 区间DP的更多相关文章

  1. 【BZOJ-4380】Myjnie 区间DP

    4380: [POI2015]Myjnie Time Limit: 40 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 162  Solved: ...

  2. 【POJ-1390】Blocks 区间DP

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5252   Accepted: 2165 Descriptio ...

  3. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

  4. BZOJ1055: [HAOI2008]玩具取名[区间DP]

    1055: [HAOI2008]玩具取名 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1588  Solved: 925[Submit][Statu ...

  5. poj2955 Brackets (区间dp)

    题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...

  6. HDU5900 QSC and Master(区间DP + 最小费用最大流)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...

  7. BZOJ 1260&UVa 4394 区间DP

    题意: 给一段字符串成段染色,问染成目标串最少次数. SOL: 区间DP... DP[i][j]表示从i染到j最小代价 转移:dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k ...

  8. 区间dp总结篇

    前言:这两天没有写什么题目,把前两周做的有些意思的背包题和最长递增.公共子序列写了个总结.反过去写总结,总能让自己有一番收获......就区间dp来说,一开始我完全不明白它是怎么应用的,甚至于看解题报 ...

  9. Uva 10891 经典博弈区间DP

    经典博弈区间DP 题目链接:https://uva.onlinejudge.org/external/108/p10891.pdf 题意: 给定n个数字,A和B可以从这串数字的两端任意选数字,一次只能 ...

随机推荐

  1. C. Painting the Fence

    链接 [https://codeforces.com/contest/1132/problem/C] 题意 就是有个n长的栅栏,然后每个油漆工可以染的区域不同 给你q让你选出q-2个人使得被染色的栅栏 ...

  2. Two distinct points CodeForces - 1108A (签到)

    You are given two segments [l1;r1][l1;r1] and [l2;r2][l2;r2] on the xx-axis. It is guaranteed that l ...

  3. python 中的super()继承,搜索广度为先

    一.python中类的继承 1.1 单继承 在python 中我们可以这样来定义一个类:及继承它的子类 class Father: def __init__(self, mes): #1 父类的ini ...

  4. Vue-router路由使用,单页面的实现

    1.安装路由系统 NPM npm install vue-router 2.在main.js中进入引用 import VueRouter from 'vue-router' 3.创建三个空的组件: V ...

  5. marMariaDB & MYSQL flexviews

    Using Flexviews - part one, introduction to materialized views - Percona Database Performance Bloght ...

  6. Nginx三部曲(2)性能

    我们会告诉你 Nginx 如何工作及其背后的理念,还有如何优化以加快应用的性能,如何安装启动和保持运行. 这个教程有三个部分: 基本概念 —— 这部分需要去了解 Nginx 的一些指令和使用场景,继承 ...

  7. babel (三) babel polly-fill

    Babel includes a polyfill that includes a custom regenerator runtime and core-js. This will emulate ...

  8. [转帖]NUMA

    作者:ibless 来源:CSDN 原文:https://blog.csdn.net/ibless/article/details/80114009 其实 很早之前对这一块有了解 比较多的的是 CCN ...

  9. Leetcode SingleNumber I & II & III 136/137/260

    SingleNumber I: 题目链接:https://leetcode-cn.com/problems/single-number/ 题意: 给定一个非空整数数组,除了某个元素只出现一次以外,其余 ...

  10. 搞了一下午时间全浪费在这了,其实是自己拷贝了patch文件,导致tab变成了空格的错

    很老实的基于最新的kernel,源文件,修改了代码.通过diff -uNr --show-c-function dir1 dir2 > ipv6.patch制作了patch文件,准备代码上库构建 ...