QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#124770#5420. Inscryptionchinguun_0301#TL 1ms3392kbC++141.0kb2023-07-15 15:21:162023-07-15 15:21:16

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-15 15:21:16]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3392kb
  • [2023-07-15 15:21:16]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

int n;

int gcd(int a, int b){
    if(a == 0)return b;
    return gcd(b % a, a);
}
void solve(){
    cin >> n;
    int x;
    int sum = 0, b = 0;
    int ttl1 = 0, ttl0 = 0, ttlm1 = 0;
    for(int i = 0; i < n; i++){
        cin >> x;
        
        if(x == 1) ttl1++;
        if(x == -1) ttlm1++;
        if(x == 0) ttl0++;

        if(x == -1) sum--;
        else sum++;

        if(sum < 0) b = 1;
    }

    if(b == 1){
        cout << -1 << "\n";
        return;
    }
    
    int ttl = ttl0 + ttlm1 - ttl1;
    if(ttl % 2 == 0) ttl = ttl / 2;
    else ttl = ttl / 2 + 1;

    ttl1 += ttl;
    ttlm1 += (ttl0 - ttl);
    
    int s1 = ttl1 + 1;
    int s2 = s1 - ttlm1;
    
    if(s1 == 0 && s2 == 0) {
        cout << "-1\n";
        return;
    }

    int a = gcd(s1, s2);
    assert(a != 0);
    cout << s1 / a << " " << s2 / a << "\n";
}

int main(){
    int t; 
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3392kb

input:

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:

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

result:

ok 6 lines

Test #2:

score: -100
Time Limit Exceeded

input:

1000000
1
1
1
-1
1
1
1
1
1
1
1
1
1
-1
1
-1
1
0
1
0
1
1
1
0
1
-1
1
0
1
1
1
0
1
1
1
0
1
1
1
0
1
0
1
0
1
1
1
-1
1
1
1
1
1
-1
1
0
1
1
1
0
1
-1
1
0
1
-1
1
1
1
-1
1
0
1
1
1
1
1
-1
1
0
1
-1
1
-1
1
-1
1
-1
1
0
1
0
1
-1
1
0
1
-1
1
0
1
0
1
0
1
0
1
0
1
-1
1
1
1
0
1
0
1
1
1
0
1
-1
1
1
1
1
1
0
1
1
1
1
1
1
1
0
1
...

output:

3 4
-1
3 4
3 4
3 4
3 4
-1
-1
1 1
1 1
3 4
1 1
-1
1 1
3 4
1 1
3 4
1 1
3 4
1 1
1 1
1 1
3 4
-1
3 4
3 4
-1
1 1
3 4
1 1
-1
1 1
-1
3 4
-1
1 1
3 4
3 4
-1
1 1
-1
-1
-1
-1
1 1
1 1
-1
1 1
-1
1 1
1 1
1 1
1 1
1 1
-1
3 4
1 1
1 1
3 4
1 1
-1
3 4
3 4
1 1
3 4
3 4
3 4
1 1
-1
-1
1 1
3 4
-1
3 4
1 1
3 4
3 4
-1
1 1
3 4
1 ...

result: