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. 【模板】概率dp

    有n个投资事件,和一个成功概率最低接受值rate.每个投资的价值是c[i],成功概率是p[i](浮点数). 在保证成功概率≥rate的情况下,使价值最大化. #include<bits/stdc ...

  2. python 之函数

    一 函数的定义:对功能和动作的封装和定义.二 函数的格式:def 函数名(形参列表): 函数名就是变量名:规则就是变量的规则 函数体(return) ret = 函数名(实参列表)三 函数的返回值:函 ...

  3. 修改zabbix字体格式

    环境: centos7 zabbix3.2 1.获取喜欢的字体格式文件(喜欢别的字体也可以去网上下载) 通常都是ttf格式,可直接在windows下获取C:\Windows\Fonts 2.配置zab ...

  4. ssm整合-错误4

    严重: Servlet.service() for servlet [dispatcher] in context with path [/management] threw exception [R ...

  5. 【操作系统作业—lab1】linux shell脚本 遍历目标文件夹和所有文件 | 包括特殊字符文件名的处理

    要求:写一个linux bash脚本来查看目标文件夹下所有的file和directory,并且打印出他们的绝对路径. 运行command:./myDir.sh  input_path  output_ ...

  6. 使用hibernate框架连接oracle数据库进行简单的增删改

    初始化配置和session 关于配置文件这里就不在赘述了,假设配置文件配好后我们需要加载配置和sessionFactory,并获取session,因为每次进行增删改查时都需要session,所以封装成 ...

  7. over开窗函数的用法

    over(partition by c1.pmid,d1.type,e1.objid  order by e1.objid ) pinum 先根据字段排序,pinum.在取第一条数据and p1.pi ...

  8. .Net Core On Liunx 环境搭建之安装Mysql8

    上一篇文章安装了MongoDB紧接上一篇随笔,来进行MySql数据库的安装 服务器环境:阿里云云服务器,操作系统CentOS.7-x64 注:文章的图片是我从我的CSDN博客中直接粘贴过来的,不是扒的 ...

  9. MySQL数据操作(DML)

    表结构准备: mysql> CREATE TABLE student( -> sid INT PRIMARY KEY AUTO_INCREMENT, ), -> age INT, ) ...

  10. weui-switch开关控件,表单提交后如何取值

    最近在学习weui这个框架,做了一些小的试验,发现weui-switch控件直接提交不能获取到表单信息,在segmentfault上发现也有人提了这个问题,有人说可以设置一个隐含标签来捕获开关的状态, ...