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 }

 

 

转载于:https://www.cnblogs.com/hnusttongguang/archive/2012/07/21/2603078.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
校园失物招领系统管理系统按照操作主体分为管理员和用户。管理员的功能包括字典管理、论坛管理、公告信息管理、失物招领管理、失物认领管理、寻物启示管理、寻物认领管理、用户管理、管理员管理。用户的功能等。该系统采用了Mysql数据库,Java语言,Spring Boot框架等技术进行编程实现。 校园失物招领系统管理系统可以提高校园失物招领系统信息管理问题的解决效率,优化校园失物招领系统信息处理流程,保证校园失物招领系统信息数据的安全,它是一个非常可靠,非常安全的应用程序。 ,管理员权限操作的功能包括管理公告,管理校园失物招领系统信息,包括失物招领管理,培训管理,寻物启事管理,薪资管理等,可以管理公告。 失物招领管理界面,管理员在失物招领管理界面中可以对界面中显示,可以对失物招领信息的失物招领状态进行查看,可以添加新的失物招领信息等。寻物启事管理界面,管理员在寻物启事管理界面中查看寻物启事种类信息,寻物启事描述信息,新增寻物启事信息等。公告管理界面,管理员在公告管理界面中新增公告,可以删除公告。公告类型管理界面,管理员在公告类型管理界面查看公告的工作状态,可以对公告的数据进行导出,可以添加新公告的信息,可以编辑公告信息,删除公告信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值