QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#751559 | #9252. Penguins in Refrigerator | fansizhe | WA | 2ms | 28188kb | C++20 | 4.7kb | 2024-11-15 19:22:37 | 2024-11-15 19:22:38 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
char buf1[1<<21],*p1=buf1+(1<<21),*p2=buf1+(1<<21);
char _getchar(){
if(p1==p2)fread(p1=buf1,1<<21,1,stdin);
return *p1++;
}
int read(){
int x=0;char c=_getchar();
while(c<'0'||c>'9')c=_getchar();
while(c>='0'&&c<='9')x=x*10+(c^'0'),c=_getchar();
return x;
}
char buf2[1<<21],*p3=buf2,*p4=buf2+(1<<21);
void _putchar(char x){
if(p3==p4)fwrite(p3=buf2,1<<21,1,stdout);
*p3++=x;
}
void write(int x){
if(!x)return _putchar('0'),_putchar(' '),void();
int num[25],len=0;
while(x)num[++len]=x%10,x/=10;
while(len)_putchar(num[len--]^'0');
_putchar(' ');
}
#define ll long long
const ll mod=1e9+7;
int n,W;
int a[1000005],pos[1000005],val[1000005];
int st[1000005],top;
vector<int> edge[1000005];
int deg[1000005];
ll fac[1000005],inv[1000005];
int vis[1000005];
ll power(ll a,ll p){
ll res=1;
while(p){
if(p&1)res=res*a%mod;
p>>=1;
a=a*a%mod;
}
return res;
}
int mx[4000005],mxp[4000005],mn[4000005],mnp[4000005],tree[4000005];
void build(int k,int l,int r){
tree[k]=r-l+1;
if(l==r){
mx[k]=mn[k]=val[l];
mxp[k]=mnp[k]=l;
return;
}
int mid=l+r>>1;
build(k*2,l,mid);
build(k*2+1,mid+1,r);
if(mx[k*2]>=mx[k*2+1])mx[k]=mx[k*2],mxp[k]=mxp[k*2];
else mx[k]=mx[k*2+1],mxp[k]=mxp[k*2+1];
if(mn[k*2]<=mn[k*2+1])mn[k]=mn[k*2],mnp[k]=mnp[k*2];
else mn[k]=mn[k*2+1],mnp[k]=mnp[k*2+1];
}
void erase(int k,int l,int r,int x){
tree[k]--;
if(l==r){
mn[k]=0x3f3f3f3f,mnp[k]=0;
return;
}
int mid=l+r>>1;
if(x<=mid)erase(k*2,l,mid,x);
else erase(k*2+1,mid+1,r,x);
if(mn[k*2]<=mn[k*2+1])mn[k]=mn[k*2],mnp[k]=mnp[k*2];
else mn[k]=mn[k*2+1],mnp[k]=mnp[k*2+1];
}
int querymx(int k,int l,int r,int x,int y){
if(l>=x&&r<=y)return mxp[k];
int mid=l+r>>1;
if(y<=mid)return querymx(k*2,l,mid,x,y);
if(x>mid)return querymx(k*2+1,mid+1,r,x,y);
int res1=querymx(k*2,l,mid,x,y),res2=querymx(k*2+1,mid+1,r,x,y);
if(val[res1]>=val[res2])return res1;
else return res2;
}
int query(int k,int l,int r,int x,int y){
if(l>=x&&r<=y)return tree[k];
int mid=l+r>>1;
if(y<=mid)return query(k*2,l,mid,x,y);
if(x>mid)return query(k*2+1,mid+1,r,x,y);
return query(k*2,l,mid,x,y)+query(k*2+1,mid+1,r,x,y);
}
pair<int,int> querymn(int k,int l,int r,int x,int y){
if(l>=x&&r<=y)return make_pair(mn[k],mnp[k]);
int mid=l+r>>1;
if(y<=mid)return querymn(k*2,l,mid,x,y);
if(x>mid)return querymn(k*2+1,mid+1,r,x,y);
return min(querymn(k*2,l,mid,x,y),querymn(k*2+1,mid+1,r,x,y));
}
ll solve(int l,int r){
if(l>r)return 1;
int p=querymx(1,1,n,l,r);
int all=query(1,1,n,l,r),cnt=0;
while(1){
auto q=querymn(1,1,n,l,r);
if(q.first+val[p]<=W)cnt++,erase(1,1,n,q.second);
else break;
}
// int p=l;
// for(int i=l;i<=r;i++)if(val[i]>val[p])p=i;
// int all=0,cnt=0;
// for(int i=l;i<=r;i++)if(!vis[i])all++;
// for(int i=l;i<=r;i++)if(!vis[i]&&val[i]+val[p]<=W)cnt++,vis[i]=1;
return solve(l,p-1)*solve(p+1,r)%mod*fac[all]%mod*inv[all-cnt]%mod;
}
int main(){
n=read(),W=read();
for(int i=1;i<=n;i++)pos[n-(a[i]=read())+1]=i;
for(int i=1;i<=n;i++)val[pos[i]]=read();
fac[0]=1;
for(int i=1;i<=n;i++)fac[i]=fac[i-1]*i%mod;
inv[n]=power(fac[n],mod-2);
for(int i=n;i>=1;i--)inv[i-1]=inv[i]*i%mod;
build(1,1,n);
write(solve(1,n));_putchar('\n');
for(int i=1;i<=n;i++){
if(top&&val[i]+val[st[1]]>W){
int l=1,r=top;
while(l<r){
int mid=l+r+1>>1;
if(val[st[mid]]+val[i]<=W)r=mid-1;
else l=mid;
}
edge[a[st[l]]].push_back(a[i]),deg[a[i]]++;
}
while(top&&val[st[top]]<=val[i])top--;
st[++top]=i;
}
top=0;
for(int i=n;i>=1;i--){
if(top&&val[i]+val[st[1]]>W){
int l=1,r=top;
while(l<r){
int mid=l+r+1>>1;
if(val[st[mid]]+val[i]<=W)r=mid-1;
else l=mid;
}
edge[a[i]].push_back(a[st[l]]),deg[a[st[l]]]++;
}
while(top&&val[st[top]]<=val[i])top--;
st[++top]=i;
}
priority_queue<int,vector<int>,greater<int>> que;
for(int i=1;i<=n;i++)if(!deg[i])que.push(i);
while(!que.empty()){
int x=que.top();que.pop();
write(x);
for(int y:edge[x])if(!--deg[y])que.push(y);
}
_putchar('\n');
fwrite(buf2,p3-buf2,1,stdout);
// cerr<<"time: "<<clock()*1000./CLOCKS_PER_SEC<<"ms"<<endl;
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 28188kb
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 '