# BZOJ 1588 营业额统计 set

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

 1 #include<bits/stdc++.h>
2 #define IOS ios::sync_with_stdio(false);//不可再使用scanf printf
3 #define Max(a, b) ((a) > (b) ? (a) : (b))//禁用于函数，会超时
4 #define Min(a, b) ((a) < (b) ? (a) : (b))
5 #define Mem(a) memset(a, 0, sizeof(a))
6 #define Dis(x, y, x1, y1) ((x - x1) * (x - x1) + (y - y1) * (y - y1))
7 #define MID(l, r) ((l) + ((r) - (l)) / 2)
8 #define lson ((o)<<1)
9 #define rson ((o)<<1|1)
11 using namespace std;
13 {
14     int x=0,f=1;char ch=getchar();
15     while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
16     while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
17     return x*f;
18 }
19
20 typedef long long ll;
21 const int maxn = 1000000 + 10;
22 const int MOD = 1000000007;//const引用更快，宏定义也更快
23 const ll INF = 1e16;
24 const double eps = 1e-6;
25
26 set<int>s;
27 set<int>::iterator it;
28 int main()
29 {
30     int n, x;
31     scanf("%d", &n);
32     int ans = 0, first = 1;
33     while(n--)
34     {
35         scanf("%d", &x);
36         if(s.count(x))continue;
37         s.insert(x);
38         if(first)first = 0, ans = x;
39         else
40         {
41             it = s.find(x);
42             int tmp = 1e9 + 7;
43             if(it != s.begin())
44             {
45                 it--;
46                 tmp = min(tmp, x - (*it));
47                 it++;
48             }
49             it++;
50             if(it != s.end())
51             {
52                 tmp = min(tmp, (*it) - x);
53             }
54             ans += tmp;
55             //cout<<ans<<endl;
56         }
57     }
58     cout<<ans<<endl;
59     return 0;
60 }

posted @ 2018-09-18 22:23  _努力努力再努力x  阅读(205)  评论(0编辑  收藏  举报