http://acm.timus.ru/problem.aspx?space=1&num=2072
回忆一下
1 #include <iostream> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <io.h> 5 #include <queue> 6 #include <map> 7 8 using namespace std; 9 10 struct Tmp 11 { 12 Tmp() 13 { 14 l = 1000000001; 15 r = 0; 16 } 17 int l; 18 int r; 19 }; 20 21 int main() 22 { 23 //freopen("data.in","r",stdin); 24 int n; 25 cin>>n; 26 map<int,Tmp> mt; 27 for(int i = 1; i <= n; ++i) 28 { 29 int k; 30 cin>>k; 31 mt[k].l = min(mt[k].l, i); 32 mt[k].r = max(mt[k].r, i); 33 } 34 long long ans = n; 35 36 int lastl = 1; 37 int lastr = 1; 38 long long LANS = 0; 39 long long RANS = 0; 40 for(map<int,Tmp>::iterator it = mt.begin(); it != mt.end(); ++it) 41 { 42 Tmp tmp = (it->second); 43 44 long long TLN = (tmp.r - tmp.l) + min(abs(tmp.r - lastl) + LANS, abs(tmp.r - lastr) + RANS); 45 long long TRN = (tmp.r - tmp.l) + min(abs(tmp.l - lastl) + LANS, abs(tmp.l - lastr) + RANS); 46 LANS = TLN; 47 RANS = TRN; 48 lastl = tmp.l; 49 lastr = tmp.r; 50 } 51 52 ans += min(LANS, RANS); 53 54 cout<<ans<<endl; 55 return 0; 56 }