一个值得纪念的状压问题。

在这里插入图片描述

//柳总牛批
#include <bits/stdc++.h>
#define P pair<int,int>
#define fr first
#define se second
using namespace std;
typedef long long ll;
const int maxx=20;
const int inf=0x3f3f3f3f;
const ll mod=1e9+7;
int a[maxx],b[maxx];
int dp[1<<19][maxx];
vector<int> aset;
int main(){
    int n; cin>>n;
    for(int i=0;i<n;i++) cin>>a[i];
    for(int i=0;i<n;i++) cin>>b[i];
    memset(dp,0x3f,sizeof dp);
    int ed = 1<<n;
    for(int st=1;st<ed;st++){
        aset.clear();
        for(int i=0;i<n;i++)
            if((st>>i)&1) aset.push_back(i);
        if(aset.size()==1){
            dp[st][aset[0]]=aset[0];
            continue;
        }
        int num=aset.size()-1;
        for(auto x:ase t){
            int cnt=0;
            for(auto y:aset) cnt+=(y>x);
            int pre=st^(1<<x);
            for(auto y:aset){
                if(x==y) continue;
                int vx,vy;
                vy=((num&1)!=(y&1))?a[y]:b[y];
                vx=((num&1)==(x&1))?a[x]:b[x];
                if(vy>vx) continue;
                dp[st][x]=min(dp[st][x],dp[pre][y]+(x+cnt-num));
            }
        }
    }
    int ans=inf;
    for(int i=0;i<n;i++) ans=min(ans,dp[ed-1][i]);
    if(ans==inf) puts("-1");
    else cout<<ans<<endl;
}

大致思路:dp[i][j]记录当前选中了i状态,最后为编号j。转移选中从一个被选中的,用奇偶判断表现出来的值,判断合法。 合法了就转移。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值