• UVa 1335 Beijing Guards (二分+贪心)


    题意:n 个人成一个圈,每个人想要 ri 种不同的礼物,要求相邻两个人没有相同的,求最少需要多少礼物。

    析:如果 n 是偶数,那么答案一定是相邻两个人的礼物总种数之和的最大值,那么如果是奇数,就没那么好做了,我们可以二分答案,

    在每次判定时,我们可以有这样的贪心策略,第一个人 1 - r1,在后面的人中,编号为奇数的尽量往后取,编号为偶数的尽量往前取,

    因为这样我们才能保证第 n 个人和第一个人尽量不相交。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <unordered_map>
    #include <unordered_set>
    #define debug() puts("++++");
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 1e5 + 5;
    const int mod = 2000;
    const int dr[] = {-1, 1, 0, 0};
    const int dc[] = {0, 0, 1, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
    }
    
    int a[maxn];
    int l[maxn], r[maxn];
    
    bool judge(int mid){
        l[1] = a[1], r[1] = 0;
        int x = a[1], y = mid - a[1];
        for(int i = 2; i <= n; ++i){
            if(i & 1){
                r[i] = min(a[i], y-r[i-1]);
                l[i] = a[i] - r[i];
                if(l[i] + l[i-1] > x)  return false;
            }
            else{
                l[i] = min(a[i], x-l[i-1]);
                r[i] = a[i] - l[i];
                if(r[i] + r[i-1] > y)  return false;
            }
        }
        return l[n] == 0;
    }
    
    int solve(){
        if(n & 1){
            int l = 1, r = 5e5;
            while(l < r){
                int mid = (r + l) >> 1;
                if(judge(mid)) r = mid;
                else  l = mid + 1;
            }
            return l;
        }
        int ans = 0;
        for(int i = 1; i <= n; ++i) ans = max(ans, a[i] + a[i+1]);
        return ans;
    }
    
    int main(){
        while(scanf("%d", &n) == 1 && n){
            for(int i = 1; i <= n; ++i)  scanf("%d", a+i);
            if(1 == n){ printf("%d
    ", a[1]);  continue; }
            a[n+1] = a[1];
            printf("%d
    ", solve());
        }
        return 0;
    }
    /*
    9
    8
    15
    35
    16
    21
    90
    55
    50
    32
    */
    
  • 相关阅读:
    iuplua test failure
    lua C++ wrapper
    lua
    Redux系列01:从一个简单例子了解action、store、reducer
    Meteor入门介绍
    Express入门介绍vs实例讲解
    React半科普文
    Express模版引擎hbs备忘
    Gulp:插件编写入门
    gulp.src()内部实现探究
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/6534188.html
Copyright © 2020-2023  润新知