B. Two Cakes

2023-03-09 19:35:18 浏览数 (1)

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

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

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

代码语言:javascript复制
#include<bits/stdc  .h>
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("%lldn", sum);
    }
    return 0;
}

0 人点赞