hdoj 4302 Holedox Eating 【优先队列】

本文概述了AI音视频处理领域的关键技术,包括视频分割、语义识别、自动驾驶、AR、SLAM等,并探讨了其在实际应用中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Holedox Eating

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


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
 
 
优先队列:一个记录左边食物,一个记录右边食物。
 
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
priority_queue<int,vector<int>,less<int> >left;
priority_queue<int,vector<int>,greater<int> >right;
int main()
{
    int t,n,m;
    int i,j,k=1;
    int op,pos;
    int now;//当前所在位置 
    int move;//记录总移动数 
    int face;//记录上一次移动方向 1为右 0为左 
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        while(!left.empty())
        {
            left.pop();
        } 
        while(!right.empty())
        {
            right.pop();
        }
        move=now=0;face=1;//第一步必向右 
        while(m--)
        {
            scanf("%d",&op);
            if(!op)
            {
                scanf("%d",&pos);
                if(pos>=now)
                right.push(pos);
                else
                left.push(pos);
            }
            else
            {
                if(left.empty()&&right.empty())
                {
                    continue;
                }
                if(left.empty())
                {
                    move+=right.top()-now;
                    now=right.top();
                    right.pop();
                    face=1;
                    continue;
                }
                if(right.empty())
                {
                    move+=now-left.top();
                    now=left.top();
                    left.pop();
                    face=0;
                    continue;
                }
                if(now-left.top()<right.top()-now)
                {
                    move+=now-left.top();
                    now=left.top();
                    left.pop();
                    face=0;
                }
                else if(now-left.top()>right.top()-now)
                {
                    move+=right.top()-now;
                    now=right.top();
                    right.pop();
                    face=1;
                }
                else
                {
                    if(face)
                    {
                        move+=right.top()-now;
                        now=right.top();
                        right.pop();
                        face=1;
                    }
                    else
                    {
                        move+=now-left.top();
                        now=left.top();
                        left.pop();
                        face=0;
                    }
                }
            }
        }
        printf("Case %d: ",k++);
        printf("%d\n",move);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值