uva1335 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 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 l n 100000,
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 i and i
+ 1 are neighbors, they cannot receive the same award. The rst 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.

如果n是偶数,那么直接取相邻两个差的最大值,然后奇数尽量往前取,偶数尽量往后取即可。
如果n是奇数的话,1尽量往前取,n就应该尽量往后取,照这样的话1以后的都应该偶数往前取,奇数往后取。但是往后取的时候并不知道应该取到哪,所以需要先二分答案。然后贪心地取一遍验证就行了。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,r[100010],f[100010],g[100010];
int solve0()
{
    int i,ret=0;
    r[n+1]=r[1];
    for (i=1;i<=n;i++)
      ret=max(ret,r[i]+r[i+1]);
    return ret;
}
bool ok(int x)
{
    int i;
    f[1]=r[1];
    g[1]=0;
    for (i=2;i<=n;i++)
    {
        if (f[i-1]+g[i-1]+r[i]>x) return 0;
        if (i&1)
        {
            g[i]=min(x-r[1]-g[i-1],r[i]);
            f[i]=r[i]-g[i];
        }
        else
        {
            f[i]=min(r[1]-f[i-1],r[i]);
            g[i]=r[i]-f[i];
        }
    }
    return !f[n];
}
int solve1()
{
    if (n==1) return r[1];
    int l=0,r=300000,mid;
    while (l<r)
    {
        mid=(l+r)/2;
        if (ok(mid)) r=mid;
        else l=mid+1;
    }
    return l;
}
int main()
{
    int i;
    while (scanf("%d",&n)&&n)
    {
        for (i=1;i<=n;i++)
          scanf("%d",&r[i]);
        printf("%d\n",(n&1)?solve1():solve0());
    }
}
dnSpy是目前业界广泛使用的一款.NET程序的反编译工具,支持32位和64位系统环境。它允许用户查看和编辑.NET汇编和反编译代码,以及调试.NET程序。该工具通常用于程序开发者在维护和调试过程中分析程序代码,尤其在源代码丢失或者无法获取的情况下,dnSpy能提供很大的帮助。 V6.1.8版本的dnSpy是在此系列软件更新迭代中的一个具体版本号,代表着该软件所具备的功能与性能已经达到了一个相对稳定的水平,对于处理.NET程序具有较高的可用性和稳定性。两个版本,即32位的dnSpy-net-win32和64位的dnSpy-net-win64,确保了不同操作系统架构的用户都能使用dnSpy进行软件分析。 32位的系统架构相较于64位,由于其地址空间的限制,只能支持最多4GB的内存空间使用,这在处理大型项目时可能会出现不足。而64位的系统能够支持更大的内存空间,使得在处理大型项目时更为方便。随着计算机硬件的发展,64位系统已经成为了主流,因此64位的dnSpy也更加受开发者欢迎。 压缩包文件名“dnSpy-net-win64.7z”和“dnSpy-net-win32.7z”中的“.7z”表示该压缩包采用了7-Zip压缩格式,它是一种开源的文件压缩软件,以其高压缩比著称。在实际使用dnSpy时,用户需要下载对应架构的压缩包进行解压安装,以确保软件能够正确运行在用户的操作系统上。 dnSpy工具V6.1.8版本的发布,对于.NET程序员而言,无论是32位系统还是64位系统用户,都是一个提升工作效率的好工具。用户可以根据自己计算机的操作系统架构,选择合适的版本进行下载使用。而对于希望进行深度分析.NET程序的开发者来说,这个工具更是不可或缺的利器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值