欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1802B.Settlement of Guinea Pigs
Settlement of Guinea Pigs
Dasha loves guinea pigs very much. In this regard, she decided to settle as many guinea pigs at home as possible and developed a plan for the next days. Every day, she will either buy a new guinea pig or call a doctor to examine all her pets.
Unfortunately, the store where she was going to buy guinea pigs does not understand them. Therefore, it cannot determine their gender. Dasha can't do it either. The only one who can help is a doctor.
To keep guinea pigs, aviaries are needed. Dasha plans to buy them in the same store. Unfortunately, only one species is sold there — a double aviary. No more than two guinea pigs can live in it.
Since Dasha does not want to cause moral injury to her pets — she will not settle two guinea pigs of different genders in one aviary.
Help Dasha calculate how many aviaries in the worst case you need to buy so that you can be sure that at no moment of time do two guinea pigs of different genders live in the same aviary.
As part of this task, we believe that guinea pigs have only two genders — male and female.
Input
The first line of input data contains one number () — the number of input data sets.
The first line of each input data set contains one number () — the number of days Dasha has a plan for.
The next line contains numbers () — Dasha's plan. If , then on the th day, Dasha will buy a new guinea pig. If , then on the th day, a doctor will come to Dasha and help determine the sex of all guinea pigs that Dasha already has.
It is guaranteed that the sum of for all input data sets does not exceed .
Output
For each set of input data, output one number — the minimum number of aviaries Dasha needs to buy so that no matter what the genders of the pigs turn out to be, we can settle them so that at no point in time do two guinea pigs of different genders live together.
Note
In the first set of input data, Dasha needs to put each guinea pig in a separate enclosure, since she does not know their gender.
In the second set of input data, Dasha will buy guinea pigs, which means she will need aviaries.
In the third set of input data, you even need aviaries to put each guinea pig in a separate aviary before the doctor arrives at the th day. When she finds out their gender, at least two guinea pigs will be of the same gender and they can be placed in one aviary, and the third in another aviary. Thus, she will have one free aviary in which she can settle a new guinea pig. So answer is .
In the fourth set of input data, we show that is the optimal answer.
To begin with, we note that the first four guinea pigs can be placed one at a time in an aviary. Then a doctor will come and determine their gender. Among these four guinea pigs there will be at least one pair of the same gender, because: either male guinea pigs are at least , or they are not more than , which means that the female is at least . Now we can put this couple in one aviary, and the other two in separate ones. We will have one more empty aviary where we can put a new pig.
Now let's show that the answer is at least . Let's say that among the first guinea pigs, are female and is male. We need at least aviaries to settle them. Then, when we buy a new guinea pig, we will need another aviary in which we will put it, since we do not know its gender.
Samples
6
3
1 1 1
3
2 2 2
5
1 1 1 2 1
10
1 2 1 2 1 2 1 2 1 2
20
1 2 1 1 1 1 1 2 1 2 1 2 2 1 1 1 1 1 1 1
20
2 1 1 2 1 1 2 1 2 2 1 1 1 2 2 1 1 1 1 2
3
0
3
4
12
9
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |