QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#425871 | #1443. Potato Shuffle | grass8cow# | WA | 2ms | 10276kb | C++17 | 2.5kb | 2024-05-30 17:53:56 | 2024-05-30 17:53:57 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7,I=1e9+10;
int n,k,a[201000];
#define pi pair<int,int>
#define fi first
#define se second
struct SGT{
pi mi[801000];
void sc(int p){mi[p]=min(mi[p<<1],mi[p<<1|1]);}
inline void build(int p,int l,int r,int ty){
if(l==r){mi[p]={a[l]*ty,l};return;}
int mid=(l+r)>>1;
build(p<<1,l,mid,ty),build(p<<1|1,mid+1,r,ty);
sc(p);
}
inline void up(int p,int l,int r,int x,int z){
if(l==r){mi[p]={z,l};return;}
int mid=(l+r)>>1;
if(x<=mid)up(p<<1,l,mid,x,z);
else up(p<<1|1,mid+1,r,x,z);
sc(p);
}
inline pi ask(int p,int l,int r,int x,int y){
if(x<=l&&r<=y)return mi[p];
int mid=(l+r)>>1;
pi nt={I,-1};
if(x<=mid)nt=min(nt,ask(p<<1,l,mid,x,y));
if(y>mid)nt=min(nt,ask(p<<1|1,mid+1,r,x,y));
return nt;
}
}A,B;
struct BIT{
int tr[201000];
inline void ad(int x,int z){for(;x<=n;x+=(x&-x))tr[x]+=z;}
inline int ask(int x){int s=0;for(;x;x-=(x&-x))s+=tr[x];return s;}
}T;
void cl(int x){A.up(1,1,n,x,I),B.up(1,1,n,x,-I),T.ad(x,-1);}
int ans,jc[200100],ij[200100],inv[201000];
#define pb push_back
int C(int a,int b){if(a<0||b<0||a<b)return 0;return 1ll*jc[a]*ij[b]%mod*ij[a-b]%mod;}
void cdq(int l,int r){
if(l>r)return;
pi t1=A.ask(1,1,n,l,r);if(t1.fi>=I)return;
pi t2=B.ask(1,1,n,l,r);
int wc=T.ask(r)-T.ask(l-1);
if(t1.fi-t2.fi<=k){
//找到所有最小值并删去。
int o=l;
vector<int>P;
while(o<=r){
pi z=A.ask(1,1,n,o,r);
if(z.fi==t1.fi)P.pb(z.se),o=z.se+1;
else break;
}
for(int x:P)cl(x);
ans=1ll*ans*C(wc,(int)P.size())%mod;
cdq(l,r);return;
}
else{
int o=l;
vector<int>P;
while(o<=r){
pi z=B.ask(1,1,n,o,r);
if(z.fi==t2.fi)P.pb(z.se),o=z.se+1;
else break;
}
int sz=P.size();
for(int i=0;i<=sz;i++)cdq((i==0)?l:(P[i-1]+1),(i==sz)?r:(P[i]-1));
}
}
int main(){
scanf("%d%d",&n,&k);
ans=1;
inv[1]=1;
for(int i=2;i<=n;i++)inv[i]=mod-1ll*inv[mod%i]*(mod/i)%mod;
jc[0]=ij[0]=1;
for(int i=1;i<=n;i++)jc[i]=1ll*i*jc[i-1]%mod,ij[i]=1ll*inv[i]*ij[i-1]%mod,T.ad(i,1);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
A.build(1,1,n,1),B.build(1,1,n,-1);
cdq(1,n);printf("%d",ans);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 9892kb
input:
3 7 5 2 4
output:
3
result:
ok "3"
Test #2:
score: 0
Accepted
time: 1ms
memory: 9924kb
input:
5 4 1 2 3 2 1
output:
10
result:
ok "10"
Test #3:
score: 0
Accepted
time: 0ms
memory: 9948kb
input:
6 4 9 10 1 9 8 9
output:
1
result:
ok "1"
Test #4:
score: 0
Accepted
time: 2ms
memory: 9980kb
input:
8 0 4 9 2 9 2 3 9 10
output:
1
result:
ok "1"
Test #5:
score: 0
Accepted
time: 0ms
memory: 10276kb
input:
5 3 9 8 1 9 8
output:
1
result:
ok "1"
Test #6:
score: -100
Wrong Answer
time: 2ms
memory: 9992kb
input:
6 12 4 7 2 9 1 9
output:
48
result:
wrong answer 1st words differ - expected: '60', found: '48'