LA 3177 Beijing Guards (二分+贪心)

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 were protected by guard towers, and there was a guard living in each tower. The wall can be considered to be a large ring, where every guard tower has exaetly two neighbors.

The guard had to keep an eye on his section of the wall all day, so he had to stay in the tower. This is a very boring job, thus it is important to keep the guards motivated. The best way to motivate a guard is to give him lots of awards. There are several different types of awards that can be given: the Distinguished Service Award, the Nicest Uniform Award, the Master Guard Award, the Superior Eyesight Award, etc. The Central Department of City Guards determined how many awards have to be given to each of the guards. An award can be given to more than one guard. However, you have to pay attention to one thing: you should not give the same award to two neighbors, since a guard cannot be proud of his award if his neighbor already has this award. The task is to write a program that determines how many different types of awards are required to keep all the guards motivated.

Input 

The input contains several blocks of test eases. Each case begins with a line containing a single integer ln100000, the number of guard towers. The next n lines correspond to the n guards: each line contains an integer, the number of awards the guard requires. Each guard requires at least 1, and at most l00000 awards. Guard iand i + 1 are neighbors, they cannot receive the same award. The first guard and the last guard are also neighbors.

The input is terminated by a block with n = 0.

Output 

For each test case, you have to output a line containing a single integer, the minimum number x of award types that allows us to motivate the guards. That is, if we have x types of awards, then we can give as many awards to each guard as he requires, and we can do it in such a way that the same type of award is not given to neighboring guards. A guard can receive only one award from each type.

Sample Input 

3
4
2
2
5
2
2
2
2
2
5
1
1
1
1
1
0

Sample Output 

8
5
3

偶数比较好计算,就是相邻两个人的最大和,奇数不好考虑

以第一个人的值为分界线将总的值分成两部分,偶数人尽量拿前面,奇数人尽量拿后面,尽可能保证最后一个人和第一个人不冲突,这里用两个数组简化中间过程的

演变


#include<iostream>
#include<string.h>
#include<cstdio>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std;
const int N = 2e5+10;
typedef long long LL;
int n, f;
const double pi=acos(-1.0);
int a[N], l[N], r[N];
int judge(int p)
{
    l[1]=a[1], r[1]=0;
    int x=a[1], y=p-a[1];
    //if(y<=0) return 0;
    for(int i=2;i<=n;i++)
    {
        if(i%2==0)
        {
            l[i]=min(x-l[i-1],a[i]);
            r[i]=a[i]-l[i];
        }
        else
        {
            r[i]=min(y-r[i-1],a[i]);
            l[i]=a[i]-r[i];
        }
    }
    return l[n]==0;
}

int main()
{

    while(scanf("%d", &n)==1&&n!=0)
    {
        for(int i=1;i<=n;i++) scanf("%d", &a[i]);
        if(n==1)
        {
            cout<<a[1]<<endl;
            continue;
        }
        int p=0;
        a[0]=a[n];
        for(int i=1;i<=n;i++)
        {
            p=max(p,a[i-1]+a[i]);
        }
        if(n%2==0)
        {
            printf("%d\n",p);
            continue;
        }
        int l=p, r=p*10+10000, mid, ans;
        while(l<=r)
        {
            mid=(l+r)/2;
            if(judge(mid)) ans=mid, r=mid-1;
            else l=mid+1;
        }
        cout<<ans<<endl;
    }
    return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
博物馆守卫问题可以用贪心算法来解决。具体来说,我们可以按照以下步骤来解决这个问题: 1. 将所有的展室按照面积从大到小排序。 2. 从最大的展室开始,依次安排守卫,直到所有的守卫都已经安排完毕为止。 3. 对于每一个展室,我们都选择离该展室最近的尚未被安排守卫的守卫来进行安排。具体来说,我们可以维护一个守卫队列,每次选择队列头部的守卫来进行安排,并将该守卫从队列中删除。 下面是用C++实现的代码: ```c++ #include <iostream> #include <algorithm> #include <queue> #include <vector> using namespace std; const int MAX_N = 1000; // 展室结构体 struct Room { int area; // 展室的面积 int index; // 展室的下标 }; // 按照面积从大到小排序 bool cmp(Room a, Room b) { return a.area > b.area; } // 贪心算法求解 int solve(int n, int m, Room* rooms) { sort(rooms, rooms + n, cmp); // 按照面积从大到小排序 priority_queue<int, vector<int>, greater<int>> guards; // 守卫队列,使用小根堆来维护 for (int i = 0; i < m; i++) { guards.push(i); // 将所有守卫放入队列中 } int ans = 0; // 最小的最大展室面积 for (int i = 0; i < n; i++) { int guard = guards.top(); guards.pop(); ans = max(ans, rooms[i].area); // 更新最小的最大展室面积 // 将该守卫安排到当前展室 cout << "守卫" << guard << "负责展室" << rooms[i].index << endl; // 将该守卫所覆盖的展室标记为已经有守卫了 for (int j = i; j < n; j++) { if (rooms[j].index == rooms[i].index) { rooms[j].index = guard; } } guards.push(guard); // 将该守卫重新放入队列中 } return ans; } int main() { int n, m; Room rooms[MAX_N]; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> rooms[i].area; rooms[i].index = i; } int ans = solve(n, m, rooms); cout << "最小的最大展室面积为:" << ans << endl; return 0; } ``` 注意,在实现中我们将展室按照面积从大到小排序,并且维护一个守卫队列,每次选择队列头部的守卫来进行安排。这个过程的时间复杂度是$O(n \log m)$,其中$n$是展室的数量,$m$是守卫的数量。如果展室数量很大,这个算法的效率可能会比较低。可以尝试使用其他更高效的算法来解决这个问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值