hdu4302 Holedox Eating

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4302

Holedox Eating

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1031 Accepted Submission(s): 354


Problem Description
Holedox is a small animal which can be considered as one point. It lives in a straight pipe whose length is L. Holedox can only move along the pipe. Cakes may appear anywhere in the pipe, from time to time. When Holedox wants to eat cakes, it always goes to the nearest one and eats it. If there are many pieces of cake in different directions Holedox can choose, Holedox will choose one in the direction which is the direction of its last movement. If there are no cakes present, Holedox just stays where it is.
 

 

Input
The input consists of several test cases. The first line of the input contains a single integer T (1 <= T <= 10), the number of test cases, followed by the input data for each test case.The first line of each case contains two integers L,n(1<=L,n<=100000), representing the length of the pipe, and the number of events.
The next n lines, each line describes an event. 0 x(0<=x<=L, x is a integer) represents a piece of cake appears in the x position; 1 represent Holedox wants to eat a cake.
In each case, Holedox always starts off at the position 0.
 

 

Output
Output the total distance Holedox will move. Holedox don’t need to return to the position 0.
 

 

Sample Input
3 10 8 0 1 0 5 1 0 2 0 0 1 1 1 10 7 0 1 0 5 1 0 2 0 0 1 1 10 8 0 1 0 1 0 5 1 0 2 0 0 1 1
 

 

Sample Output
Case 1: 9 Case 2: 4 Case 3: 2
 

题意:

一只狗,在0~n长的管子里面吃蛋糕,0操作代表在某个点放蛋糕,1代表狗吃掉离他最近的蛋糕,当距离狗相同时,吃原来方向的蛋糕,要求的是狗走的步数,狗的起始点在0.

思路:

线段树的题,首先节点存储的信息是区间内离狗最近的点。然后只要依次对节点进行更新即可。

代码:

 1 #include <stdio.h>
 2 int a[400020];//节点
 3 int b[400020];//代表某个点有多少块蛋糕
 4 int s,n;
 5 int xt;//判断狗的方向
 6 int fb(int x)
 7 {
 8     if(x<0)
 9         x=-x;
10     return x;
11 }
12 void build(int l,int r,int rt)
13 {
14     if(l==r)
15     {
16         a[rt]=400010;
17         return ;
18     }
19     int m=(l+r)/2;
20     build(l,m,2*rt);
21     build(m+1,r,2*rt+1);
22     if(fb(n-a[2*rt])<fb(n-a[2*rt+1]))//选取离狗最近的点。
23         a[rt]=a[2*rt];
24     else
25         a[rt]=a[2*rt+1];
26 }
27 void update(int l,int r,int rt,int x1,int x2)
28 {
29     if(l==r)
30     {
31         a[rt]=x2;
32         return ;
33     }
34     int m=(l+r)/2;
35     if(m>=x1)
36         update(l,m,2*rt,x1,x2);
37     if(m<x1)
38         update(m+1,r,2*rt+1,x1,x2);
39     if(fb(n-a[2*rt])<fb(n-a[2*rt+1]))// 选取离狗最近的点。
40         a[rt]=a[2*rt];
41     if(fb(n-a[2*rt])>fb(n-a[2*rt+1]))
42         a[rt]=a[2*rt+1];
43     if(fb(n-a[2*rt])==fb(n-a[2*rt+1]))//特别注意等于的情况。
44     {
45         if(xt==1)
46             a[rt]=a[2*rt+1];
47         if(xt==0)
48             a[rt]=a[2*rt];
49     }
50 }
51 int main()
52 {
53     int i,j,m;
54     int n1;
55     int op;
56     int x1,x2,x3;
57     while(scanf("%d",&m)!=EOF)
58     {
59         for(i=1;i<=m;i++)
60         {
61             scanf("%d%d",&n1,&x1);
62             for(j=0;j<=n1;j++)
63                 b[j]=0;
64             build(0,n1,1);
65             s=n=0;
66             xt=1;//狗的开始方向
67             while(x1--)
68             {
69                 scanf("%d",&op);
70                 if(op==0)
71                 {
72                     scanf("%d",&x2);
73                     b[x2]++;//在这个点的蛋糕数+1
74                     update(0,n1,1,x2,x2);//更新节点信息
75                 }
76                 if(op==1)
77                 {
78                     if(a[1]!=400010)//这个条件可以使当没有蛋糕时狗不动
79                     {
80                         if(a[1]>n)
81                             xt=1;
82                         if(a[1]<n)
83                             xt=0;
84                         s=s+fb(n-a[1]);
85                         n=a[1];
86                         if(b[n]>0)//这个条件可以让狗吃掉当前点的多块蛋糕
87                             b[n]--;
88                         if(b[n]==0)
89                             update(0,n1,1,n,400010);//当这个点蛋糕被吃完时,就更新节点信息。
90                     }
91                 }
92             }
93             printf("Case %d: ",i);
94             printf("%d\n",s);
95         }
96     }
97     return 0;
98 }

 

 

posted @ 2012-07-21 21:32  hnust_tongguang  阅读(265)  评论(0编辑  收藏  举报