QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#559161#9252. Penguins in RefrigeratorhewanyingWA 64ms107568kbC++142.1kb2024-09-11 20:35:572024-09-11 20:35:57

Judging History

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

  • [2024-09-11 20:35:57]
  • 评测
  • 测评结果:WA
  • 用时:64ms
  • 内存:107568kb
  • [2024-09-11 20:35:57]
  • 提交

answer

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

const int N=1e6+5,H=1e9+7;
int n,p[N],a[N],ans=1,ls[N],rs[N],W,st[N],tp=0,fa[N],id[N],sz[N],pre[N],Ans[N],cnt=0,b[N],lim[N];
vector<int> G[N][2];
priority_queue<int> Q;
set<int> S[N];
bool vis[N];

int mul(int a,int b){return 1ll*a*b%H;}

int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}

void merge(int u,int v){
  if(!u||!v) return;
  u=find(u),v=find(v);
  if(u!=v) fa[v]=u;
}

void dfs1(int u,int lst){
  int szlc=0,szrc=0;lim[u]=lst,S[lst].insert(b[u]);
  for(auto v:G[u][0]) dfs1(v,u),szlc+=sz[v];
  for(int i=0;i<(int)G[u][0].size()-1;i++) ans=mul(ans,szlc-i);
  for(auto v:G[u][1]) dfs1(v,lst),szrc+=sz[v];
  for(int i=0;i<(int)G[u][1].size()-1;i++) ans=mul(ans,szrc-i);
  sz[u]=szlc+szrc+1;
}

void dfs2(int u){
  if(G[u][0].empty()&&G[u][1].empty()) return;
  for(auto v:G[u][0]) if(G[v][0].empty()&&G[v][1].empty()) Q.push(-b[v]);
  for(auto v:G[u][0]) dfs2(v);
  while(!Q.empty()&&-Q.top()<=b[u]){
	int tmp=-Q.top();Q.pop();
	if(!vis[tmp]) vis[tmp]=1,Ans[++cnt]=tmp;
  }
  for(auto i:S[u]) if(!vis[i]) vis[i]=1,Ans[++cnt]=i;
  Ans[++cnt]=b[u];
  for(auto v:G[u][1]) if(G[v][0].empty()&&G[v][1].empty()) Q.push(-b[v]);
  for(auto v:G[u][1]) dfs2(v);
}

int main(){
  ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  cin>>n>>W;
  for(int i=n;i>=1;i--) cin>>b[i],p[b[i]]=i;
  for(int i=1;i<=n;i++) cin>>a[p[i]],fa[i]=i,id[i]=i;
  ++n,id[n]=n,a[n]=W,fa[n]=n,b[n]=n;
  sort(id+1,id+n+1,[&](int x,int y){return a[x]>a[y];});

  for(int i=1,k;i<=n;i++){
	k=tp;
	while(k>0&&a[st[k]]<a[i]) --k;
    if(k) rs[st[k]]=i,pre[i]=st[k];
	if(k<tp) ls[i]=st[k+1],pre[st[k+1]]=i;
	tp=k,st[++tp]=i;
  }

//   cerr<<"hi"<<endl;

  for(int i=1,r=n;i<=n;i++){
	// cerr<<"i = "<<i<<endl;
    while(r>=1&&a[id[r]]+a[id[i]]<=W) merge(id[r],ls[id[r]]),merge(id[r],rs[id[r]]),--r;
    if(i!=1){
	  int u=pre[find(id[i])],v=id[i];
	//   cerr<<u<<" -> "<<v<<endl;
	  if(v<u) G[u][0].pb(v);
	  else G[u][1].pb(v);
	}
  }
  dfs1(n,n+1),dfs2(n);

//   cerr<<"cnt = "<<cnt<<endl;

  cout<<ans<<'\n';

  for(int i=1;i<n;i++) cout<<Ans[i]<<' ';
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 19ms
memory: 98792kb

input:

5 10
1 2 3 4 5
6 5 3 9 2

output:

3
5 4 2 1 3 

result:

ok 2 lines

Test #2:

score: 0
Accepted
time: 12ms
memory: 98160kb

input:

5 10
1 2 3 4 5
2 4 3 3 8

output:

30
1 5 2 3 4 

result:

ok 2 lines

Test #3:

score: 0
Accepted
time: 4ms
memory: 99868kb

input:

5 10
1 2 3 4 5
2 3 4 5 1

output:

120
1 2 3 4 5 

result:

ok 2 lines

Test #4:

score: 0
Accepted
time: 12ms
memory: 98080kb

input:

5 10
1 2 3 4 5
2 3 4 5 6

output:

60
1 2 3 5 4 

result:

ok 2 lines

Test #5:

score: 0
Accepted
time: 64ms
memory: 107568kb

input:

100000 96
1996 78922 45321 68844 32404 82013 66552 81163 17216 48170 35495 56660 13480 43118 23173 47257 50168 87069 26167 67231 31758 25694 61063 56642 8923 7727 54528 96554 38964 7604 6822 16256 45300 58869 31359 48638 87645 14779 81505 59585 89293 9291 7002 31810 84701 77648 78295 42595 11394 479...

output:

457992974
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 10...

result:

ok 2 lines

Test #6:

score: -100
Wrong Answer
time: 40ms
memory: 104412kb

input:

100000 84
93330 3894 94859 22134 49668 30606 26739 82976 76701 56323 75537 7626 87226 20857 98177 21811 70827 75898 8111 48223 26186 64222 63002 79024 19126 41638 1048 43857 25379 19764 60207 27675 77665 66327 6274 34861 30287 13449 64505 51490 5804 65843 49014 85795 12365 31565 34411 71697 66568 28...

output:

559106436
286 1405 6270 7883 9737 10462 11030 97500 13210 18520 19647 22714 23696 28370 29959 31380 35927 36321 38702 41872 42143 44112 44168 44177 48705 49065 50436 54800 55958 56525 59155 60272 63458 66106 68194 69393 70815 71454 72098 75273 78306 78433 92484 80037 16124 18243 21509 2104 13210 161...

result:

wrong answer 1st lines differ - expected: '524727018', found: '559106436'