FZU 1492 地震预测(链表)
实际上把数组排序一遍加入链表中,再记录好数组原来的数在链表中的位置。我们只需要维护链表的删除操作就可以了。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# include <cstdio> # include <cstring> # include <cstdlib> # include <iostream> # include <vector> # include <queue> # include <stack> # include <map> # include <set> # include <cmath> # include <algorithm> using namespace std; # define lowbit(x) ((x)&(-x)) # define pi 3.1415926535 # define eps 1e-9 # define MOD 1000000009 # define INF 1000000000 # define mem(a,b) memset(a,b,sizeof(a)) # define FOR(i,a,n) for(int i=a; i<=n; ++i) # define FO(i,a,n) for(int i=a; i<n; ++i) # define bug puts("H"); # define lch p<<1,l,mid # define rch p<<1|1,mid+1,r # define mp make_pair # define pb push_back typedef pair<int,int> PII; typedef vector<int> VI; # pragma comment(linker, "/STACK:1024000000,1024000000") typedef long long LL; int Scan() { int res=0, flag=0; char ch; if((ch=getchar())=='-') flag=1; else if(ch>='0'&&ch<='9') res=ch-'0'; while((ch=getchar())>='0'&&ch<='9') res=res*10+(ch-'0'); return flag?-res:res; } void Out(int a) { if(a<0) {putchar('-'); a=-a;} if(a>=10) Out(a/10); putchar(a%10+'0'); } const int N=100005; //Code begin... struct List{int cur, pre, suc;}list[N]; int a[N], b[N]; bool comp(int x, int y){return a[x]<a[y];} int main () { int n; while (~scanf("%d",&n)) { FOR(i,1,n) scanf("%d",a+i), b[i]=i; a[0]=a[n+1]=INF; int sum=a[n]; sort(b+1,b+n+1,comp); FOR(i,1,n) list[b[i]].cur=i, list[i].pre=i-1, list[i].suc=i+1; FO(i,1,n) { int j=list[i].cur; sum+=min(abs(a[i]-a[b[list[j].pre]]), abs(a[i]-a[b[list[j].suc]])); list[list[j].pre].suc=list[j].suc; list[list[j].suc].pre=list[j].pre; } printf("%d\n",sum); } return 0; }