构造+贪心 Codeforces584E Anton and Ira

传送门:点击打开链接

题意:给你一个1~n的排列s1,和另一个排列s2,要把s1变成s2,只能交换数字,交换数字的代价是两个数字位置之差,求最小代价

思路:首先把题目变换一下,,变成已知原串s3,要变成1,2,3,...,n-1,n的排列的最小代价,,可以通过s1和s2得到s3

之后的操作,就变得十分技巧。首先,我们能发现,如果交换的两个数字,都是朝着各自的位置前进,那么就一定是最优的。那么现在就算有这个思路,复杂度也是O(n^3),有没有办法能使复杂度降低,,使用一些数据结构当然是可以的,,不过我们这里有更简单的方法。

假如每次我都是取最小的还没有还原的数,然后往前去交换,慢慢的让它还原到原来的位置,那么这样一定是最优的,接下来我们就证明


如果我取的数字是x,现在这个x在p位置上

如果p<x,按照我的步骤,前x-1已经全部还原了,那么我x的位置至少应该是>=x的,所以这种情况是不可能出现的

如果p=x,那么已经还原了,就不需要处理了

那么必然有p>x,再看,因为1~x-1这些数字肯定是已经还原了,那么换句话说,比x小的数字已经全部还原了,现在在x前面的数字个数,就是p-1,那么p-1个数中又有x-1个数字比x小,又因为p-1>=x,p-1-(x-1)>=1,所以,在区间[x,p-1]中间至少会存在一个数字>=p!

所以复杂度就简化到了O(n^2),就在可承受范围内了~

#include<map>
#include<set>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define fuck(x) cout<<"["<<x<<"]"
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w+",stdout)
using namespace std;
typedef long long LL;
typedef pair<int, int>PII;

const int MX = 2e3 + 5;

int A[MX], B[MX];
vector<PII>pr;

int main() {
    int n; //FIN;
    while(~scanf("%d", &n)) {
        pr.clear();

        for(int i = 1; i <= n; i++) {
            int t;
            scanf("%d", &t);
            A[t] = i;
        }
        for(int i = 1; i <= n; i++) {
            int t;
            scanf("%d", &t);
            B[A[t]] = i;
        }

        int ans = 0;
        while(true) {
            int pos = -1;
            for(int j = 1; j <= n; j++) {
                if(B[j] != j && (pos == -1 || B[j] < B[pos])) {
                    pos = j;
                }
            }
            if(pos == -1) break;

            while(B[pos] != pos) {
                for(int j = pos - 1; j >= 1; j--) {
                    if(B[j] >= pos) {
                        pr.push_back(PII(j, pos));
                        swap(B[pos], B[j]);
                        ans += pos - j;
                        pos = j;
                        break;
                    }
                }
            }
        }

        printf("%d\n%d\n", ans, (int)pr.size());
        for(int i = 0; i < (int)pr.size(); i++) {
            printf("%d %d\n", pr[i].first, pr[i].second);
        }
    }
    return 0;
}


### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值