QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#51524 | #1443. Potato Shuffle | Jayfeather233 | WA | 17ms | 4324kb | C++ | 4.1kb | 2022-10-02 16:06:33 | 2022-10-02 16:06:36 |
Judging History
answer
#include <iostream>
#include <cstdio>
#include <algorithm>
const int MOD = 1e9+7;
int fast_pow(int a,int b){
int ans=1;
while(b){
if(b&1) ans=(1ll*ans*a)%MOD;
a=(1ll*a*a)%MOD;
b>>=1;
}
return ans;
}
#define ls (u<<1)
#define rs ((u<<1)+1)
#define MAXN 100100
namespace maxtree{
int maxn[MAXN<<2];
void build(int* s, int u, int l, int r){
if(l==r){
maxn[u]=s[l];
return;
}
int mid=(l+r)>>1;
build(s,ls,l,mid);
build(s,rs,mid+1,r);
maxn[u]=std::max(maxn[ls],maxn[rs]);
}
int ask(int u, int l, int r, int ll, int rr){
if(ll<=l&&r<=rr) return maxn[u];
if(rr<l || r<ll) return 0;
int mid=(l+r)>>1;
return std::max(ask(ls,l,mid,ll,rr),ask(rs,mid+1,r,ll,rr));
}
}
namespace sizetree{
int sz[MAXN<<2];
void build(int u, int l, int r){
if(l==r){
sz[u]=1;
return;
}
int mid=(l+r)>>1;
build(ls,l,mid);
build(rs,mid+1,r);
sz[u]=sz[ls]+sz[rs];
}
int ask(int u, int l, int r, int ll, int rr){
if(ll<=l&&r<=rr) return sz[u];
if(rr<l || r<ll) return 0;
int mid=(l+r)>>1;
return ask(ls,l,mid,ll,rr)+ask(rs,mid+1,r,ll,rr);
}
void modify(int u,int l,int r,int x){
//printf("mdy at %d %d %d %d\n",u,l,r,x);
if(x<l || r<x) return;
if(l==r){
sz[u]=0;
return;
}
int mid=(l+r)>>1;
modify(ls,l,mid,x);
modify(rs,mid+1,r,x);
sz[u]=sz[ls]+sz[rs];
}
void opt(int u,int l,int r){
printf("%d: %d %d %d\n",u,l,r,sz[u]);
if(l==r) return;
int mid=(l+r)>>1;
opt(ls,l,mid);
opt(rs,mid+1,r);
}
}
#undef ls
#undef rs
namespace link{
struct point{
int pos,nxt,pre;
}s[MAXN];
int head[MAXN],ls=1;
void add(int u,int p){//add a integer[u] at position[p]
s[ls].pos=p;
s[ls].nxt=head[u];
s[head[u]].pre=ls;
head[u]=ls++;
}//reverse
}
using namespace std;
int n,k;
int disc_mp[100100];
int ny[100100];
int after_disc[100100];
int find_next_max(int l,int r){
return maxtree::ask(1,1,n,l,r);
}
int C(int n,int m){//choose m from tot n
m=min(m,n-m);
int ans=1;
for(int i=1;i<=m;i++){
if(ny[i]==0) printf("err at C\n");
ans=(1ll*ans*ny[i])%MOD;
ans=(1ll*ans*(n-i+1))%MOD;
}
return ans;
}
int tot;
int work(int l,int r,int minn){
int maxn=find_next_max(l,r);
if(maxn<minn || r<=l) return 1;
//printf("AT %d %d %d %d\n",l,r,maxn,minn);
int tot_sz=sizetree::ask(1,1,n,l,r);
//printf(" size:%d\n",tot_sz);
int ans=1;
for(int t=0;disc_mp[t]<=k-disc_mp[maxn]&&t<=maxn;t++){
//printf(" disc:t=%d\n",t);
int bel=0;
for(int i=link::head[t];i;i=link::s[i].nxt){
if(r<link::s[i].pos) break;
if(link::s[i].pos<l) continue;
bel++;
if(!link::s[i].pre){
link::head[t]=link::s[i].nxt;
link::s[link::s[i].nxt].pre=0;
}else{
link::s[link::s[i].pre].nxt=link::s[i].nxt;
link::s[link::s[i].nxt].pre=link::s[i].pre;
}
sizetree::modify(1,1,n,link::s[i].pos);
//sizetree::opt(1,1,n);
//printf(" modify:%d\n",link::s[i].pos);
}
ans=(1ll*ans*C(tot_sz,bel))%MOD;
//printf(" bel:%d ans:%d\n",bel);
tot_sz-=bel;
minn=max(minn,t);
}
for(int i=link::head[maxn];i;i=link::s[i].nxt){
ans=(1ll*ans*work(l,link::s[i].pos-1,minn+1))%MOD;
l=link::s[i].pos+1;
}
ans=(1ll*ans*work(l,r,minn+1))%MOD;
return ans;
}
struct tt{
int v,pos;
}disc[100100];
int operator < (const tt a,const tt b){
return a.v==b.v ? a.pos>b.pos : a.v<b.v;
}
int main(){
cin>>n>>k;
for(int i=1;i<=100000;i++) ny[i]=fast_pow(i,MOD-2);
for(int i=0;i<n;i++){
scanf("%d",&disc[i].v);
disc[i].pos=i;
}
sort(disc,disc+n);
for(int i=1;i<n;i++){
if(disc[i].v!=disc[i-1].v){
disc_mp[tot]=disc[i-1].v;
disc[i-1].v=tot;
tot++;
}else{
disc_mp[tot]=disc[i-1].v;
disc[i-1].v=tot;
}
}
disc_mp[tot]=disc[n-1].v;
disc[n-1].v=tot;
//for(int i=0;i<=tot;i++) printf("mp:%d->%d\n",i,disc_mp[i]);
for(int i=0;i<n;i++){
after_disc[disc[i].pos+1]=disc[i].v;
}
//for(int i=1;i<=n;i++){
// printf("%d ",after_disc[i]);
//}
//printf("\n");
maxtree::build(after_disc,1,1,n);
sizetree::build(1,1,n);
for(int i=n;i>=1;i--){
link::add(after_disc[i],i);
}
printf("%d",work(1,n,0));
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 12ms
memory: 4248kb
input:
3 7 5 2 4
output:
3
result:
ok "3"
Test #2:
score: 0
Accepted
time: 11ms
memory: 4128kb
input:
5 4 1 2 3 2 1
output:
10
result:
ok "10"
Test #3:
score: 0
Accepted
time: 11ms
memory: 4200kb
input:
6 4 9 10 1 9 8 9
output:
1
result:
ok "1"
Test #4:
score: 0
Accepted
time: 15ms
memory: 4220kb
input:
8 0 4 9 2 9 2 3 9 10
output:
1
result:
ok "1"
Test #5:
score: 0
Accepted
time: 15ms
memory: 4220kb
input:
5 3 9 8 1 9 8
output:
1
result:
ok "1"
Test #6:
score: 0
Accepted
time: 16ms
memory: 4076kb
input:
6 12 4 7 2 9 1 9
output:
60
result:
ok "60"
Test #7:
score: 0
Accepted
time: 16ms
memory: 4136kb
input:
10 18 9 6 3 9 5 3 9 9 5 1
output:
37800
result:
ok "37800"
Test #8:
score: 0
Accepted
time: 16ms
memory: 4192kb
input:
1 13 5
output:
1
result:
ok "1"
Test #9:
score: 0
Accepted
time: 16ms
memory: 4244kb
input:
4 7 10 4 4 8
output:
1
result:
ok "1"
Test #10:
score: 0
Accepted
time: 11ms
memory: 4208kb
input:
2 4 3 3
output:
1
result:
ok "1"
Test #11:
score: 0
Accepted
time: 15ms
memory: 4128kb
input:
3 12 8 1 6
output:
3
result:
ok "3"
Test #12:
score: 0
Accepted
time: 15ms
memory: 4200kb
input:
9 3 3 10 5 8 9 1 3 1 9
output:
1
result:
ok "1"
Test #13:
score: 0
Accepted
time: 13ms
memory: 4136kb
input:
10 19 8 9 6 8 3 5 8 8 8 2
output:
30240
result:
ok "30240"
Test #14:
score: 0
Accepted
time: 12ms
memory: 4204kb
input:
3 8 3 8 7
output:
1
result:
ok "1"
Test #15:
score: 0
Accepted
time: 15ms
memory: 4220kb
input:
5 6 8 7 6 8 3
output:
1
result:
ok "1"
Test #16:
score: 0
Accepted
time: 15ms
memory: 4220kb
input:
10 4 4 6 7 7 7 5 10 8 9 5
output:
1
result:
ok "1"
Test #17:
score: 0
Accepted
time: 16ms
memory: 4172kb
input:
3 5 9 5 8
output:
1
result:
ok "1"
Test #18:
score: 0
Accepted
time: 15ms
memory: 4280kb
input:
1 15 4
output:
1
result:
ok "1"
Test #19:
score: 0
Accepted
time: 15ms
memory: 4200kb
input:
4 0 9 2 8 9
output:
1
result:
ok "1"
Test #20:
score: 0
Accepted
time: 16ms
memory: 4216kb
input:
7 10 4 1 9 9 7 8 6
output:
7
result:
ok "7"
Test #21:
score: 0
Accepted
time: 11ms
memory: 4204kb
input:
9 15 9 10 10 9 1 2 2 9 3
output:
1512
result:
ok "1512"
Test #22:
score: 0
Accepted
time: 15ms
memory: 4240kb
input:
2 12 4 9
output:
1
result:
ok "1"
Test #23:
score: 0
Accepted
time: 17ms
memory: 4180kb
input:
941 1050595461 97199151 967582202 627391061 648812583 382698254 725831431 99920429 942104929 638728093 697787100 277585567 423990676 376336278 986391434 449561958 800545732 3456798 386231691 307296784 129242596 740319256 218489131 899192544 344153713 971619078 363368462 206242305 149813823 430534965...
output:
731201602
result:
ok "731201602"
Test #24:
score: 0
Accepted
time: 16ms
memory: 4180kb
input:
931 1802136692 972656407 966750261 14838713 725079303 96491328 170902145 865911285 121473856 881400060 115397807 343248192 713973122 693395876 845120872 810689262 697741053 598095593 117050186 62294356 887558804 107838689 348014209 207407232 962013618 291145455 44153613 619738233 106508008 266410860...
output:
910622578
result:
ok "910622578"
Test #25:
score: -100
Wrong Answer
time: 13ms
memory: 4324kb
input:
908 396822628 848113662 965918320 549770012 801346023 810284401 763456507 631902141 300842783 124072027 680492162 556394465 3955568 10455474 556366661 171816567 594936374 340218037 700385033 669808280 498391365 475358123 477539288 515621920 579873524 758155480 724938765 885750512 63202193 102286755 ...
output:
850808257
result:
wrong answer 1st words differ - expected: '701616507', found: '850808257'