zoukankan      html  css  js  c++  java
  • 【multimap的应用】D. Array Division

    http://codeforces.com/contest/808/problem/D

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<string>
     5 #include<algorithm>
     6 #include<cmath>
     7 #include<set>
     8 #include<map> 
     9 #include<utility>
    10 using namespace std;
    11 typedef long long ll;
    12 const int maxn=1e5+2;
    13 ll a[maxn];
    14 ll sum[maxn];
    15 multimap<ll,int> m;
    16 multimap<ll,int> ::iterator it;
    17 pair<multimap<ll,int>::iterator,multimap<ll,int>::iterator> pos; 
    18 int main()
    19 {
    20     int n;
    21     scanf("%d",&n);
    22     memset(sum,0,sizeof(sum));
    23     for(int i=1;i<=n;i++)
    24     {
    25         scanf("%lld",&a[i]);
    26         m.insert(make_pair(a[i],i)); 
    27         sum[i]=sum[i-1]+a[i];
    28     }
    29     if(sum[n]%2==1)
    30     {
    31         printf("NO
    ");
    32         return 0;
    33     }
    34     ll half=sum[n]/2;
    35     for(int i=1;i<=n;i++)
    36     {
    37         ll s=sum[i]-half;
    38         if(s==0)
    39         {
    40             printf("YES
    ");
    41             return 0;
    42         }
    43         else if(s>0)
    44         {
    45             pos=m.equal_range(s);
    46             while(pos.first!=pos.second)    
    47             {
    48                 if(pos.first->second<=i)
    49                 {
    50                     printf("YES
    ");
    51                     return 0;
    52                 }
    53                 pos.first++;
    54             }
    55         }
    56         else 
    57         {
    58             s=-s;
    59             pos=m.equal_range(s);
    60             while(pos.first!=pos.second)
    61             {
    62                 if(pos.first->second>i)
    63                 {
    64                     printf("YES
    ");
    65                     return 0;
    66                 }
    67                 pos.first++;
    68             }
    69         }
    70     }
    71     printf("NO
    ");
    72     
    73     return 0;
    74 }
    View Code

    equal_range的用法:

     //定义pair 对象position; pair数据类型是 2个 multimap<string,string>::iterator 指针。
        pair<multimap<string, string>::iterator, multimap<string, string>::iterator> position;
        //如果键存在,函数返回2个指针,第一个指针指向键第一个匹配的元素
        //第二个指针指向键最后一个匹配的元素的下一位置
        position = author.equal_range(search_item);
        while (position.first != position.second)
        {
            cout << position.first->first << "  " << position.first->second << " ";
            position.first++;
        }
        cout << endl;

    map的find函数只能找key,所以需要把a[i]作为key,但同一个数可以出现多次,所以需要用multimap,multimap内容根据key排序后,equal_range函数key出现的区间。

  • 相关阅读:
    2.分布式锁
    1. junit用法,before,beforeClass,test,after, afterClass的执行顺序
    GC算法
    记一次"截图"功能的前期调研过程!
    程序员转行手册!
    Yarn详细的工作流程
    Yarn的三种调度器(Scheduler)
    Hadoop序列化与Java序列化的区别
    MapReduce执行过程
    从普通登录到单点登录图例
  • 原文地址:https://www.cnblogs.com/itcsl/p/6870278.html
Copyright © 2011-2022 走看看