• P3052 [USACO12MAR]Cows in a Skyscraper G


    看到这个数据范围,显然状压
    (f[i][j])表示开了i个电梯,装的牛的集合为j,每次往最后一台电梯加入牛,让最后一台电梯的重量为(f[i][j])

    std::mt19937 r(std::chrono::system_clock::now().time_since_epoch().count());
    const int N = 21;
    int n, W, w[N];
    
    namespace DP {
    	int f[19][1 << 19], mx; //¿ªÁËi¸öµçÌÝ£¬Ñ¡ÁËj¼¯ºÏÀïÃæµÄÅ££¬ÖØÁ¿Îªfij
    
    	int main() {
    		memset(f, 0x3f, sizeof f);
    		mx = (1 << n) - 1;
    
    		rep(i, 1, n) {
    			f[1][1 << (i - 1)] = w[i];
    		}
    
    		rep(i, 1, n) { //µçÌÝÊý
    			rep(j, 0, mx) {
    				if(f[i][j] > 1e9) continue;
    				rep(k, 1, n) {
    					if(j & (1 << (k - 1))) continue;
    					if(f[i][j] + w[k] <= W) {
    						f[i][j | 1 << (k - 1)] = min(f[i][j | 1 << (k - 1)], f[i][j] + w[k]);
    					} else {
    						f[i + 1][j | 1 << (k - 1)] = min(f[i][j | 1 << (k - 1)], w[k]);
    					}
    				}
    			}
    		}
    		rep(i, 1, n) {
    			if(f[i][mx] < 1e9) {
    				out(i, '
    ');
    				break;
    			}
    		}
    
    		return 0;
    	}
    
    }
    
    namespace rnd {
    	int main() {
    		int t = clock();
    		int ans(n);
    		
    		while(clock() - t < CLOCKS_PER_SEC * 0.98)
    		{
    			srand(r());
    			int g(0), num(1);
    			rep(i, 1, n) {
    				if(g + w[i] <= W) {
    					g += w[i];
    				} else {
    					g = w[i];
    					++num;
    				}
    			}
    			ans = min(ans, num);
    			std::random_shuffle(w + 1, w + n + 1);
    		}
    		out(ans, '
    ');
    		return 0;
    	}
    }
    

    本文来自博客园,作者:{2519},转载请注明原文链接:https://www.cnblogs.com/QQ2519/p/15466189.html

  • 相关阅读:
    Premetheus告警QQ邮箱
    Prometheus+grafana监控SpringBoot2应用
    Grafana整合Prometheus
    Prometheus:入门初体验
    接口幂等性思路
    OpenFeign远程调用丢失请求头问题解决办法
    gradle构建脚本
    windows安装gradle
    CompletableFuture异步编排
    线程池(ThreadPoolExcutor)基本介绍
  • 原文地址:https://www.cnblogs.com/QQ2519/p/15466189.html
Copyright © 2020-2023  润新知