B. Settlement of Guinea Pigs(CF)(div2)

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 𝑡(1⩽𝑡⩽105) — the number of input data sets.

The first line of each input data set contains one number 𝑛 (1⩽𝑛⩽105) — the number of days Dasha has a plan for.

The next line contains 𝑛 numbers 𝑏1,𝑏2,𝑏3,…,𝑏𝑛 (1⩽𝑏𝑖⩽2) — Dasha's plan. If 𝑏𝑖=1, then on the 𝑖th day, Dasha will buy a new guinea pig. If 𝑏𝑖=2, 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 105105.

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.

Example

input

Copy

 
 

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

output

Copy

3
0
3
4
12
9

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 0 guinea pigs, which means she will need 0 aviaries.

In the third set of input data, you even need 3 aviaries to put each guinea pig in a separate aviary before the doctor arrives at the 4th 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 3.

In the fourth set of input data, we show that 4 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 2, or they are not more than 1, which means that the female is at least 3. 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 4. Let's say that among the first 4 guinea pigs, 3 are female and 1 is male. We need at least 3 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.

 

每一次出现2,老鼠就会有原来的占据n个笼子变为占据n/2+1个笼子,所以每一次记录最大值即可 

#include<iostream>
using namespace std;
#define ll long long
int x;
int main()
{
    int t;
    cin >> t;
    while (t--){
        ll n;
        ll ans = 0, pigs = 0, cages = 0;
        cin >> n;
        for (int i = 0; i < n; i++){
            cin >> x;
            if (x == 1){
                pigs++;
                cages++;
            }
            else{
                if (pigs != 0){
                    cages = pigs / 2 + 1;
                }

            }
            ans = max(ans, cages);
        }

        cout << ans << endl;

    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

q619718

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值