QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#641421 | #9252. Penguins in Refrigerator | 11d10xy | WA | 46ms | 46956kb | C++14 | 2.0kb | 2024-10-14 20:27:04 | 2024-10-14 20:27:04 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using i64=long long;
constexpr i64 mod=1000000007;
int W,n,p[1000010],val[1000010],L[1000010],R[1000010],stk[1000010],tp,deg[1000010];
bool flag[1000010];
vector<int>G[1000010];
namespace ds{
int cnt[1000010];
inline void ins(int p){
for(;p<=n;p+=p&-p)cnt[p]++;
}
inline int ask(int p){
int r=0;for(;p;p-=p&-p)r+=cnt[p];
return r;
}
}
int main(){
cin>>n>>W;
for(int i=1;i<=n;i++)scanf("%d",&p[n-i+1]);
for(int i=1;i<=n;i++)scanf("%d",&val[i]),flag[i]=val[i]<=W/2;
for(int i=1;i<=n;i++){
if(flag[p[i]]){
L[i]=*(partition_point(stk+1,stk+tp+1,[&](int x){return val[p[x]]+val[p[i]]>W;})-1);
}else{
for(;tp&&val[stk[tp]]<=val[i];tp--);
stk[++tp]=i;
}
}
tp=0,stk[0]=n+1;
for(int i=n;i>=1;i--){
if(flag[p[i]]){
R[i]=*(partition_point(stk+1,stk+tp+1,[&](int x){return val[p[x]]+val[p[i]]>W;})-1);
}else{
for(;tp&&val[p[stk[tp]]]<=val[p[i]];tp--);
stk[++tp]=i;
}
}
i64 ans=1;
vector<int>perm;
for(int i=1;i<=n;i++){
if(!flag[p[i]])ds::ins(i);
else perm.push_back(i);
}
sort(begin(perm),end(perm),[](int x,int y){
return R[x]-L[x]<R[y]-L[y];
});
for(int u:perm){
(ans*=ds::ask(R[u]-1)-ds::ask(L[u+1-1])+1)%=mod;
ds::ins(u);
}
printf("%lld\n",ans);
perm={};
for(int i=1;i<=n;i++){
if(!flag[p[i]])perm.push_back(i);
else{
if(L[i])G[L[i]].push_back(i);
if(R[i]!=n+1)G[i].push_back(R[i]);
}
}
for(int i=1;i<perm.size();i++)G[perm[i-1]].push_back(perm[i]);
for(int i=1;i<=n;i++)for(int v:G[i])deg[v]++;
struct CMP_{
bool operator()(int x,int y)const{
return p[x]>p[y];
}
};
priority_queue<int,vector<int>,CMP_>q;
for(int i=1;i<=n;i++)if(!deg[i])q.push(i);
for(;!q.empty();){
int u=q.top();q.pop();
printf("%d ",p[u]);
for(int v:G[u])if(!--deg[v])q.push(v);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 38632kb
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: 0ms
memory: 38616kb
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: 36832kb
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: 3ms
memory: 38624kb
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: 32ms
memory: 39748kb
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: 46ms
memory: 46956kb
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:
753539404 8 12 15 22 24 25 27 31 37 41 47 61 63 76 77 81 86 90 92 94 103 104 105 107 110 114 122 125 126 128 133 136 138 154 159 169 172 179 182 183 187 191 192 194 209 210 218 219 225 230 237 239 240 260 262 271 287 289 292 310 311 319 321 322 323 327 335 338 340 341 342 347 353 354 360 363 364 373...
result:
wrong answer 1st lines differ - expected: '524727018', found: '753539404'