您的位置:首页 > 其它

2072. Kirill the Gardener 3

2017-02-07 21:39 1036 查看
http://acm.timus.ru/problem.aspx?space=1&num=2072

回忆一下

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <queue>
#include <map>

using namespace std;

struct Tmp
{
Tmp()
{
l = 1000000001;
r = 0;
}
int l;
int r;
};

int main()
{
//freopen("data.in","r",stdin);
int n;
cin>>n;
map<int,Tmp> mt;
for(int i = 1; i <= n; ++i)
{
int k;
cin>>k;
mt[k].l = min(mt[k].l, i);
mt[k].r = max(mt[k].r, i);
}
long long ans = n;

int lastl = 1;
int lastr = 1;
long long LANS = 0;
long long RANS = 0;
for(map<int,Tmp>::iterator it = mt.begin(); it != mt.end(); ++it)
{
Tmp tmp = (it->second);

long long TLN = (tmp.r - tmp.l) + min(abs(tmp.r - lastl) + LANS, abs(tmp.r - lastr) + RANS);
long long TRN = (tmp.r - tmp.l) + min(abs(tmp.l - lastl) + LANS, abs(tmp.l - lastr) + RANS);
LANS = TLN;
RANS = TRN;
lastl = tmp.l;
lastr = tmp.r;
}

ans += min(LANS, RANS);

cout<<ans<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: