icpc南京(G贪心)

You are lost deep in the forest. The only thing that is still accompanying you is your stoat. It has an initial attack of 11. It is your only Beast at the beginning.

A single path revealed itself before you. On the path are 𝑛 event marks. Every event mark falls into one of the following:

  • Card Choice: A dentizen of the forest shall grace your caravan. You will gain an additional Beast. It will always have an initial attack of 1
  • Mysterious Stone: You will be compelled to make a worthy sacrifice. Two Beasts from your caravan of your choice will perform the ritual: one to be lost forever, adding its attack onto the other. Failure to perform the ritual will forbid you to go on.
  • Fork in the Road: You will choose to trigger either a Card Choice or a Mysterious Stone. You can't choose to do nothing.

When you walk through the winding road, the event marks will be triggered in order. Find out the maximum average of attack for your Beasts you can achieve after all event marks are completed.

Input

There are multiple test cases. The first line of the input contains an integer 𝑇indicating the number of test cases. For each test case:

The first line contains one integer 𝑛 (1≤𝑛≤106) indicating the number of event marks.

The second line contains 𝑛 integers 𝑎1,𝑎2,⋯,𝑎𝑛 (−1≤𝑎𝑖≤1) where 𝑎𝑖 indicates the type of the 𝑖-th event mark: 11 means a Card Choice, −1−1 means a Mysterious Stone and 00 means a Fork in the Road.

It's guaranteed that the sum of 𝑛over all test cases does not exceed 106106.

Output

For each test case output one line.

If it is impossible to complete all event marks, output one integer −1.

Otherwise it can be proven that the answer is a rational number 𝑝′𝑞′. Output two integers 𝑝and 𝑞 where 𝑝𝑞 is the simplest fraction representation of 𝑝′𝑞′.

𝑝𝑞 is the simplest fraction representation of 𝑝′𝑞′ if 𝑝𝑞=𝑝′𝑞′and the greatest common divisor of 𝑝and 𝑞 is 1.

Example

input

Copy

 
 

6

7

1 1 1 -1 1 1 -1

4

1 0 -1 0

4

0 -1 -1 0

1

0

2

0 0

1

-1

output

Copy

3 2
3 1
-1
1 1
2 1
-1

Note

The first sample test case is explained as follows:

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
using namespace std;
typedef pair<int,int>PII;
constexpr int N=1e6+7;
int gcd(int x,int y){
    return y? gcd(y,x%y):x;
}
signed main(){
    int T;
    scanf("%d",&T);
    while(T--) {
        int n;
        scanf("%d", &n);
        // s:序列元素之和
        // c:序列元素数量
        // t:之前所有自由选择事件中,选了几次事件二
        int s = 1, c = 1, t = 0;
        bool failed = false;
        // 按顺序处理所有事件
        for (int i = 1; i <= n; i++) {
            int x;
            scanf("%d", &x);
            // 事件一
            if (x == 1) s++, c++;
                // 事件二
            else if (x == -1) {
                // 尝试直接执行
                if (c > 1) c--;
                    // 无法直接执行,尝试反悔一次自由选择
                else if (t >= 1) t--, s++, c++;
                    // 无法反悔,无解
                else failed = true;
            } else {
                // 自由选择事件,尽可能选择事件二
                if (c > 1) t++, c--;
                else s++, c++;
            }
        }
        if (failed) printf("-1\n");
        else {
            int g = gcd(s, c);
            printf("%d %d\n", s / g, c / g);
        }
    }
    return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

q619718

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

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

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

打赏作者

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

抵扣说明:

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

余额充值