找出比num小的最大数和比num大的最小数,然后计算差值和最小。只是两个查找的过程,和插入的过程,结果在控制a, b的位置wa了N次,
/*************************************************************************
> File Name: HNOI2002.cpp
> Author: Ac_Toy
> Mail: ycsgldy@163.com
> Created Time: 2013年06月21日 星期五 06时48分17秒
************************************************************************/
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef unsigned int u32;
typedef long long i64;
typedef unsigned long long u64;
typedef vector<int> IV;
typedef vector<bool> BV;
typedef pair<int,int> II;
typedef vector<II> IIV;
#define For(t,v,c) for(t::const_iterator v=c.begin(); v!=c.end(); ++v)
const int INF = 0x7FFFFFFF;
const double eps = 1E-10;
const double PI = acos(-1);
struct Node *null;
int cnt;//序列元素个数
struct Node {
Node *ch[2];
int s;//结点总数
i64 v;
int cmp(int k) const {
int d = k - ch[0]->s;
if ( d == 1 ) return -1;
return d <= 0 ? 0 : 1;
}
void maintain() {
s = ch[0]->s + ch[1]->s + 1;
}
};
void rotate(Node* &o, int d) {//旋转
Node* k = o->ch[d^1]; o->ch[d^1] = k->ch[d]; k->ch[d] = o;
o->maintain(); k->maintain(); o = k;
}
void splay(Node* &o, int k) {//找到序列的左数第k个元素并伸展到根
int d = o->cmp(k);
if(d == 1) k -= o->ch[0]->s + 1;
if(d != -1) {
Node* p = o->ch[d];
int d2 = p->cmp(k);
int k2 = (d2 == 0 ? k : k - p->ch[0]->s - 1);
if(d2 != -1) {
splay(p->ch[d2], k2);
if(d == d2) rotate(o, d^1); else rotate(o->ch[d], d);
}
rotate(o, d^1);
}
}
// 合并left和right。假定left的所有元素比right小。注意right可以是null,但left不可以
Node* merge(Node* left, Node* right) {
splay(left, left->s);
left->ch[1] = right;
left->maintain();
return left;
}
// 把o的前k小结点放在left里,其他的放在right里。1<=k<=o->s。当k=o->s时,right=null
void split(Node* o, int k, Node* &left, Node* &right) {
splay(o, k);
left = o;
right = o->ch[1];
o->ch[1] = null;
left->maintain();
}
const int maxn = 100000 + 10;
int A[1];
struct SplaySequence {
int n;
Node seq[maxn];
Node *root;
Node* build(int sz) {
if(!sz) return null;
Node* L = build(sz/2);
Node* o = &seq[++n];
o->v = A[n-1];//结点编号;A[0]是虚拟结点
o->ch[0] = L;
o->ch[1] = build(sz - sz/2 - 1);
o->s = 0;
o->maintain();
return o;
}
void init(int sz) {
n = 0;
null = new Node();
null->s = 0, null->v = INF;
root = build(sz);
}
};
vector<int> ans;
void debug(Node* o) {
if(o != null) {
debug(o->ch[0]);
printf ( "%d ", o->v );
//printf ( " v=%d,s=%d,flip=%d,lazy=%d,mi=%d", o->v,o->s,o->flip,o->lazy2,o->mi);
//printf ( "%d, left=%d,s=%d,mi=%d,*right=%d,s=%d,mi=%d\n", o->v, o->ch[0]->v,o->ch[0]->s,o->ch[0]->mi, o->ch[1]->v,o->ch[1]->s,o->ch[1]->mi );
debug(o->ch[1]);
}
}
SplaySequence ss;
int find_low(int x) {
Node *p = ss.root, *ret = null;
while(p != null) {
if(p->v <= x) ret = p, p = p->ch[1];
else p = p->ch[0];
}
return ret->v;
}
int find_up(int x) {
Node *p = ss.root, *ret = null;
while(p != null) {
if(p->v >= x) ret = p, p = p->ch[0];
else p = p->ch[1];
}
return ret->v;
}
void add(int x) {
Node *p = ss.root;
int k = 0;
while(p != null) {
if(p->v > x) p = p->ch[0];
else {
k += p->ch[0]->s + 1;
p = p->ch[1];
}
}
Node *left, *right;
split(ss.root, k, left, right);
Node *mid = new Node();
mid->ch[0] = mid->ch[1] = null;
mid->s = 1; mid->v = x;
ss.root = merge(merge(left, mid), right);
}
int main()
{
int Case, num, ans(0);
scanf("%d", &Case);
A[0] = -INF;
ss.init(1);
for(int i = 1; i <= Case; ++i) {
if(scanf("%d",&num) == EOF) num = 0;
int tmp = INF;
int a = find_low(num), b = find_up(num);
if(a != -INF) tmp = min(tmp, num - a);
if(b != INF) tmp = min(tmp, b - num);
if(tmp == INF) tmp = num;
ans += tmp;
if(tmp != 0) add(num);
}
printf("%d\n", ans);
return 0;
}