QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#71678 | #3238. Just So You Know | chenshi | WA | 2452ms | 112576kb | C++ | 3.3kb | 2023-01-11 18:50:09 | 2023-01-11 18:50:11 |
Judging History
answer
#include<cstdio>
#include<queue>
#include<utility>
#include<algorithm>
namespace iobuff{const int LEN=1e6;char in[LEN+5],out[LEN+5];char *pin=in,*pout=out,*ed=in,*eout=out+LEN;
inline char gc(){return pin==ed&&(ed=(pin=in)+fread(in,1,LEN,stdin),ed==in)?EOF:*pin++;}
inline void pc(char c){pout==eout&&(fwrite(out,1,LEN,stdout),pout=out);(*pout++)=c;}
inline void flush(){fwrite(out,1,pout-out,stdout),pout=out;}
template<typename T>inline void scan(T&x){static int f;static char c;
for(c=gc(),f=1,x=0;c<'0'||c>'9';c=gc()) f=(c=='-'?-1:1);for(;c>='0'&&c<='9';c=gc()) x=10*x+c-'0';x*=f;}
template<typename T>inline void putint(T x,char div){static char s[20];static int top;top=0;
for(x<0?pc('-'),x=-x:0;x;x/=10) s[top++]=x%10;for(!top?pc('0'),0:0;top--;) pc(s[top]+'0');pc(div);}}
using namespace iobuff;
using namespace std;
long long gcd(long long a,long long b){return b?gcd(b,a%b):a;}
const int o=1e6+10;
int T,n,a[o],sa[o],rk[o],buc[o],x[o],y[o],cnt,height[o],len[o],p[o],mx,fa[o],s[o],sz[o];
long long ans,g,tot;priority_queue<pair<long long,int> > q;vector<int> vec[o];
int fr(int x){return fa[x]==x?x:fa[x]=fr(fa[x]);}
inline void sufsort(){
mx=105;
for(int i=1;i<mx;++i) buc[i]=0;
for(int i=1;i<=n;++i) ++buc[x[i]=s[i]];
for(int i=2;i<mx;++i) buc[i]+=buc[i-1];
for(int i=n;i;--i) sa[buc[x[i]]--]=i;
for(int md=1;md<=n;md<<=1){
cnt=0;
for(int i=n-md+1;i<=n;++i) y[++cnt]=i;
for(int i=1;i<=n;++i) if(sa[i]>md) y[++cnt]=sa[i]-md;
for(int i=1;i<mx;++i) buc[i]=0;
for(int i=1;i<=n;++i) ++buc[x[i]];
for(int i=2;i<mx;++i) buc[i]+=buc[i-1];
for(int i=n;i;--i) sa[buc[x[y[i]]]--]=y[i],y[i]=0;
y[sa[1]]=cnt=1;
for(int i=2;i<=n;++i) y[sa[i]]=((x[sa[i]]==x[sa[i-1]]&&x[sa[i]+md]==x[sa[i-1]+md])?cnt:++cnt);
if(cnt==n) break;for(int i=1;i<=n;++i) x[i]=y[i];mx=cnt+1;
}
};
inline void get_height(){
for(int i=0;i<=n;++i) height[i]=0,rk[sa[i]]=i,vec[i].clear(),sz[i]=0;
for(int i=1,j,k=0;i<=n;++i) if(rk[i]>1){
if(k) --k;
for(j=sa[rk[i]-1];j+k<=n&&i+k<=n&&s[i+k]==s[j+k];++k);
++sz[height[rk[i]]=k];
}
}
inline bool cmp(int A,int B){return height[A]<height[B];}
int main(){
for(scan(T);T--;ans=cnt=0,priority_queue<pair<long long,int> >().swap(q)){
scan(n);
for(int i=1;i<=n;++i) scan(a[i]),s[i]=a[i]+1;
sufsort();get_height();
for(int i=1;i<=n;++i) len[i]=n-sa[i]+1,s[fa[i]=i]=1;
for(int i=0;i<=n;++i) if(sz[i]) vec[i].reserve(sz[i]);
for(int i=2;i<=n;++i) vec[height[i]].push_back(i);
continue;
for(int i=n,p,t;i+1;--i) for(int j=vec[i].size();j--;){
t=fr(p=vec[i][j]);
if(len[p-1]>height[p]) q.push(make_pair(-s[p-1],len[p-1]-height[p]));
if(len[t]>height[p]) q.push(make_pair(-s[t],len[t]-height[p]));
len[t]=height[p];s[fa[p-1]=t]+=s[p-1];
}
if(len[fr(1)]) q.push(make_pair(-n,len[fr(1)]));
continue;
for(long long v,v_,c,c_;1;){
v=q.top().first;c=q.top().second;q.pop();
if(c>1){
ans-=c/2*2*v;q.push(make_pair(v*2,c/2));
if(c&1) q.push(make_pair(v,1));
}
else if(q.empty()) break;
else{
v_=q.top().first;c_=q.top().second;q.pop();
ans-=v+v_;q.push(make_pair(v+v_,1));
if(c_>1) q.push(make_pair(v_,c_-1));
}
}
tot=n*(n+1ll)/2;g=gcd(ans,tot);
ans/=g;tot/=g;
if(tot==1) putint(ans,'\n');
else putint(ans,'/'),putint(tot,'\n');
}
flush();
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 2452ms
memory: 112576kb
input:
1000 1033 25 82 89 85 87 49 93 78 46 30 16 55 39 50 47 99 88 31 80 70 27 85 5 2 65 40 87 6 18 34 42 68 61 73 68 36 47 8 28 83 88 19 13 27 89 64 67 26 65 35 55 50 41 5 96 54 47 82 15 85 97 62 61 36 20 49 35 9 51 89 49 91 59 12 40 63 27 39 56 49 92 45 79 34 37 78 1 2 23 53 15 0 14 13 42 30 53 80 80 79...
output:
result:
wrong answer 1st lines differ - expected: '10162982/534061', found: ''