【luogu P2234 [HNOI2002]營業額統計】 題解
阿新 • • 發佈:2018-06-13
cst check += lse IT ble http int www.
題目鏈接: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; }
【luogu P2234 [HNOI2002]營業額統計】 題解