ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Codeforces Round #743 (Div. 2)

2021-09-20 11:05:54  阅读:209  来源: 互联网

标签:743 dist int res Codeforces second Div include first


B. Swaps

思路:

把a,b数组开成pair,first存值,second存下标,然后把两个数组排序,由于在同一位置下,a数组永远小于b数组,所以我们每到一个位置,就取一个min的dist(a[i]到开头的距离),保证a[i]的位置尽量靠前,然后再取一个min的res,由于下标从1开始,所以res取最小的b.second + dist - 2

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>

using namespace std;

typedef pair<int, int>PII;
typedef long long LL;

const int N = 100010;

PII a[N], b[N];

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        int n;
        cin >> n;
        for (int i = 1; i <= n; i++) cin >> a[i].first, a[i].second = i;
        for (int i = 1; i <= n; i++) cin >> b[i].first, b[i].second = i;

        sort(a + 1, a + 1 + n);
        sort(b + 1, b + 1 + n);

        int dist = 0x3f3f3f3f, res = 0x3f3f3f3f;
        for (int i = 1; i <= n; i++)
        {
            dist = min(dist, a[i].second);//随着i的增加,a[i]要保证位置要尽量靠前
            res = min(res, b[i].second + dist - 2);
        }

        cout << res << endl;
    }
    return 0;

}

//3(1) 1(2)
//4(1) 2(2)
//
//1(2) 3(1)
//2(2) 4(1)
//
//7(1) 5(2) 9(3) 1(4) 3(5)
//2(1) 4(2) 6(3) 10(4) 8(5)
//
//1(4) 3(5) 5(2) 7(1) 9(3)
//2(1) 4(2) 6(3) 8(5) 10(4)

 

标签:743,dist,int,res,Codeforces,second,Div,include,first
来源: https://www.cnblogs.com/yctql/p/15313844.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有