zoukankan      html  css  js  c++  java
  • MUTC 1 B Holedox Eating STL

    Holedox Eating

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


    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
     

    Recommend
    zhuyuanchen520
     

    -----------------

    STL map中的元素按照从小到大的顺序排序,用迭代器it++和it--到达相邻元素

    -----------------

    #include <iostream>
    #include <cstdio>
    #include <map>
    #include <algorithm>
    #include <cstring>
    
    using namespace std;
    
    const int OO=1e9;
    
    map<int,int>mp;
    map<int,int>::iterator it,itr,itl;
    map<int,int>::iterator p;
    int main()
    {
        //freopen("input.txt","r",stdin);
        int T,len,m,tp,x;
        int dir;
        int ans;
        int cas=1;
        int ke;
        scanf("%d",&T);
        while (T--)
        {
            ans=0;
            ke=0;
            mp.clear();
            scanf("%d%d",&len,&m);
            dir=1;
            mp[OO]=1;
            mp[-OO]=1;
            mp[0]=0;
            p=mp.find(0);
            while (m--)
            {
                scanf("%d",&tp);
                if (tp==0)
                {
                    scanf("%d",&x);
                    mp[x]++;
                    ke++;
                    continue;
                }
                if (ke==0) continue;
                ke--;
                itl=itr=it=p;
                itl--;
                itr++;
                if (p->second>0)
                {
                    p->second--;
                    continue;
                }
                else if ( p->first - itl->first < itr->first - p->first &&
                          itl->first!=OO &&
                          itl->first!=-OO )
                {
                    ans+=p->first - itl->first;
                    p=itl;
                    p->second--;
                    dir=-1;
                }
                else if ( p->first - itl->first > itr->first - p->first &&
                          itr->first!=OO &&
                          itr->first!=-OO )
                {
                    ans+=itr->first - p->first;
                    p=itr;
                    p->second--;
                    dir=1;
                }
                else if ( p->first - itl->first == itr->first - p->first &&
                          itl->first!=OO &&
                          itl->first!=-OO &&
                          itr->first!=OO &&
                          itr->first!=-OO )
                {
                    if (dir==1)
                    {
                        ans+=itr->first - p->first;
                        p=itr;
                        p->second--;
                    }
                    else if (dir==-1)
                    {
                        ans+=p->first - itl->first;
                        p=itl;
                        p->second--;
                    }
                }
                if (it->second==0)
                {
                    mp.erase(it);
                }
            }
            printf("Case %d: %d\n",cas++,ans);
        }
        return 0;
    }
    




  • 相关阅读:
    事务(进程 ID 133)与另一个进程被死锁在 锁 资源上,并且已被选作死锁牺牲品的解决方案
    Waiting for cache lock
    Win11系统将软件安装在C盘后,打开软件会有无法正常读写C盘下文件的问题
    SharePoint 2010 微软学习教程
    Oracle 远程连接配置文件
    如何优化操作大数据量数据库(几十万以上数据)(二。改善SQL语句)
    SQL BI Microsoft MSDN
    Oracle 错误:ORA06413: Connection not open 解决办法
    2008R2Win7管理一创建域和加入域
    学生表 课程表 成绩表 教师表 50个常用sql语句
  • 原文地址:https://www.cnblogs.com/cyendra/p/3226321.html
Copyright © 2011-2022 走看看