zoukankan      html  css  js  c++  java
  • 贪吃蛇(双重优先队列的综合运用)

    Holedox Eating

    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
    题意理解:一条蛇生活在一个管子里,然后管子上面的某些位置会一次出现食物,每次蛇都会吃最近的食物,吃完之后就原地不动,等待下一次吃食物,如果有两个食物距离蛇一样远并且都是最近的,那么蛇不会掉头,而是直接按他最后停留的方向走,去吃自己前方的食物,最后给一些命令,问蛇一共走了多少路。
    思路:无论蛇怎么移动去吃蛋糕,总会有俩队数列在他左右,所以优先左边最大的出来,右边最小的出来,依次比较就能求出总路程了。
      1 #include<iostream>
      2 #include<cstdio>
      3 #include<string>
      4 #include<cstring>
      5 #include<queue>
      6 #include<vector>
      7 #include<algorithm>
      8 using namespace std;
      9 struct cmp
     10 {
     11     bool operator()(int x,int y)
     12     {
     13         return x>y;
     14     }
     15 };
     16 int main()
     17 {
     18    int t;
     19    cin>>t;
     20    int cas=1;
     21      priority_queue<int >left;
     22        priority_queue<int,vector<int>,cmp> right;
     23    while(t--)
     24    {
     25        int sum=0;
     26        int l,n;
     27        cin>>l>>n;
     28       while(!left.empty())  left.pop();
     29       while(!right.empty()) right.pop();
     30        string ok="right";
     31        int flag=0;
     32        while(n--)
     33        {
     34            int x,y;
     35            cin>>x;
     36            if(x==0)
     37            {
     38                cin>>y;
     39                if(y>=flag) right.push(y);
     40                else left.push(y);
     41 
     42 
     43            }
     44            else
     45            {
     46                if(right.empty()&&left.empty())
     47                 continue;
     48 
     49                 if(right.empty()||left.empty())
     50              {
     51 
     52                  if(!right.empty()) {sum+=right.top()-flag;
     53      flag=right.top();
     54       right.pop();
     55 
     56       ok="right";}
     57                  else {sum+=flag-left.top();
     58      flag=left.top();
     59 
     60       left.pop();ok="left";}
     61 
     62 
     63              }
     64              else
     65              {
     66                   if(right.top()-flag<flag-left.top())
     67                    {sum+=right.top()-flag;
     68      flag=right.top();
     69 
     70       right.pop();ok="right";}
     71                    else if(right.top()-flag>flag-left.top())
     72                     {sum+=flag-left.top();
     73      flag=left.top();
     74       left.pop();ok="left";}
     75                    else {
     76                     if(ok=="right")
     77                     {
     78                         sum+=right.top()-flag;
     79                       flag=right.top();
     80                     right.pop();
     81 
     82                     }
     83                     else
     84                         {
     85                             sum+=flag-left.top();
     86      flag=left.top();
     87       left.pop();
     88                         }
     89               }
     90    }
     91 
     92  }
     93 
     94  }
     95  cout<<"Case "<<cas++<<": ";
     96 cout<<sum<<endl;
     97 
     98    }
     99     return 0;
    100 
    101 
    102 }
  • 相关阅读:
    51单片机学习笔记(清翔版)(23)——红外通讯
    51单片机学习笔记(清翔版)(22)——数字温度传感器DS18B20
    信号与系统1.1.4信号的分类-信号的MATLAB的表示与绘图
    信号与系统1.1.3信号的分类-能量与功率信号,因果与反因果
    信号与系统1.1.2信号的分类-周期与非周期
    信号与系统1.1.1信号的分类-确定与随机-离散与连续
    51单片机学习笔记(清翔版)(21)——ADDA数模转换
    51单片机学习笔记(清翔版)(19)——串口通信
    ecplise问题总结
    Android广播机制(转)
  • 原文地址:https://www.cnblogs.com/blvt/p/7221578.html
Copyright © 2011-2022 走看看