首页 > 代码库 > HYSBZ_1588_set

HYSBZ_1588_set

http://www.lydsy.com/JudgeOnline/problem.php?id=1588

 

直接set做就好了,注意set的迭代器只能--之类的操作。

OJ崩了,也不知道对错。

 

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<set>using namespace std;set<int> s;set<int>::iterator it;int main(){    int temp,n;    scanf("%d",&n);    scanf("%d",&temp);    int sum = temp;    s.insert(temp);    for(int i = 2;i <= n;i++)    {        scanf("%d",&temp);        it = s.lower_bound(temp);        if(it == s.begin()) sum += *it-temp;        else if(it == s.end())  sum += temp-*(--it);        else        {            int a = *it-temp,b = temp-*(--it);            sum += min(a,b);        }        s.insert(temp);    }    printf("%d\n",sum);    return 0;}

 

HYSBZ_1588_set