程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU4302(map的用法)

HDU4302(map的用法)

編輯:C++入門知識

 

[cpp] 
#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; 

#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;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved