B. Two Cakes

Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2)

题意:两个人都从最左边出发,对于每个人来说都要拿够从 1 - n 的每个数,相邻两个数距离是 1,问最小花费步数可以让两个人全部拿够。

&:排序,依次选取就可以。 

#include
using namespace std;
typedef long long ll;
struct node {
    ll x;
    ll idx;
}s[200005];
bool vis[200005];
bool cmp(struct node a, struct node b)
{
    if(a.x == b.x) return a.idx < b.idx;
    else return a.x < b.x;
}
int main()
{
    ll n;
    while(~scanf("%lld", &n))
    {
        for(ll i = 0; i < 2*n; i ++)
        {
            scanf("%lld", &s[i].x);
            s[i].idx = i;
        }
        sort(s,s+2*n,cmp);
        ll sum = 0;
        memset(vis,false,sizeof(vis));
        ll last = 0;
        ll lasto = 0;
        ll num = 0;
//        for(ll i = 0; i < 2* n; i ++) printf("%lld %lld \n",s[i].x,s[i].idx);
        for(ll i = 0; i < 2*n; i +=2)
        {
            sum += abs(s[i].idx - last);
            last = s[i].idx;
        }
        last = 0;
        for(ll i = 1; i < 2 * n; i += 2){
            sum += abs(s[i].idx - last);
            last = s[i].idx;
        }
        printf("%lld\n", sum);
    }
    return 0;
}

 

你可能感兴趣的:(思维题,/,Codeforces)