Description

A plotter is a vector graphics printing device that connects to a computer to print graphical plots. There are two types of plotters: pen plotters and electrostatic plotters. Pen plotters print by moving a pen across the surface of a piece of paper. They can draw complex line art, including text, but do so very slowly because of the mechanical movement of the pens. In this problem, we are considering this matter of slowness for our special type of pen plotter. A discrete horizontal pen plotter can draw only horizontal line segments whose end points have discrete coordinates (integer x and y’s). The drawing method is quite simple. The pen starts its journey from the upper left corner of the page (x=y=0) and moves only right while drawing the specified lines on that row. Then, it moves back completely to the left, moves one row down (y ← y+1), and repeats this task for the second row. The same is done for the next rows. In other words, the pen can move down only when it is far on the left side (i.e. when x=0), and can have at most one left-to-right pass and at most one right-to-left pass on each row. 
It takes one unit of time to move the pen one unit of length to the left (x ← x-1), or to the right (x ← x+1). This time is doubled if the pen is on the paper and is drawing a line segment. It takes no time to move one row down (when x=0). 
Since it might take a long time for the plotter to draw all the given line segments, we have decided to add a new feature to our plotter: drawing time-limit. By specifying the time-limit, the plotter should draw the maximum number of lines (using the same drawing method given above) that can be drawn within that time-limit. Given the time-limit and line segments, you should find this maximum number.

Input

The input contains multiple test cases. Each test case starts with a line containing two integers n and t. The integer n is the number of line segments (n ≤ 1000) and t is the time-limit (t ≤ 106). Each of the next n lines specifies a line segment by giving three integers y, xs, and xt. Integer y indicates the row of that line segment (0 ≤ y ≤ 2000), and xs and xt are the x-coordinates of its end points (0 ≤ xs ≤ xt ≤ 106). The line segments are disjoint and do not have any intersections. A case of n = t = 0 shows the end of input and should not be processed.

Output

Write the result of the ith test case on the ith line of output. Each line should have only one integer, indicating the maximum number of line segments that can be drawn in its corresponding test case. 

题目大意:有n条水平的横线,每条横线都有一个纵坐标,和横线的开始横坐标和结束横坐标。现在有一支笔,要划这些线,这支笔只能从上往下移动,并且只能在x=0的地方从上往下移动。画横线的时候一定要从左到右画,画线移动的时间是普通移动时间的两倍。笔的每次横坐标移动花费时间为1,现在有时间限制t,问最多能画多少条线。

思路:先按y轴、y轴从小到大排序(即我们只能从序号小的移动到序号大的),然后dp[i][j]表示画到第 i 条横线,一共走过了 j 条横线,所花费的最小时间。然后每一个点不同步数找之前的可以走过来的点。暴力点时间复杂度为$O(n^3)$,不过貌似数据比较弱,n≤1000都秒过了。

代码(32MS):

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std; const int MAXN = ; struct Node {
int y, st, ed;
void read() {
scanf("%d%d%d", &y, &st, &ed);
}
bool operator < (const Node &rhs) const {
if(y != rhs.y) return y < rhs.y;
return ed < rhs.ed;
}
}; int length(const Node &a, const Node &b) {
if(a.y == b.y) return b.st - a.ed;
return a.ed + b.st;
} Node a[MAXN];
int dp[MAXN][MAXN];
int n, t, ans; void solve() {
memset(dp, 0x3f, sizeof(dp));
ans = ;
for(int i = ; i <= n; ++i) {
if((dp[i][] = * a[i].ed - a[i].st) <= t) ans = max(ans, );
for(int j = ; j <= i; ++j) {
for(int k = j - ; k < i; ++k)
dp[i][j] = min(dp[i][j], dp[k][j - ] + length(a[k], a[i]));
dp[i][j] += * (a[i].ed - a[i].st);
if(dp[i][j] <= t) ans = max(ans, j);
}
}
} int main() {
while(scanf("%d%d", &n, &t) != EOF && n + t) {
for(int i = ; i <= n; ++i) a[i].read();
sort(a + , a + n + );
solve();
printf("%d\n", ans);
}
}

POJ 3858 Hurry Plotter(DP)的更多相关文章

  1. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  2. POJ - 2385 Apple Catching (dp)

    题意:有两棵树,标号为1和2,在Tmin内,每分钟都会有一个苹果从其中一棵树上落下,问最多移动M次的情况下(该人可瞬间移动),最多能吃到多少苹果.假设该人一开始在标号为1的树下. 分析: 1.dp[x ...

  3. POJ 1260:Pearls(DP)

    http://poj.org/problem?id=1260 Pearls Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8 ...

  4. POJ 2192 :Zipper(DP)

    http://poj.org/problem?id=2192 Zipper Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1 ...

  5. POJ 1191 棋盘分割(DP)

    题目链接 题意 : 中文题不详述. 思路 : 黑书上116页讲的很详细.不过你需要在之前预处理一下面积,那样的话之后列式子比较方便一些. 先把均方差那个公式变形, 另X表示x的平均值,两边平方得 平均 ...

  6. POJ 2533-Longest Ordered Subsequence(DP)

    Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 34454   Acc ...

  7. POJ 1018 Communication System(DP)

    http://poj.org/problem?id=1018 题意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m1.m2.m3.....mn个厂家提供生产,而每个厂家生产 ...

  8. POJ 2948 Martian Mining(DP)

    题目链接 题意 : n×m的矩阵,每个格子中有两种矿石,第一种矿石的的收集站在最北,第二种矿石的收集站在最西,需要在格子上安装南向北的或东向西的传送带,但是每个格子中只能装一种传送带,求最多能采多少矿 ...

  9. POJ 3280 Cheapest Palindrome(DP)

    题目链接 题意 :给你一个字符串,让你删除或添加某些字母让这个字符串变成回文串,删除或添加某个字母要付出相应的代价,问你变成回文所需要的最小的代价是多少. 思路 :DP[i][j]代表的是 i 到 j ...

随机推荐

  1. 示例浅谈PHP与手机APP开发,即API接口开发

    示例浅谈PHP与手机APP开发,即API接口开发 API(Application Programming Interface,应用程序接口)架构,已经成为目前互联网产品开发中常见的软件架构模式,并且诞 ...

  2. 《计算机图形学3D》

    <计算机图形学方法原理应用> Opengl语言    光线跟踪   贝塞尔曲线  射线追踪   色彩理论  纹理映射 逆向运动   MPI  仿射   绘制流水线   透视变换   bre ...

  3. 分布式id生成

    2016年08月09日 14:15:21 yuanyuanispeak 阅读数:318 编辑 一.需求缘起 几乎所有的业务系统,都有生成一个记录标识的需求,例如: (1)消息标识:message-id ...

  4. JS基础——数组API之数组操作(filter、map、some、every、sort)

    var arr = [1,2,3,4];   forEach arr.forEach((item,index,arr) => { console.log(item) //结果为1,2,3,4 } ...

  5. spring boot 搭建基本套路《1》

    1. Spring复习 Spring主要是创建对象和管理对象的框架. Spring通过DI实现了IoC. Spring能很大程度的实现解耦. 需要掌握SET方式注入属性的值. 需要理解自动装配. 需要 ...

  6. html样式不兼容 详解(转)

    网站对火狐不兼容的原因以及解决的方法 1.DOCTYPE 影响 CSS 处理 2.FF: div 设置 margin-left, margin-right 为 auto 时已经居中, IE 不行 3. ...

  7. weex踩坑记录

    weex框架样式问题--我暂时使用最基本的样式css,weex前端开发的话web端会显示各种的html标签.写出的样式也都会显示的很好,但是在app端的话,就没有很好的兼容性,只是支持文档中的一些标签 ...

  8. HTML5+ MUI实现ajax的一个demo

    index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> &l ...

  9. 微信小程序横向滚动

    <scroll-view scroll-x="true" style=" white-space: nowrap; display: flex" > ...

  10. Matplotlib 子图的创建

    在matplotlib中,整个图像为一个Figure对象 在Figure对象中可以包含一个或者多个Axes对象  每个Axes对象相当于一个子图了 每个Axes(ax)对象都是一个拥有自己坐标系统的绘 ...