• Codeforces1458B Glass Half Spilled (DP)


    题意

    (n)个杯子,第(i)个杯子的容量为(a_i),初始时有(b_i)单位的水。

    可以将水在杯子之间转移,转移时会有一半的损耗。

    若允许随意转移,但最终只拿(k)杯水,问(k)个杯子里的水体积和最大能为多少。

    对于(k = 1, 2, ..., n)依次输出答案。

    解法

    (O(n^4))动态规划。

    假设取(k)杯水,那么最优的做法会是将剩下的水都往这(k)杯里倒,直到倒满。所以就是要最大化

    假设(dp_{k, c})表示取(k)杯,杯子总容量为(c),不经过转移的最大水量。那么加上转移操作的最大水量就是(min(c, dp_{c, k} + frac{sum - dp_{c, k}}{2}))

    (dp_{k, c})就类似01背包问题的dp算一下,最后再枚举容量求最终答案即可。

    AC代码

    #include <bits/stdc++.h>
    using namespace std;
     
    using ll = int64_t;
    using ull = uint64_t;
    using VI = vector<int>;
    using VL = vector<ll>;
    using VVI = vector<vector<int>>;
    using VVL = vector<vector<ll>>;
    using PII = pair<int,int>;
    using PLL = pair<ll, ll>;
     
    #define REP(i, _, __) for (int i = (_); i < (__); ++i)
    #define PER(i, _, __) for (int i = (_-1); i >= (__); --i)
    #define FOR(i, _, __) for (int i = (_); i <= (__); ++i)
    #define ROF(i, _, __) for (int i = (_); i >= (__); --i)
    #define FE(v, V) for (const auto& v: V)
    
    #define EB emplace_back
    #define PB push_back
    #define MP make_pair
    #define FI first
    #define SE second
    #define SZ(x) (int((x).size()))
    #define ALL(x) (x).begin(),(x).end()
    #define LLA(x) (x).rbegin(),(x).rend()
    
    const double PI = acos(-1.0);
       
    namespace Backlight {
        const int __BUFFER_SIZE__ = 1 << 20;
        bool NEOF = 1;
        int __top;
        char __buf[__BUFFER_SIZE__], *__p1 = __buf, *__p2 = __buf, __stk[996];
    
        template<typename T>
        T MIN(T a, T b) { return min(a, b); }
    
        template<typename First, typename... Rest>
        First MIN(First f, Rest... r) { return min(f, MIN(r...)); }
    
        template<typename T>
        T MAX(T a, T b) { return max(a, b); }
    
        template<typename First, typename... Rest>
        First MAX(First f, Rest... r) { return max(f, MAX(r...)); }
    
        template<typename T>
        void updMin(T& a, T b) { if (a > b) a = b; }
    
        template<typename T>
        void updMax(T& a, T b) { if (a < b) a = b; }
    
        inline char nc() {
            return __p1 == __p2 && NEOF && (__p2 = (__p1 = __buf) + fread(__buf, 1, __BUFFER_SIZE__, stdin), __p1 == __p2) ? (NEOF = 0, EOF) : *__p1++;
        }
       
        template<typename T>
        inline bool read(T &x) {
            char c = nc();
            bool f = 0; x = 0;
            while (!isdigit(c)) c == '-' && (f = 1), c = nc();
            while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = nc();
            if (f) x = -x;
            return NEOF;
        }
    
        inline bool need(char c) { return (c != '
    ') && (c != ' '); }
    
        inline bool read(char& a) {
            while ((a = nc()) && need(a) && NEOF) ;
            return NEOF;
        }
    
        inline bool read(char *a) {
            while ((*a = nc()) && need(*a) && NEOF) ++a; 
            *a = '';
            return NEOF;
        }
    
        inline bool read(double &x) {
            bool f = 0; char c = nc(); x = 0;
            while (!isdigit(c))  { f |= (c == '-'); c = nc(); }
            while (isdigit(c)) { x = x * 10.0 + (c ^ 48); c = nc(); }
            if (c == '.') {
                double temp = 1; c = nc();
                while (isdigit(c)) { temp = temp / 10.0; x = x + temp * (c ^ 48); c = nc(); }
            }
            if (f) x = -x;
            return NEOF;
        }
    
        template<typename T, typename... T2>
        inline bool read(T &x, T2 &... rest) {
            read(x);
            return read(rest...);
        }
    
        template<typename T>
        inline void print(T x) {
            if (x < 0) putchar('-'), x = -x;
            if (x == 0) { putchar('0'); return; }
            __top = 0;
            while(x) {
                __stk[++__top] = x % 10 + '0';
                x /= 10;
            }
            while(__top) {
                putchar(__stk[__top]);
                --__top;
            }
        }
    
        template<typename First, typename... Rest>
        inline void print(First f, Rest... r) {
            print(f); putchar(' ');
            print(r...);
        }
    
        template<typename T>
        inline void println(T x) {
            print(x); 
            putchar('
    ');
        }
    
        template<typename First, typename... Rest>
        inline void println(First f, Rest... r) {
            print(f); putchar(' ');
            println(r...);
        }
    
        template<typename T>
        inline void _dbg(const char *format, T value) { cerr << format << '=' << value << endl; }
       
        template<typename First, typename... Rest>
        inline void _dbg(const char *format, First f, Rest... r) {
            while(*format != ',') cerr << *format++;
            cerr << '=' << f << ", ";
            _dbg(format + 1, r...);
        }
          
        template<typename T>
        ostream &operator<<(ostream& os, vector<T> V) {
            os << "[ "; for (auto v : V) os << v << ","; return os << " ]";
        }
       
        template<typename T>
        ostream &operator<<(ostream& os, set<T> V) {
            os << "[ "; for (auto v : V) os << v << ","; return os << " ]";
        }
    
        template<typename T>
        ostream &operator<<(ostream& os, multiset<T> V) {
            os << "[ "; for (auto v : V) os << v << ","; return os << " ]";
        }
     
        template<typename T1, typename T2>
        ostream &operator<<(ostream& os, map<T1, T2> V) {
            os << "[ "; for (auto v : V) os << v << ","; return os << " ]";
        }
      
        template<typename L, typename R>
        ostream &operator<<(ostream &os, pair<L, R> P) {
            return os << "(" << P.first << "," << P.second << ")";
        }
    
        #ifdef BACKLIGHT
        #define debug(...) cerr << "33[31m" << "[" << __LINE__ << "] : "; _dbg(#__VA_ARGS__, __VA_ARGS__); cerr << "33[0m";
        // #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__); 
        #else
        #define debug(...)
        #endif
    }
    
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    int rnd(int l, int r) { return l + rng() % (r - l + 1); }
    
    using namespace Backlight;
    const int N = 105;
    const int C = 10000;
    const int K = 1e7 + 5;
    const int MOD = 1e9 + 7;              // 998244353 1e9 + 7
    const int INF = 0x3f3f3f3f;             // 1e9 + 7 0x3f3f3f3f
    const ll LLINF = 0x3f3f3f3f3f3f3f3f;    // 1e18 + 9 0x3f3f3f3f3f3f3f3f
    const double eps = 1e-8;
    
    int n, a[N], b[N], dp[N][N * N];
    void solve(int Case) { // printf("Case #%d: ", Case);
        read(n);
        FOR(i, 1, n) read(a[i], b[i]);
    
        memset(dp, -1, sizeof(dp));
    
        int sum = 0;
        FOR(i, 1, n) sum += b[i];
    
        dp[0][0] = 0;
        FOR(i, 1, n) {
            ROF(j, n, 1) {
                ROF(c, C, a[i]) {
                    if (dp[j - 1][c - a[i]] != -1)
                        dp[j][c] = max(dp[j][c], dp[j - 1][c - a[i]] + b[i]);
                }
            }
        }
    
        FOR(k, 1, n) {
            double ans = 0, x;
            FOR(c, 0, C) {
                if (dp[k][c] != -1) {
                    x = min(double(c), (sum + dp[k][c]) / 2.0);
                    ans = max(ans, x);
                }
            }
            printf("%.12f ", ans);
        }
        puts("");
    }
    
    int main() {
    #ifdef BACKLIGHT
        freopen("in.txt", "r", stdin);
        // freopen("out.txt", "w", stdout);
        auto begin = std::chrono::steady_clock::now();
    #endif
    
        // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
        int T = 1;
        // read(T);
        for (int _ = 1; _ <= T; _++) solve(_);
    
    #ifdef BACKLIGHT
        auto end = std::chrono::steady_clock::now();
        auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin);
        cerr << "33[32mTime Elasped: " << duration.count() << " ms33[0m" << endl;
    #endif
        return 0;
    }
    
  • 相关阅读:
    Linux内核分析期中总结
    Linux内核分析期末总结
    《Linux内核设计与实现》第四章读书笔记
    《Linux内核分析》第八周 进程的切换和系统的一般执行过程
    《深入理解计算机系统》第七章读书笔记
    《Linux内核设计与实现》第三章读书笔记
    "Linux内核分析"第七周
    "Linux内核分析"第六周实验报告
    “Linux内核分析”第五周报告
    Linux实验四报告
  • 原文地址:https://www.cnblogs.com/zengzk/p/14162221.html
Copyright © 2020-2023  润新知