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;
    }
    
  • 相关阅读:
    Android Studio代码自己主动检測错误提示
    uva 1567
    UWP 新手教程2——怎样实现自适应用户界面
    远程服务的使用场景
    本地服务和远程服务
    本地应用调用远程服务中的方法
    混合方式开启服务
    绑定服务抽取接口
    绑定服务调用服务里的方法
    bind绑定服务的生命周期
  • 原文地址:https://www.cnblogs.com/hankeke/p/hdu4336.html
Copyright © 2011-2022 走看看