QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#176285 | #5420. Inscryption | Zhou_JK | RE | 0ms | 0kb | C++23 | 1.3kb | 2023-09-11 14:34:25 | 2023-09-11 14:34:25 |
answer
#include<iostream>
#include<cstdio>
using namespace std;
const int N=1000005;
int gcd(int a,int b)
{
return b==0?a:gcd(b,a%b);
}
int n;
int a[N];
int s[N];
int suf[N];
void solve()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
if(a[i]==-1) s[i]=s[i-1]-1;
else s[i]=s[i-1]+1;
suf[n+1]=suf[n]=s[n];
for(int i=n-1;i>=1;i--)
suf[i]=min(suf[i+1],s[i]);
// for(int i=1;i<=n;i++)
// cerr<<s[i]<<" ";cerr<<"\n";
int tot=1,sum=1;
for(int i=1;i<=n;i++)
{
if(a[i]==1) tot++,sum++;
else if(a[i]==-1)
{
if(tot>=2) tot--;
else
{
printf("-1\n");
return;
}
}
else
{
// cerr<<"now"<<i<<" "<<suf[i+1]-s[i]<<"\n";
if(tot-1<=-(suf[i+1]-s[i])) sum++,tot++;
else
{
if(tot<=1)
{
exit(1);
}
tot--;
}
}
}
int g=gcd(tot,sum);
tot/=g,sum/=g;
printf("%d %d\n",sum,tot);
return;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
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