Gym 240668E - Anton and Ira - [贪心]

题目链接:https://codeforces.com/gym/240668/problem/E

题意:

给两个 $1 \sim n$ 的排列 $p,s$,交换 $p_i,p_j$ 两个元素需要花费 $|i-j|$,要求你用最少的钱交换 $p$ 中的元素使得其变成 $s$。

题解:

思路很简单,给个例子如下:

$p$:$5,1,2,3,4,6$

$s$:$3,4,1,6,2,5$

我们不难发现,像:

$p$:$5,1,\underline{2},\underline{3},4,6$

$s$:$\underline{3},4,1,6,\underline{2},5$

或者

$p$:$5,1,\underline{2},3,\underline{4},6$

$s$:$3,\underline{4},1,6,\underline{2},5$

这样的情况,交换两个数字必定是不会亏的,而且是必须要花费的,所以每次都直接暴力枚举找符合的 $(i,j)$,一旦遇到就立刻把它俩交换即可。

当然不能太过暴力地 $O(n^2)$ 去找 $(i,j)$,需要一点剪枝和区间控制。

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
#define mk(x,y) make_pair(x,y)
#define fi first
#define se second
const int maxn=2e3+10;
int n;
int s[maxn],t[maxn];
int pos[maxn];

P Find()
{
    int x,y;
    for(int i=1;i<=n;i++)
    {
        x=pos[s[i]];
        if(i<x)
        {
            for(int j=i+1;j<=x;j++)
            {
                y=pos[s[j]];
                if(y<=i) return mk(i,j);
            }
        }
    }
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    cin>>n;
    for(int i=1;i<=n;i++) cin>>s[i];
    for(int i=1;i<=n;i++) cin>>t[i], pos[t[i]]=i;

    int diff=0;
    for(int i=1;i<=n;i++) diff+=(s[i]!=t[i]);

    vector<P> op;
    int cost=0;
    while(diff)
    {
        P r=Find();
        swap(s[r.fi],s[r.se]);
        //printf("%d <-> %d\n",r.fi,r.se);
        op.push_back(r);
        cost+=abs(r.fi-r.se);
        diff-=(s[r.fi]==t[r.fi]);
        diff-=(s[r.se]==t[r.se]);
    }

    cout<<cost<<‘\n‘;
    cout<<op.size()<<‘\n‘;
    for(auto x:op) cout<<x.fi<<" "<<x.se<<‘\n‘;
}

原文地址:https://www.cnblogs.com/dilthey/p/10543258.html

时间: 2024-10-12 11:14:18

Gym 240668E - Anton and Ira - [贪心]的相关文章

Codeforces Round #329 (Div. 2)B. Anton and Lines 贪心

B. Anton and Lines The teacher gave Anton a large geometry homework, but he didn't do it (as usual) as he participated in a regular round on Codeforces. In the task he was given a set of n lines defined by the equations y = ki·x + bi. It was necessar

Gym 100886J Sockets 二分答案 + 贪心

Description standard input/outputStatements Valera has only one electrical socket in his flat. He also has m devices which require electricity to work. He's got n plug multipliers to plug the devices, the i-th plug multiplier has ai sockets. A device

codeforces 584E Anton and Ira [想法题]

题意简述: 给定一个$1$到$n(n<=2000)$的初始排列以及最终排列 我们每次可以选取位置为$i$和$j$的 并交换它们的位置 花费为$ |i-j| $ 求从初始状态变换到末状态所需最小花费 ----------------------------------------------------------------------------------------------------------- 比赛时这题留了$40min$ 然而自己贪心策略还是有漏洞 结束后看了首页的官方题解 感

2016&quot;百度之星&quot; - 初赛(Astar Round2A)1006 Gym Class(HDU5695)——贪心+拓扑排序

分析:首先,利用贪心可知,如果要所有人的分数和最高,需要把序号大的优先放在前面.其次,对于a的前面不能为b,那么只能a在b前面了,那么就建立一条从a到b的边,并且b的入度加1.然后就是拓扑排序了.要分数最高,则把哪些入度为0的点(他们不需要有哪些人一定要在他们前面,最自由)丢进优先队列,然后就可以实现把序号大的尽量放在前面而且满足题意了. 具体见代码: 1 #include <stdio.h> 2 #include <algorithm> 3 #include <string

Gym - 101806T: Touch The Sky(贪心)

Figure: The house floats up in the sky by balloons. This picture is also used in 2018 KAIST RUN Spring Contest poster. In the year 2117, Professor Jaemin Yu developed a linear-time algorithm for TSP(Traveling Salesperson Problem). Not long after that

Gym 240668 - A/B/C/D/E - (Done)

链接:https://codeforces.com/gym/240668 A - Olesya and Rodion - [水] 题解:注意到 $t$ 的范围是 $[2,10]$,对于位数小于 $2 \times 3 \times \cdots \times 10 = 3628800$ 的数,暴力枚举去找:否则就直接在 $3628800$ 后面补零即可. AC代码: #include<bits/stdc++.h> using namespace std; int n,t; int p10[8]

Codeforces Round #324 (Div. 2)

今天写写cf上以前的水题,找找自信 A. Olesya and Rodion 此题要求一个能被t整除的n位数,直接t为开始,后面全部为0. 当然,需要排除位数为1但t=10的情况. #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int main() { int n,t,i; scanf("%d%d"

codeforces Gym 100187F F - Doomsday 区间覆盖贪心

F. Doomsday Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/F Description Doomsday comes in t units of time. In anticipation of such a significant event n people prepared m vaults in which, as they think, it will

hdu-5695 Gym Class(贪心+拓扑排序)

题目链接: Gym Class Time Limit: 6000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Problem Description 众所周知,度度熊喜欢各类体育活动. 今天,它终于当上了梦寐以求的体育课老师.第一次课上,它发现一个有趣的事情.在上课之前,所有同学要排成一列, 假设最开始每个人有一个唯一的ID,从1到N,在排好队之后,每个同学会找出包括自己在内的前方所有同学的最小ID,作为