QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#116347 | #5108. Prehistoric Programs | thanhs | WA | 23ms | 49180kb | C++20 | 2.8kb | 2023-06-28 15:49:11 | 2023-06-28 15:49:14 |
Judging History
answer
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef stack<ll> sll;
typedef queue<ll> qll;
typedef deque<ll> dll;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
#define endl '\n'
#define pb push_back
#define FOR(i,a,b) for(int i = a; i <= b; i++)
#define BACK(i,a,b) for(int i = a; i >= b; i--)
#define fastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define fi first
#define se second
#define bp __builtin_popcountll
#define gcd __gcd
#define bit(i,n) ((n>>i)&1)
#define setmin(x,y) x=min((x),(y))
#define setmax(x,y) x=max((x),(y))
const int MAXN = (1e6)+5;
// const ll SQRT = 4;
const long long inf = 1e18;
const long long MOD = 998244353;
int n,c1,c2,mn[MAXN],cost[MAXN],bound[10*MAXN],sz;
pair<int,int> st[MAXN];
pair<pair<int,int>,int> a[MAXN];
queue<int> ans;
string s;
void build(const int& x,const int& lx,const int& rx)
{
if(rx-lx==1)
{
if(lx<n) st[x]={a[lx].fi.se,lx};
else st[x]={INT_MIN,-1};
return;
}
int m=rx+lx>>1;
build(2*x+1,lx,m);
build(2*x+2,m,rx);
st[x]=max(st[2*x+1],st[2*x+2]);
}
void build()
{
sz=1;
while(sz<n) sz*=2;
sz*=2;
build(0,0,sz);
}
void del(const int& x,const int& lx,const int & rx,const int &u)
{
if(rx-lx==1)
{
st[x]={INT_MIN,-1};
return;
}
int m=lx+rx>>1;
if(u<m) del(2*x+1,lx,m,u);
else del(2*x+2,m,rx,u);
st[x]=max(st[2*x+1],st[2*x+2]);
}
void del(const int& x)
{
del(0,0,sz,x);
}
pair<int,int> query(const int& x,const int& lx,const int& rx,const int& l,const int &r)
{
// cout<<x<<' '<<lx<<' '<<rx<<endl;
if(rx<=l||r<=lx) return {INT_MIN,-1};
if(lx>=l&&rx<=r) return st[x];
int m=lx+rx>>1;
return max(query(2*x+1,lx,m,l,r),query(2*x+2,m,rx,l,r));
}
pair<int,int> query(const int& x)
{
return query(0,0,sz,0,x);
}
signed main()
{
fastIO
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
cin>>n;
FOR(i,0,n-1) a[i].se=i;
FOR(i,0,n-1) a[i].fi.fi=1e8;
FOR(i,0,n-1)
{
cin>>s;
FOR(j,0,s.size()-1)
{
if(s[j]=='(') c1++;
else c2++;
a[i].fi.se+=(s[j]=='('?1:-1);
a[i].fi.fi=min(a[i].fi.fi,a[i].fi.se);
}
}
if(c1!=c2) {cout<<"impossible"; return 0;}
sort(a,a+n,greater<pair<pair<int,int>,int>>());
FOR(i,0,n-1) a[i].fi.fi=(a[i].fi.fi>0?0:-a[i].fi.fi);
FOR(i,0,n-1) bound[a[i].fi.fi]=i;
FOR(i,1,1e7) bound[i]=max(bound[i],bound[i-1]);
build();
int cur=0;
FOR(i,0,n-1)
{
pair<int,int> tmp=query(bound[cur]+1);
if(cur+tmp.fi<0) {cout<<"impossible"; return 0;}
cur+=tmp.fi;
del(tmp.se);
ans.push(a[tmp.se].se);
}
while(!ans.empty()) {cout<<ans.front()+1<<endl; ans.pop();}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 23ms
memory: 49180kb
input:
50000 ( ( ))))()))()(()))()()()))()(((((()(((()))()(((()))((()(())))))(()( ) ( ) ((( ( ( ( ( ( () ( ) ( )((())()(( )))))( ( ) )) )() ( ) ) )()( ( ( () ( ) ( )()((((())()))())( ( ( )( ( ( (()())()) ) ) ( ( ( )((())))((())))))))))((((()()))()))))))))((()())())) ) )() ) ) ) ) ) ())(())))))()(()((()(())...
output:
impossible
result:
wrong answer you didn't find a solution but jury did