| 记录编号 | 176856 | 评测结果 | AAAAAAAAAA | 
    
        | 题目名称 | 1533.[HNOI 2002]营业额统计 | 最终得分 | 100 | 
    
        | 用户昵称 |  啊吧啦吧啦吧 | 是否通过 | 通过 | 
    
        | 代码语言 | C++ | 运行时间 | 0.344 s | 
    
        | 提交时间 | 2015-08-10 07:41:23 | 内存使用 | 0.32 MiB | 
    
    
    
    		显示代码纯文本
		
		#include <iostream>
#include <fstream>
#include <cstdio>
#include <set>
#include <climits>
#include <cstdlib>
 
using namespace std;
 
const int MAXN(32768);
int n;
unsigned int ans = 0;
set<int> a;
ofstream fout("turnover.out");
#define cout fout
 
main()
{
	FILE *fin;
	int x;
	fin = fopen("turnover.in", "r");
	fscanf(fin, "%d%d", &n, &x);
	ans = x;
	a.insert(x);
	for (int i = 1; i < n; ++i){
		int tig;
		if (fscanf(fin, "%d", &x) == EOF)
			x = 0;
		tig = INT_MAX;
		if (a.lower_bound(x) != a.end())
			tig = abs(*a.lower_bound(x) - x);
		if (a.lower_bound(x) != a.begin())
			tig = min(tig, abs(*(--a.lower_bound(x)) - x));
		if(++a.lower_bound(x)!=a.end()&&a.lower_bound(x)!=a.end())
			tig = min(tig, abs(*(++a.lower_bound(x)) - x));
		ans += tig;
		a.insert(x);
//		cout << ans << endl;
	}
	fclose(fin);
	
	cout << ans;
	fout.close();
//	for(;;);
}