QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#345046 | #7953. Product Delivery | juancs# | WA | 1ms | 3644kb | C++20 | 2.6kb | 2024-03-06 01:28:16 | 2024-03-06 01:28:18 |
Judging History
answer
#include<bits/stdc++.h>
#define el '\n'
#define pb push_back
#define forn(i, n) for(int i = 0; i < (int)n; ++i)
#define for1(i, n) for(int i = 1; i <= (int)n; ++i)
#define fore(i,l,r ) for(int i = l; i<= r; ++i)
#define sz(a) (int) a.size()
#define fi first
#define se second
#define d(x) cerr << #x << ' ' << x << el
using namespace std;
typedef pair<int, int> ii;
typedef unsigned long long ll;
struct frac{
ll num, den;
frac(ll num, ll den):num(num), den(den){
simplify();
}
frac(frac a, frac b, frac c){
*this = a + (b/c);
simplify();
}
frac operator+(frac b){
frac c(b.den * num + b.num * den, den * b.den);
return c;
}
frac operator/(frac b){
frac c(num * b.den, den * b.num);
return c;
}
void simplify(){
int gc = __gcd(num, den);
num /= gc, den/=gc;
}
};
string s;
vector<int> vs;
int n;
bool valid = true;
frac go(int l, int r){
if(r - l + 1 < 3) {
valid = 0;
return {0,1};
}
vector<frac> arr;
fore(i, l, r){
if(vs[i] == ')'){
valid = 0;
break;
}
if(vs[i] == '('){
int d = 1;
int li = i++;
while(i <= r && d != 0){
if(vs[i] == '(') ++d;
else if(vs[i] == ')') --d;
if(d < 0){
valid = 0; break;
}
if(!d) break;
++i;
}
if(i > r){
valid = 0;
break;
}
assert(vs[li] == '(' && vs[i] == ')');
arr.pb(go(li + 1, i-1));
}else{
arr.pb({vs[i] - '0' , 1});
}
}
if(sz(arr) != 3 && (l!=0 && r != n-1)) valid = 0;
if(!valid){d(l), d(r); return {0,1};}
if(l == 0 && r == n-1)return arr.front();
// if(sz(arr) == 3){
// forn(i,3){
// cout<<arr[i].num << ", "<<arr[i].den<<el;
// }
// }
// frac res(arr[0], arr[1], arr[2]);
// cout<<res.num << ", "<<res.den<<el;
return frac(arr[0], arr[1], arr[2]);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin>>n;
cin.ignore();
getline(cin,s);
stringstream ss(s);
string cur = "";
int cnt = 0;
while(ss>>cur){
vs.pb(cur[0]);
}
if(cnt > 0) valid = 0;
if(!valid){
cout<<"-1"<<el;
return 0;
}
frac ans = go(0, n-1);
if(!valid){
cout<<"-1"<<el;
return 0;
}
cout<<ans.num <<" "<<ans.den<<el;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3644kb
input:
4 13 15 5 8 6 14 3 7
output:
1 1
result:
wrong answer 1st lines differ - expected: '2', found: '1 1'