QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#756506 | #9252. Penguins in Refrigerator | thomaswmy | WA | 4ms | 73372kb | C++14 | 2.3kb | 2024-11-16 20:45:08 | 2024-11-16 20:45:09 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+10;
const int Mod=1e9+7;
typedef long long ll;
int qpow(int x,ll y) {
int z=1;
for(;y;y>>=1) {
if(y&1) z=1ll*z*x%Mod;
x=1ll*x*x%Mod;
}
return z;
}
int read() {
int s=0;
char c=getchar();
while(c<'0' || c>'9') c=getchar();
while(c>='0' && c<='9') s=s*10+c-'0',c=getchar();
return s;
}
int n,W;
int a[N],b[N],w[N];
pair<int,int> ww[N];
pair<int,int> p[N],c[N];
int ctot;
int s[N];
vector<int> sons[N];
int siz[N],f[N];
vector<int> g[N];
int deg[N];
struct DSU{
int fa[N];
void init(int n) {
for(int i=1;i<=n;i++) fa[i]=i;
}
int get(int x) {
if(fa[x]==x) return x;
return fa[x]=get(fa[x]);
}
}dsu[2];
bool cmp(pair<int,int> u,pair<int,int> v) {
if(u.first!=v.first) return u.first<v.first;
return u.second>v.second;
}
int dfs(int u) {
siz[u]=1;
int res=1;
for(int v:sons[u]) {
res=1ll*res*dfs(v)%Mod;
siz[u]+=siz[v];
}
if(u) res=1ll*res*(s[c[u].second-1]-s[c[u].first]+siz[u])%Mod;
return res;
}
void solve() {
int lst=0;
for(int i=1;i<=n;i++) {
if(w[a[i]]*2>W) {
if(lst) g[lst].push_back(i),deg[i]++;
lst=i;
}
else {
if(p[i].first>0) g[p[i].first].push_back(i),deg[i]++;
if(p[i].second<=n) g[i].push_back(p[i].second),deg[p[i].second]++;
}
}
priority_queue<pair<int,int> > q;
for(int i=1;i<=n;i++) if(!deg[i]) q.push({-a[i],i});
while(!q.empty()) {
int u=q.top().second;
printf("%d ",a[u]);
q.pop();
for(int v:g[u]) {
deg[v]--;
if(!deg[v]) q.push({-a[v],v});
}
}
}
int main() {
n=read(),W=read();
for(int i=1;i<=n;i++) b[a[i]=read()]=i;
for(int i=1;i<=n;i++) w[i]=read(),ww[i]={w[i],i};
sort(ww+1,ww+1+n);
dsu[0].init(n+1),dsu[1].init(n+1);
for(int i=n;i>=1;i--) {
int u=b[ww[i].second];
int l,r;
for(l=u;l>0 && w[a[u]]+w[a[l]]<=W;l=dsu[0].get(l)) {
dsu[0].fa[l]=l-1;
}
for(r=u;r<=n && w[a[u]]+w[a[r]]<=W;r=dsu[1].get(r)) {
dsu[1].fa[r]=r+1;
}
p[u]={l,r};
if(w[a[u]]*2<=W) c[++ctot]=p[u];
}
for(int i=1;i<=n;i++) {
s[i]=s[i-1];
if(w[a[i]]*2>W) s[i]++;
}
sort(c+1,c+1+ctot,cmp);
int rt=0;
c[0]={0,n+2};
for(int i=1;i<=ctot;i++) {
while(c[rt].second<c[i].second) rt=f[rt];
f[i]=rt;
sons[rt].push_back(i);
rt=i;
}
printf("%d\n",dfs(0));
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 4ms
memory: 73372kb
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 '