zoukankan      html  css  js  c++  java
  • Luogu3694 邦邦的大合唱站队 (状压DP)

    状态由(从前往后排好的长度)(排好的团队)决定,(DP)方程挺有思考价值的。

    #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("
    
    ----------
    
    ")
    #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;	
    }
    

  • 相关阅读:
    设计模式之里氏替换原则
    设计模式之依赖倒置原则讲解
    条款10 若不想使用编译器自动生成的函数,就该明确拒绝
    Django---常用字段和参数
    Python中abc
    Python中鸭子类型
    Python多继承的正确打开方式:mixins机制
    python新式类和经典类的区别
    Django---drf权限、频率、过滤、排序、异常处理
    删库跑路技巧 删库跑路命令
  • 原文地址:https://www.cnblogs.com/bingoyes/p/11221869.html
Copyright © 2011-2022 走看看