UVA1335-- Beijing Guards

题意:有n个人围成一个圈,其中第i个人想要ri个不同的礼物。求最少需要多少种礼物,使得相邻的人的礼物都不相同。

思路:这是大白上面的一道贪心题目。想法挺好的。

首先如果n为偶数时,只要找出相邻两个人的r值最大,就是所需的最少的礼物数量。如果为奇数时,那情况就不一样了,因为当第1个和第n个都是奇数,按照上面的方法,他们的礼物种类是一样的,就不符合题意。那么我们可以按照第一个人所需要的礼物数量为基准划分区间。假设最坏情况下的礼物总数为R = max(ri) * 3,所以区间划分为【1,r1】,【r1 + 1,R】。所以只要记录每个人在这两个区间内取了几个,分别用front[i],back[i]表示。最后在判断是否符合,也就是第n个人是否在【1,r1】内取礼物。注意特判n
== 1时的情况。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAXN = 100010;

int arr[MAXN], front[MAXN], back[MAXN];
int n;

int judge(int num) {
    int x = arr[1], y = num - arr[1];
    front[1] = x;
    back[1] = 0;
    for (int i = 2; i <= n; i++) {
        if (i % 2 == 1) {
            back[i] = min(y - back[i - 1], arr[i]); //尽量往后取
            front[i] = arr[i] - back[i];
        }
        else {
            front[i] = min(x - front[i - 1], arr[i]); //尽量往前取
            back[i] = arr[i] - front[i];
        }
    }
    return front[n] == 0;
}

int main() {
    while (scanf("%d", &n) == 1 && n) {
        for (int i = 1; i <= n; i++)
            scanf("%d", &arr[i]);

        if (n == 1) {
            printf("%d\n", arr[1]);
            continue;
        } 

        arr[n + 1] = arr[1];
        int L = 0, R = 0;
        for (int i = 1; i <= n; i++)
            L = max(L, arr[i] + arr[i + 1]);

        memset(front, 0,sizeof(front));
        memset(back, 0,sizeof(back));
        if (n % 2 == 1) {
            for (int i = 1; i <= n; i++)
                R = max(R, arr[i] * 3);
            while (L < R) {
                int mid = L + (R - L) / 2;
                if (judge(mid))
                    R = mid;
                else
                    L = mid + 1;
            }
        }
        printf("%d\n", L);
    }
    return 0;
}

UVA1335-- Beijing Guards

时间: 2024-08-03 07:44:02

UVA1335-- Beijing Guards的相关文章

Live Archive 3177 3177 - Beijing Guards 【枚举】

3177 - Beijing Guards Time limit: 3.000 seconds Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the Imperial City Wall, the Inner City Wall, and finally the Outer City Wall. Most of these walls were demolished in the

uva 1335 Beijing Guards(二分)

uva 1335 Beijing Guards Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the Imperial City Wall, the Inner City Wall, and finally the Outer City Wall. Most of these walls were demolished in the 50s and 60s to make way

UVALIVE 3177 Beijing Guards

Description Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the Imperial City Wall, the Inner City Wall, and finally the Outer City Wall. Most of these walls were demolished in the 50s and 60s to make way for roads.

【二分答案+贪心】UVa 1335 - Beijing Guards

Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the Imperial City Wall, the Inner City Wall, and finally the Outer City Wall. Most of these walls were demolished in the 50s and 60s to make way for roads. The walls we

AYITACM2016省赛第三周M - Beijing Guards(贪心+二分)

Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the Imperial City Wall, the Inner City Wall, and ?nally the Outer City Wall. Most of these walls were demolished in the 50s and 60s to make way for roads. The walls wer

[UVa 1335]Beijing Guards

题解 拿到题,没什么头绪,我们模拟一下,很容易得出一个结论: 如果$n$为偶数,那么答案就是$max(r[i]+r[i+1])$.具体方案就是,如果$i$为奇数,那么取$[1,r[i]]$:若$i$为偶数,则取$[ans-r[i]+1,ans]$. 同样我们此时的$ans$是答案的下界. 可当$n$为奇数时,我们这样贪心策略出现问题就是由于$1$和$n$都是奇数,会取$[1,r[1]]$,$[1,r[n]]$显然会重复,那么奇数我们就不能这样做了. 我们由$n$为偶数的思想,我们还是让$1$号取

Uva 长城守卫——1335 - Beijing Guards

二分查找+一定的技巧 1 #include<iostream> 2 using namespace std; 3 4 const int maxn=100000+10; 5 int n,r[maxn],Left[maxn],Right[maxn];//因为不用计算方案,所以可以按[1-r[i]]和[r[i]+1~p]中各拿几个分,当时没想到这个用set类写了个超耗时间的~~~~(>_<)~~~~ 6 7 bool ok(int p) 8 { 9 int x=r[1],y=p-r[1

LA 3177 Beijing Guards(长城守卫)

贪心找最优策略.大白上有详解. 不过这里有几个值得思考的地方. 还有几个地方想不明白,脑子不够用了,先记着,改天再做一遍 #include <cstdio> #include <algorithm> using namespace std; const int maxn = 100005; int n,a[maxn],left[maxn],right[maxn]; bool ok(int p) { int x = a[1],y = p-a[1]; left[1] = a[1];ri

hbx的毒瘤贪心系列题解

毒瘤hbx的贪心专题系列题解 A Maximal gcd 题意:现在给定一个正整数 n.你需要找到 k 个严格递增的正整数a1,?a2,?...,?ak,满足他们的和等于 n 并且他们的最大公因数尽量大.如果不可能请输出 -1.\(1\leq n,k \leq 10^{10}\) 题解:把 n 的所有因子找出来后,求最大因子 x 满足\(x* \frac {k* (k+1)}{2}\leq n\)即可.序列就是\(1* x,2* x,...,(k-1)* x,n-x* \frac{k* (k-1

2659: [Beijing wc2012]算不出的算式

2659: [Beijing wc2012]算不出的算式 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 757  Solved: 425[Submit][Status] Description 算不出的算式背景:              曾经有一个老掉牙的游戏放在我面前,我没有珍惜.直到这个游戏停产才追悔莫及.人世间最痛苦的事情莫过于此,如果上天给我一个再玩一次的机会,我一定要,通关!题目描述:       如果你真的很想玩这个游戏,那么就先看看