【luogu P2234 [HNOI2002]营业额统计】 题解

题目链接:https://www.luogu.org/problemnew/show/P2234

本来是一道打算练习splay的题目

发现暴力可以过啊。。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#define inf 0x7fffffff
#define ri register
using namespace std;
int n,answer;
struct date{
    int v, pos;
}a[132768];
bool cmp(date a, date b)
{
    return a.v < b.v;
}
int check(int x)
{
    int ans1 = inf,ans2 = inf,ans;
    if(a[x].pos == 1) return a[x].v;
    for(ri int i = x+1; i <= n; i++)
    {
        if(a[i].pos < a[x].pos)
        {
            ans1 = abs(a[i].v-a[x].v);
            break;
        }
    }
    for(ri int i = x-1; i >= 1; i--)
    {
        if(a[i].pos < a[x].pos)
        {
            ans2 = abs(a[i].v-a[x].v);
            break;
        }
    }
    if(ans1<ans2)
    return ans1;
    else
    return ans2;
}
int main()
{
    scanf("%d",&n);
    for(ri int i = 1; i <= n; i++)
    {
        scanf("%d",&a[i].v);
        a[i].pos = i;
    }
    sort(a+1,a+1+n,cmp);
    for(ri int i = 1; i <= n; i++)
    {
        answer+=check(i);
    }
    printf("%d",answer);
    return 0;
}
posted @ 2018-06-13 19:28  Misaka_Azusa  阅读(178)  评论(0编辑  收藏  举报
Live2D