hdu2155 dp

 1 #include<stdio.h>
 2 #include<string.h>
 3 int h[10005],xl[10005],xr[10005],dp[10005][5];
 4 int min(int a,int b)
 5 {if (a<b) return(a); return(b);}
 6 void sort(int l,int r)
 7 {
 8     int i,j,x,y;
 9     i=l; j=r; 
10     x=h[(i+j)/2];
11     while (i<=j)
12     {
13         while (h[i]<x) i++;
14         while (x<h[j]) j--;
15         if (i<=j)
16         {
17             y=h[i]; h[i]=h[j]; h[j]=y;
18             y=xl[i]; xl[i]=xl[j]; xl[j]=y;
19             y=xr[i]; xr[i]=xr[j]; xr[j]=y;
20             i++; j--;
21         }
22     }
23     if (l<j) sort(l,j);
24     if (i<r) sort(i,r);
25 }
26 int main()
27 { 
28     int n,x,y,t,max,m,i,j,judge;
29     while (~scanf("%d",&t))
30     while (t--)
31     {
32         scanf("%d%d%d%d%d",&n,&x,&y,&max,&m);
33         for (i=1;i<=n;i++) scanf("%d%d%d",&xl[i],&xr[i],&h[i]);
34         sort(1,n); 
35         for (i=1;i<=n;i++) dp[i][0]=dp[i][1]=m+1; 
36         for (i=1;i<=n;i++)
37         {
38             j=0;
39             
40             for (j=i-1;j>=1;j--)
41              if (xl[j]<=xl[i]&&xl[i]<=xr[j]) break; 
42             if (j==0) 
43              { if (h[i]<=max) dp[i][0]=h[i]; }
44             else if (h[i]-h[j]<=max) 
45              dp[i][0]=min(dp[j][0]+h[i]-h[j]+xl[i]-xl[j],dp[j][1]+h[i]-h[j]+xr[j]-xl[i]);
46             
47             for (j=i-1;j>=1;j--)
48              if (xl[j]<=xr[i]&&xr[i]<=xr[j]) break; 
49             if (j==0) 
50              { if (h[i]<=max) dp[i][1]=h[i]; }
51             else if (h[i]-h[j]<=max)
52              dp[i][1]=min(dp[j][0]+h[i]-h[j]+xr[i]-xl[j],dp[j][1]+h[i]-h[j]+xr[j]-xr[i]);             
53         }
54         for (j=n;j>=1;j--)
55          if (xl[j]<=x&&x<=xr[j]) break;
56         judge=m+1;
57         if (j==0)
58          { if (y<=max) judge=y; }
59         else if (y-h[j]<=max)
60          judge=min(dp[j][0]+y-h[j]+x-xl[j],dp[j][1]+y-h[j]+xr[j]-x);
61         if (judge<=m) printf("NO\n");
62         else printf("YES\n"); 
63     }
64 }

http://acm.hdu.edu.cn/showproblem.php?pid=2155

posted on 2014-07-16 16:23  xiao_xin  阅读(111)  评论(0编辑  收藏  举报

导航