码迷,mamicode.com
首页 > 其他好文 > 详细

E - 贪吃蛇

时间:2017-07-23 15:31:17      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:max   esc   near   ios   div   span   stack   describes   pac   

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. 

InputThe 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. 
OutputOutput 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 <string.h>
  3 #include <set>
  4 #include <stack>
  5 #include <queue>
  6 using namespace std;
  7 
  8 const int MAX = 5*100000 + 2000;
  9 struct cmp
 10 {
 11     bool operator () (int a,int b)
 12     {
 13         return a>b;
 14     }
 15 };
 16 
 17 
 18 
 19 int main()
 20 {
 21    int N;
 22    cin>>N;
 23    int N0 = 0;
 24    while(N--)
 25    {
 26         int x = 0;
 27         int L,n;
 28         int ti = 1;
 29         int sum =0;
 30         priority_queue<int,vector<int>,cmp> p2;
 31         priority_queue<int>p1;
 32 
 33 
 34 
 35        cin>>L>>n;
 36        while(n--)
 37        {
 38            int temp;
 39            cin>>temp;
 40            if(temp == 0)
 41            {
 42                cin>>temp;
 43                if(temp >= x)  { p2.push(temp); }
 44                else { p1.push(temp); }
 45            }
 46            else if(temp == 1)
 47            {
 48                if(!p1.empty()&&!p2.empty())
 49                {
 50                    if(x - p1.top() < p2.top() - x )
 51                    {
 52                        ti = -1;
 53                        sum += x - p1.top();
 54                        x = p1.top();
 55                        p1.pop();
 56                    }
 57                    else if( x - p1.top() > p2.top() - x)
 58                    {
 59                        ti = 1;
 60                        sum +=p2.top() - x;
 61                        x = p2.top();
 62                        p2.pop();
 63 
 64                    }
 65                    else if(x - p1.top() == p2.top() - x)
 66                    {
 67                        if(ti == 1)
 68                        {
 69                            ti = 1;
 70                            sum +=p2.top() - x;
 71                            x = p2.top();
 72                            p2.pop();
 73                        }
 74                        else if( ti == -1)
 75                        {
 76                            ti = -1;
 77                            sum += x - p1.top();
 78                            x = p1.top();
 79                            p1.pop();
 80                        }
 81 
 82                    }
 83 
 84                }
 85                else if(!p1.empty())
 86                {
 87                    ti = -1;
 88                    sum += x - p1.top();
 89                    x = p1.top();
 90                    p1.pop();
 91 
 92                }
 93                else if(!p2.empty())
 94                {
 95                    ti = 1;
 96                    sum +=p2.top() - x;
 97                    x = p2.top();
 98                    p2.pop();
 99                }
100            }
101 
102 
103        // cout<<"x= sum ="<<" "<<x<<" "<<sum<<endl;
104        }
105        cout<<"Case "<<++N0<<": "<<sum<<endl;
106    }
107 
108 
109     return 0;
110 }

 

E - 贪吃蛇

标签:max   esc   near   ios   div   span   stack   describes   pac   

原文地址:http://www.cnblogs.com/a2985812043/p/7224641.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!