HDU 4302 MAP应用

题意:有一个动物在一条长为L的直线上,每次吃离他最近的东西,东西不断刷新,它也不短在吃,N次操作,问他走了多少距离,如有前后距离相同按照上一次走的方向吃。

这题队友map过的,我map不熟,写了一下就当熟悉Map了,因为map内部有序,所以查询当前位置最近的两个点(一前一后),再定义一个方向标记就可以了。

#include <iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<map>
using namespace std;
const int oo=1000000;
int main()
{
    map<int,int>mymap;
    int t,l,n,co,a,b,ca=0;
    map<int,int>::iterator it1,it2;
    scanf("%d",&t);
    while(t--)
    {
        mymap.clear();
        mymap[oo]=1,mymap[-oo]=1;
        scanf("%d%d",&l,&n);
        co=0;
        int ans=0,dir;
        while(n--)
        {
            scanf("%d",&a);
            if(a)
            {
                it1=mymap.lower_bound(co);
                if(it1->first==co)
                {
                    it1->second--;
                    if(it1->second==0) mymap.erase(it1);
                }
                else
                {
                    it1--;
                    it2=mymap.upper_bound(co);
                    if(it1->first==-oo&&it2->first==oo) continue;
                    if(co-it1->first==it2->first-co)
                    {
                        if(dir==1)
                        {
                            it2->second--;
                            ans+=it2->first-co;
                            co=it2->first;
                            if(it2->second==0) mymap.erase(it2);
                        }
                        else
                        {
                            it1->second--;
                            ans+=co-it1->first;
                            co=it1->first;
                            if(it1->second==0) mymap.erase(it1);
                        }
                    }
                    else if(it2->first-co<co-it1->first)
                    {
                        dir=1;
                        ans+=it2->first-co;
                        co=it2->first;
                        it2->second--;
                        if(it2->second==0) mymap.erase(it2);
                    }
                    else
                    {
                        dir=0;
                        ans+=co-it1->first;
                        co=it1->first;
                        it1->second--;
                        if(it1->second==0) mymap.erase(it1);
                    }
                }
            }
            else
                scanf("%d",&b),mymap[b]++;
        }
        printf("Case %d: %d\n",++ca,ans);
    }
    return 0;
}
时间: 2025-01-21 04:50:14

HDU 4302 MAP应用的相关文章

HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)

Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input 输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串. 注意:本题只有一组测试数据,处理到文件结束. Output 对于每个提

HDOJ/HDU 1113 Word Amalgamation(字典顺序~Map)

Problem Description In millions of newspapers across the United States there is a word game called Jumble. The object of this game is to solve a riddle, but in order to find the letters that appear in the answer it is necessary to unscramble four wor

HDOJ/HDU 1075 What Are You Talking About(字符串查找翻译~Map)

Problem Description Ignatius is so lucky that he met a Martian yesterday. But he didn't know the language the Martians use. The Martian gives him a history book of Mars and a dictionary when it leaves. Now Ignatius want to translate the history book

HDOJ(HDU) 2192 MagicBuilding(用Java的Map做了下)

Problem Description As the increase of population, the living space for people is becoming smaller and smaller. In MagicStar the problem is much worse. Dr. Mathematica is trying to save land by clustering buildings and then we call the set of buildin

hdu 1198 Farm Irrigation

hdu 1198 的传送门 Sample Input 2 2 DK HF 3 3 ADC FJK IHE -1 -1 Sample Output 2 3 题目大意:有如上图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇水,问需要打多少口井. 解题思路:用并查集,注意要初始化就好了 #include <iostream> #include <cstring> #include <cstdio> using namespace

HDU 1271 Arbitrage

链接: http://acm.hdu.edu.cn/showproblem.php?pid=1217 题目: Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2594    Accepted Submission(s): 1167 Problem Description Arbitrage is the use of

HDU 1224 Free DIY Tour

链接: http://acm.hdu.edu.cn/showproblem.php?pid=1224 题目: Free DIY Tour Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1894    Accepted Submission(s): 619 Problem Description Weiwei is a software

HDU 1142 A Walk Through the Forest

链接: http://acm.hdu.edu.cn/showproblem.php?pid=1142 题目: A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3689    Accepted Submission(s): 1340 Problem Description Jimmy e

hdu 2112 HDU Today

点击打开链接hdu 2112 思路:最短路 分析:只要把名字映射成整数,然后利用整数去求解即可. 注意事项: 1 题目中的起点和终点可能相同,这个时候输出0. 2 用map映射的时候用char类型,由于string是个类效率比较低. 3 处理成无向图 代码: /*SPFA*/ #include<iostream> #include<algorithm> #include<cstdio> #include<string> #include<cstring