zoukankan      html  css  js  c++  java
  • hdu4336 Card Collector MinMax 容斥

    题目传送门

    https://vjudge.net/problem/HDU-4336

    http://acm.hdu.edu.cn/showproblem.php?pid=4336

    题解

    minmax 容斥模板题。

    一个集合 (S) 的至少有一个邮票出现的最早时间是 (frac 1{sumlimits_{iin S} p_i})


    时间复杂度 (O(2^n))

    #include<bits/stdc++.h>
    
    #define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
    #define dbg(...) fprintf(stderr, __VA_ARGS__)
    #define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
    #define fi first
    #define se second
    #define pb push_back
    
    template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
    template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}
    
    typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;
    
    template<typename I> inline void read(I &x) {
    	int f = 0, c;
    	while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
    	x = c & 15;
    	while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
    	f ? x = -x : 0;
    }
    
    #define lowbit(x) ((x) & -(x))
    
    const int N = 20 + 7;
    const int NP = (1 << 20) + 7;
    
    int n, S;
    double p[N], f[NP];
    int pcnt[NP];
    
    inline void work() {
    	S = (1 << n) - 1;
    	double ans = 0;
    	for (int s = 1; s <= S; ++s) f[s] = f[s ^ lowbit(s)] + p[std::__lg(lowbit(s)) + 1], pcnt[s] = pcnt[s ^ lowbit(s)] + 1;
    	for (int s = 1; s <= S; ++s)
    		if (pcnt[s] & 1) ans += 1 / f[s];
    		else ans -= 1 / f[s];
    	printf("%.10lf
    ", ans);
    }
    
    inline void init() {
    	for (int i = 1; i <= n; ++i) scanf("%lf", &p[i]);
    }
    
    int main() {
    #ifdef hzhkk
    	freopen("hkk.in", "r", stdin);
    #endif
    	while (~scanf("%d", &n) && n) {
    		init();
    		work();
    	}
    	fclose(stdin), fclose(stdout);
    	return 0;
    }
    
  • 相关阅读:
    图论:带花树算法-一般图最大匹配
    图论&数学:最小平均值环
    图论:朱刘算法
    图论&动态规划:虚树
    图论:动态点分治
    图论:平面图的对偶图
    图论:DFS序
    打开页面时,所有节点展开(ztree)
    Vue 常用记录
    Vue v-if and v-for
  • 原文地址:https://www.cnblogs.com/hankeke/p/hdu4336.html
Copyright © 2011-2022 走看看