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 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 integerl$ \le$n$ \le$100000, the number of guard towers. The nextnlines correspond to thenguards: each line contains an integer, the number of awards the guard requires. Each guard requires at least 1, and at most l00000 awards. Guardiandi+ 1are neighbors, they cannot receive the same award. The first guard and the last guard are also neighbors.

The input is terminated by a block withn= 0.

Output

For each test case, you have to output a line containing a single integer, the minimum numberxof award types that allows us to motivate the guards. That is, if we havextypes 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

题意:给定n个守卫战成一圈,每个守卫要r个礼物,然后问需要几种礼物分配给他们才能使得相邻两个守卫礼物都不同,

思路:二分+贪心,二分礼物数,贪心:如果是偶数个,答案为max(ri, ri + 1),如果是奇数,先把第一个人放好,然后后面每个人奇数个尽量取前面,偶数个尽量取后面,然后判断最后一个人和第一个人是不是重复即可,用一个left数组和right数组来表示以r【0】为分界线两边取的礼物数,这样一来只要最后一个人的left为0就是可以的。

代码:

#include <stdio.h>
#include <string.h>
#define max(a,b) (a)>(b)?(a):(b)
#define min(a,b) (a)<(b)?(a):(b)
const int N = 100005;


int n, r[N], L, R, left[N], right[N];


void init () {
    L = R = 0;
    for (int i = 0; i < n; i ++)
	scanf("%d", &r[i]);
    for (int i = 0; i < n - 1; i ++)
	L = max(r[i] + r[i + 1], L);
    L = max(r[n - 1] + r[0], L);
}


bool judge(int mid) {
    memset(left, 0, sizeof(left));
    memset(right, 0, sizeof(right));
    if (r[0] > mid) return false;
    left[0] = r[0]; right[0] = 0; int x = r[0], y = mid - r[0], i;
    for (i = 1; i < n; i ++) {
	if (i % 2) {
	    left[i] = min(x - left[i - 1], r[i]);
	    right[i] = r[i] - left[i];
	}
	else {
	    right[i] = min(y - right[i - 1], r[i]);
	    left[i] = r[i] - right[i];
	}
    }
    return left[n - 1] == 0;
}


int solve() {
    if (n == 1)
	return r[0];
    if (n % 2) {
	for (int i = 0; i < n; i ++)
	    R = max(r[i] * 3, R);
	while (L < R) {
	    int mid = (L + R)>>1;
	    if (judge(mid)) R = mid;
	    else L = mid + 1;
	}
	return R;
    }
    return L;
}


int main() {
    while (~scanf("%d", &n) && n) {
	init();
	printf("%d\n", solve());
    }
    return 0;
}


【四轴飞行器】非线性三自由度四轴飞行器模拟器研究(Matlab代码实现)内容概要:本文围绕非线性三自由度四轴飞行器模拟器的研究展开,重点介绍了基于Matlab的建模与仿真方法。通过对四轴飞行器的动力学特性进行分析,构建了非线性状态空间模型,并实现了姿态与位置的动态模拟。研究涵盖了飞行器运动方程的建立、控制系统设计及数值仿真验证等环节,突出非线性系统的精确建模与仿真优势,有助于深入理解飞行器在复杂工况下的行为特征。此外,文中还提到了多种配套技术如PID控制、状态估计与路径规划等,展示了Matlab在航空航天仿真中的综合应用能力。; 适合人群:具备一定自动控制理论基础和Matlab编程能力的高校学生、科研人员及从事无人机系统开发的工程技术人员,尤其适合研究生及以上层次的研究者。; 使用场景及目标:①用于四轴飞行器控制系统的设计与验证,支持算法快速原型开发;②作为教学工具帮助理解非线性动力学系统建模与仿真过程;③支撑科研项目中对飞行器姿态控制、轨迹跟踪等问题的深入研究; 阅读建议:建议读者结合文中提供的Matlab代码进行实践操作,重点关注动力学建模与控制模块的实现细节,同时可延伸学习文档中提及的PID控制、状态估计等相关技术内容,以全面提升系统仿真与分析能力。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值