EOJ:Hurry Plotter

Hurry Plotter

Time Limit: 1000MS Memory Limit: 65536K
Total Submits: 11 Accepted: 5

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 i-th test case on the i-th 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.

 

Sample Input

1 3
0 1 2
3 5
1 1 2
3 1 3
1 3 4
3 6
1 1 2
3 1 3
1 3 4
4 11
1 3 4
1 1 2
2 1 2
2 3 4
0 0

Sample Output

1
1
2
3
原题地址:http://www.cn210.com/onlinejudge/problemshow.php?pro_id=221
___________________________________________________________________________________________________________
题解:

先排序

DP[I][J]表示前I条线,且第I条必选,已经选了J个所用的最小时间

 

代码
1 #include<stdio.h>
2 #include<stdlib.h>
3  #define oo 0xfffffff
4  int dp[1001][1001],time[1001][1001];
5 int n,t,i,j,k,ans;
6 struct segment
7 {
8 int y,xs,xt;
9 }seg[1001];
10 int cmp(const void *va,const void *vb)
11 {
12 struct segment *r1=(struct segment*)va;
13 struct segment *r2=(struct segment*)vb;
14 if (r1->y>r2->y)
15 {
16 return 1;
17 }
18 else
19 {
20 if (r1->y<r2->y)
21 {
22 return -1;
23 }
24 else
25 return r1->xs-r2->xs;
26 }
27 }
28 int min(int x,int y)
29 {
30 if (x>y) return y;
31 return x;
32 }
33 int main()
34 {
35 while (scanf("%d%d",&n,&t)&&n)
36 {
37 for (i=1;i<=n;i++)
38 scanf("%d%d%d",&seg[i].y,&seg[i].xs,&seg[i].xt);
39 qsort(seg+1,n,sizeof(seg[1]),cmp);
40 for (i=0;i<=n;i++)
41 for (j=1;j<=n;j++)
42 {
43 time[i][j]=oo;
44 if (i==0)
45 time[i][j]=seg[j].xs+2*(seg[j].xt-seg[j].xs);
46 if (i!=0&&i<j)
47 {
48 if (seg[i].y==seg[j].y&&seg[i].xt<=seg[j].xs)
49 time[i][j]=seg[j].xs-seg[i].xt+2*(seg[j].xt-seg[j].xs);
50 if (seg[i].y<seg[j].y)
51 time[i][j]=seg[i].xt+seg[j].xs+2*(seg[j].xt-seg[j].xs);
52 }
53 }
54 for (i=0;i<=n;i++)
55 for (j=0;j<=n;j++)
56 dp[i][j]=oo;
57 dp[0][0]=0;
58 for (i=1;i<=n;i++)
59 for (j=1;j<=i;j++)
60 for (k=0;k<i;k++)
61 dp[i][j]=min(dp[i][j],dp[k][j-1]+time[k][i]);
62 ans=0;
63 for (i=n;i>=1;i--)
64 for (j=n;j>=1;j--)
65 if (dp[i][j]<=t)
66 if (ans<j)
67 ans=j;
68 printf("%d\n",ans);
69 }
70 return 0;
71 }
72

 

posted on 2010-07-28 14:34  风也轻云也淡  阅读(155)  评论(0编辑  收藏  举报