Luogu3694 bonbon chorus stand (like pressure DP)

By the state (from the Go back good length \) \ and (row good team \) \ decided, \ (DP \) equation quite worth thinking of.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long

//#define ON_DEBUG

#ifdef ON_DEBUG

#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x)  cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);

#else

#define D_e_Line ;
#define D_e(x)  ;
#define Pause() ;
#define FileOpen() ;

#endif

struct ios{
    template<typename ATP>ios& operator >> (ATP &x){
        x = 0; int f = 1; char c;
        for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
        while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
        x*= f;
        return *this;
    }
}io;
using namespace std;

int num[21], sum[100007][21];
int bin[21];
int f[(1 << 20) + 7];

int main(){
    int n, m;
    io >> n >> m;
    R(i,1,n){
        int x;
        io >> x;
        R(j,1,m) sum[i][j] = sum[i - 1][j];
        ++num[x];
        ++sum[i][x];
    }
    
    bin[1] = 1;
    R(i,2,m) bin[i] = bin[i - 1] << 1;
    
    Fill(f, 0x3f3f3f3f);
    f[0] = 0;
    
    int maxx = (1 << m) - 1;
    R(i,0,maxx){
        int len = 0;
        R(j,1,m){
            if(i & bin[j]){
                len += num[j];
            }
        }
        R(j,1,m){
            if(i & bin[j]){
                f[i] = Min(f[i], f[i ^ bin[j]] + num[j] - (sum[len][j] - sum[len - num[j]][j]));
            }
        }
    }
    
    printf("%d", f[maxx]);
  
    
    return 0;   
}

Guess you like

Origin www.cnblogs.com/bingoyes/p/11221869.html