G. Inscryption,2022 icpc南京站

Problem - G - Codeforces

time limit per test

3 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

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 n 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 11.
  • 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 T indicating the number of test cases. For each test case:

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

The second line contains n integers a1,a2,⋯,an (−1≤ai≤1) where ai indicates the type of the i-th event mark: 1 means a Card Choice, −1 means a Mysterious Stone and 00 means a Fork in the Road.

It's guaranteed that the sum of n 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 p′q′. Output two integers p and q where pq is the simplest fraction representation of p′q′.

pq is the simplest fraction representation of p′q′ if pq=p′q′ and the greatest common divisor of p and q 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:

EventActionBeasts
11Gain additional Beast{1,1}{1,1}
11Gain additional Beast{1,1,1}{1,1,1}
11Gain additional Beast{1,1,1,1}{1,1,1,1}
−1−1Choose Beasts with attack 11 and 11{2,1,1}{2,1,1}
11Gain additional Beast{2,1,1,1}{2,1,1,1}
11Gain additional Beast{2,1,1,1,1}{2,1,1,1,1}
−1−1Choose Beasts with attack 22 and 11{3,1,1,1}{3,1,1,1}

The average attack is 3+1+1+14=64=323+1+1+14=64=32.

The second sample test case is explained as follows:

EventActionBeasts
11Gain additional Beast{1,1}{1,1}
00Trigger Card Choice and gain additional Beast{1,1,1}{1,1,1}
−1−1Choose Beasts with attack 11 and 11{2,1}{2,1}
00Trigger Mysterious Stone and choose Beasts with attack 22 and 11{3}{3}

The average attack is 3131.

The third sample test case is explained as follows:

EventActionBeasts
0Trigger Card Choice and gain additional Beast{1,1}
−1Choose Beasts with attack 1 and 1{2}
−1Not enough BeastsFailure

解析:

 将所有 0 变为-1,当和sum小于0时再将其变回 1 ;

我最先开始的方法是将所有的 0 都变为1,超过 2 后再将 0 变回 -1,但这种做法是错的,因为贪心的原理是找最优的解如果将所有的 0 都变为1,超过 2 后再将 0 变回 -1,就会出现将当前位置前的数变为小于零的情况,而将所有 0 变为-1,当和sum小于0时再将其变回 1 ;因为它仍然是满足条件(sum>0)的解;且没有更有的解。

反思:对贪心的理解不深刻

 

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<math.h>
#include<map>

using namespace std;
typedef long long LL;
const int N = 1e6 + 5;
int n;
//int a[N];

int gcd(int a, int b) {
	if (b == 0)
		return a;
	return gcd(b, a % b);
}
int main() {
	int T;
	scanf("%d", &T);
	while (T--) {
		scanf("%d", &n);
		int sum = 0, tt = 0,flg=0,b0=0;
		for (int i = 1,a; i <= n; i++) {
			scanf("%d", &a);
			if (a > 0) {
				sum ++;
			}
			else {
				tt = a == 0 ? tt + 1 : tt;
				sum --;
				b0++;
			}
			if (sum < 0) {
				if (tt > 0) {
					tt--;
					sum += 2;
					b0--;
				}
				else {
					flg = 1;
				}
			}
			
		}
		if (flg) {
			cout << -1 << endl;
			continue;
		}
		int ans = n - b0+1;
		int cnt = sum+1;
		int ret=gcd(ans, cnt);
		ans /= ret;
		cnt /= ret;
		printf("%d %d\n", ans, cnt);
	}
	return 0;
}

posted @ 2023-11-03 21:57  Landnig_on_Mars  阅读(20)  评论(0编辑  收藏  举报  来源