Problem - 1172A - Codeforces(思维+模拟)

Problem - A - Codeforces

题目大意:有 2 n 2n 2n张牌,其中有 n n n张牌是 n n n的排列,剩余 n n n张牌是 0 0 0,现在将牌打乱平分成两份,你可以将自己手上的牌放在另一堆牌的牌顶,然后拿走牌底的牌,求最少需要多少次这样的操作,能让牌堆形成一个递增序列.

解题思路:先考虑最坏的情况,最坏的情况就是先把手上的 0 0 0全部打出去,然后把对应的数字牌全部收回,再依次的放,这样次数是 2 n 2n 2n.然后我们考虑把一张牌放到对应位置所需要的花费.
如果一张牌在牌堆 b b b中满足, b i < i b_i<i bi<i,那么将这张牌放置到对应的位置只需要让它前进几步花费是 i − b i i-b_i ibi.
如果一张牌不能只通过前进来满足条件的话,就必须先回到手牌中,然后再放到对应的位置,这样对应的花费就是 i + n − b i + 1 i+n-b_i+1 i+nbi+1.
得知这两种花费之后,我们去模拟这个过程即可.

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define syncfalse ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
const int N = 2e5+5;
int a[N], b[N], pos[N], cnt[N];
int main(){
    syncfalse
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    int n;
    cin>>n;
    for (int i = 1; i <= n; ++i)cin>>a[i];
    for (int i = 1; i <= n; ++i){
        cin>>b[i];
        if (b[i]>0)pos[b[i]]=i;
    }
    for (int i = 1; i <= n; ++i){
        if (pos[i]<i)cnt[i]=pos[i]+n-i+1;
        else cnt[i]=pos[i]-i;
    }
    int ans = 0;
    for (int i = 1; i <= n; ++i)ans=max(ans, cnt[i]);
    bool flag = true;
    while(flag){
        flag=false;
        for (int i = 1; i <= n; ++i){
            if (pos[i]==0)continue;
            if (ans>cnt[i])cnt[i]=pos[i]+n-i+1;  //当前必须先回手才能进行下一步操作.
            if (ans<cnt[i]){
                ans=cnt[i];flag=true;
            }
        }
    }
    cout << ans << "\n";

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值