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. webpack——概念的引入

    ## 在网页中会引用哪些常见的静态资源?+ JS - .js .jsx .coffee .ts(TypeScript 类 C# 语言)+ CSS - .css .less .sass .scss+ I ...

  2. TIDB4 —— 三篇文章了解 TiDB 技术内幕 - 谈调度

    原文地址:https://pingcap.com/blog-cn/tidb-internal-3/ 为什么要进行调度 先回忆一下第一篇文章提到的一些信息,TiKV 集群是 TiDB 数据库的分布式 K ...

  3. xcode7--iOS开发---将app打包发布至app store

    时隔3个月再次接触应用打包,又是一顿折腾 说说这次的感受吧: 变得是打包时间减少到4小时(其中大部分时间还是xcode7或者是iOS9的原因),不变的是还是一如既往的坑!! 好了,废话不多说,下面讲讲 ...

  4. 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛--K-密码

    链接:https://www.nowcoder.com/acm/contest/90/K 来源:牛客网 - 1.题目描述 ZiZi登录各种账号的时候,总是会忘记密码,所以他把密码都记录在一个记事本上. ...

  5. string类中字符的大小写转换

    今天做一道题,要用string类,涉及大小写转换,查看了C++文档,string类没有提供这样的方法,只好自己写. 之后是想到一个比较笨的方法,我把string当成一个容器,然后用迭代器一个一个来替换 ...

  6. ABAP术语-Distribution Model

    Distribution Model 原文:http://www.cnblogs.com/qiangsheng/archive/2008/01/25/1052434.html Model that d ...

  7. ES6、7、8、9新语法新特性

    前言 如果你擅长这种扩散式学习方式,不妨再进一步温习一下整个 ES6 引入的新特性,笔者强烈推荐阮一峰老师的 ECMAScript 6 入门 一书. ES2018 引入的特性还太新,单在对 ES6 特 ...

  8. html 弹框 优化 alert

    <!DOCTYPE html> <html> <head> <title>cs</title> </head> <styl ...

  9. rails中如何在a标签中添加其他标签

    最近在用rails写一个项目练练手,然后遇到了一个问题,就是用 <% link_to("首页", root_path) %> 生成一个a标签,之后就在想我怎么在这个a标 ...

  10. 回形矩阵--python

    def bsm(n): a = [[0]*n for x in range(n)] p = 0 q = n-1 t = 1 while p < q: for i in range(p,q): a ...