CodeForces-1257E The Contest


传送门:CodeForces-1257E

思路分析

将每个数所在的堆标记
设$dp[i][j]$表示将$i$放进第$j$堆里的最小花费

  1. $dp[i][1]=dp[i-1][1]+1$ 因为把$i$放进第一堆的话,满足前缀,那么$i-1$肯定也在第一堆
  2. $dp[i][2]$$=$$min(dp[i-1][1],dp[i-1][2])$$+1$ 这里没有$i-1$在第三堆,如果$i-1$在第三堆,要满足后缀,$i$必须在第三堆
  3. $dp[i][3]$$=$$min(dp[i-1][1],$min(dp[i-1][2],dp[i-1][3])$)$$+1$ 这个就没有限制了
  4. 最后$dp[i][pos]–$,$pos$是$i$本来的位置

样例输入

2 1 2
3 1
4
2 5

样例输出

1

AC代码

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
#define ls x<<1
#define rs x<<1|1
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))

namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
    bool IOerror=0;
    inline char nc() {
        static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
        if(p1==pend) {
            p1=buf;
            pend=buf+fread(buf,1,BUF_SIZE,stdin);
            if(pend==p1) {
                IOerror=1;
                return -1;
            }
        }
        return *p1++;
    }
    inline bool blank(char ch) {
        return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
    }
    template<class T> inline bool read(T &x) {
        bool sign=0;
        char ch=nc();
        x=0;
        for(; blank(ch); ch=nc());
        if(IOerror)return false;
        if(ch=='-')sign=1,ch=nc();
        for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
        if(sign)x=-x;
        return true;
    }
    template<class T,class... U>bool read(T& h,U&... t) {
        return read(h)&&read(t...);
    }
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;
using namespace __gnu_pbds;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> rbtree;

const int INF = 0x3f3f3f3f;
const int N= 2e5+10;

int dp[N][4],pos[N],a[4];

int main() {

#ifdef xiaofan
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
#endif

    for(int i=1; i<=3; i++) read(a[i]);
    for(int i=1; i<=3; i++) {
        for(int j=1; j<=a[i];j++) {
            int x;
            read(x);
            pos[x]=i;
        }
    }
    int n=a[1]+a[2]+a[3];
    dp[1][1]=dp[1][2]=dp[1][3]=1;
    dp[1][pos[1]]--;
    for(int i=2; i<=n; i++) {
        dp[i][1]=dp[i-1][1]+1;
        dp[i][2]=min(dp[i-1][1],dp[i-1][2])+1;
        dp[i][3]=min(dp[i-1][1],min(dp[i-1][2],dp[i-1][3]))+1;
        dp[i][pos[i]]--;
    }
    cout<<min(dp[n][1],min(dp[n][2],dp[n][3]))<<endl;




    return 0;
}


文章作者: 小凡
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 小凡 !
评论
  目录
隐藏
{% if theme.sakura.enable %}{% endif %}