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

Hdoj 4302 Holedox Eating 【multiset】

时间:2015-04-14 21:36:30      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:stl

Holedox Eating
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3430 Accepted Submission(s): 1178

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

Author

BUPT

Source

2012 Multi-University Training Contest 1
题意:在一条直线上,从0开始, 每次都是吃最近的点,每次只能吃一个,在哪个位置吃的,吃过之后就在那个位置上站着。没有饼,就站在原地,如果离左右最近的点距离一样,就按照原来走的方向。
用到一个方法lower_bound(),返回一个大于等于key值的迭代器
代码:

#include <iostream>
#include <cstdio>
#include <set>
using namespace std;

int main(){

    int t, v = 1;
    scanf("%d", &t);
    while(t --){
        int l, n;
        scanf("%d%d", &l, &n);
        multiset<int >a;
        int c, b;
        int sum = 0, cur = 0, dir = 0;
        for(int i = 0; i < n; ++i){
            scanf("%d", &c);
            if(c == 0){
                scanf("%d", &b);
                a.insert(b);
            }
            else{
                if(a.size() == 0) continue;
                multiset<int >::iterator it = a.lower_bound(cur);
                if(it == a.end()){
                        --it;
                        int ww = (*it);
                        dir = -1;
                        sum += (cur - (*it));
                        cur = (*it);
                        a.erase(it);
                }
                else if((*it) == cur){
                        int ww = (*it);
                        //cur = (*it);
                    a.erase(it);
                }
                else{
                    if(it == a.begin()){
                        int ww = (*it);
                        dir = 1;
                        sum += (*it)-cur;
                        cur = (*it);
                        a.erase(it);
                    }
                    else{
                        int ww = (*it);
                        multiset<int >::iterator d = it;
                        --d;
                int qqqq = (*d);
                        int te = (cur - (*d));
                        int temp = ((*it) - cur);
                        if(te == temp){
                            if(dir > 0){
                                sum += temp;
                                cur = (*it);
                                a.erase(it);
                            }
                            else{
                                sum += temp;
                                cur = (*d);
                                a.erase(d);
                            }
                        }
                        else if(te < temp){
                            dir = -1;
                            sum += te;
                            cur = (*d);
                            a.erase(d);
                        }
                        else{
                            dir = 1;
                            sum += temp;
                            cur = (*it);
                            a.erase(it);
                        }
                    }
                }
            }
        }
        printf("Case %d: %d\n", v++, sum);
    }
    return 0;
}

Hdoj 4302 Holedox Eating 【multiset】

标签:stl

原文地址:http://blog.csdn.net/shengweisong/article/details/45047459

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