QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#717611 | #9252. Penguins in Refrigerator | zyxawa | WA | 6ms | 52708kb | C++14 | 1.6kb | 2024-11-06 18:29:05 | 2024-11-06 18:29:07 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int n,m,x,lst,p[1000001],w[1000001],a[1000001],b[1000001],L[1000001],R[1000001],st[1000001][21],t[1000001],ind[1000001];
long long s=1;
basic_string <int> G[1000001];
priority_queue <pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;
const int mod=1e9+7;
int query(int l,int r){
int k=__lg(r-l+1);
return max(st[l][k],st[r-(1<<k)+1][k]);
}
bool cmp(int x,int y){
return w[x]>w[y];
}
void add(int i){
for(;i<=n;i+=i&-i) t[i]++;
}
int ask(int i){
int s=0;
for(;i;i-=i&-i) s+=t[i];
return s;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&p[i]),a[p[i]]=b[i]=i;
for(int i=1;i<=n;i++) scanf("%d",&x),w[p[i]]=x;
for(int i=1;i<=n;i++) st[i][0]=w[i];
for(int k=1;k<=20;k++) for(int i=1;i+(1<<k)-1<=n;i++) st[i][k]=max(st[i][k-1],st[i+(1<<k-1)][k-1]);
for(int i=1;i<=n;i++){
if(w[i]>m/2) continue;
int l=1,r=i;
while(l<r){
int mid=(l+r)/2;
if(query(mid,i-1)+w[i]>m) l=mid+1;
else r=mid;
}
L[i]=l-1,l=i,r=n;
while(l<r){
int mid=(l+r+1)/2;
if(query(i+1,mid)+w[i]>m) r=mid-1;
else l=mid;
}
R[i]=l+1,G[L[i]]+=i,G[i]+=R[i],ind[i]++,ind[R[i]]++;
}
sort(b+1,b+1+n,cmp);
for(int i=1;i<=n;i++){
if(w[i]>m/2) G[lst]+=i,lst=i,ind[i]++;
add(b[i]);
if(w[b[i]]<=m/2) s=s*(ask(R[b[i]]-1)-ask(L[b[i]]))%mod;
}
for(int i=0;i<=n;i++) if(!ind[i]) q.push({a[i],i});
printf("%lld\n",s);
while(q.size()){
auto [k,x]=q.top();
q.pop();
if(k) printf("%d ",k);
for(int y:G[x]) if(!--ind[y]) q.push({a[y],y});
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 6ms
memory: 52708kb
input:
5 10 1 2 3 4 5 6 5 3 9 2
output:
3 1 2 3 4 5
result:
wrong answer 2nd lines differ - expected: '5 4 2 1 3', found: '1 2 3 4 5 '