QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#51547#1443. Potato ShuffleJayfeather233WA 16ms6236kbC++4.1kb2022-10-02 16:35:242022-10-02 16:35:25

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-10-02 16:35:25]
  • 评测
  • 测评结果:WA
  • 用时:16ms
  • 内存:6236kb
  • [2022-10-02 16:35:24]
  • 提交

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=minn;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);
	}
	if(minn==maxn) return ans;
	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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 15ms
memory: 4208kb

input:

3 7
5 2 4

output:

3

result:

ok "3"

Test #2:

score: 0
Accepted
time: 15ms
memory: 5880kb

input:

5 4
1 2 3 2 1

output:

10

result:

ok "10"

Test #3:

score: 0
Accepted
time: 15ms
memory: 5824kb

input:

6 4
9 10 1 9 8 9

output:

1

result:

ok "1"

Test #4:

score: 0
Accepted
time: 11ms
memory: 5852kb

input:

8 0
4 9 2 9 2 3 9 10

output:

1

result:

ok "1"

Test #5:

score: 0
Accepted
time: 7ms
memory: 6236kb

input:

5 3
9 8 1 9 8

output:

1

result:

ok "1"

Test #6:

score: 0
Accepted
time: 15ms
memory: 6224kb

input:

6 12
4 7 2 9 1 9

output:

60

result:

ok "60"

Test #7:

score: 0
Accepted
time: 15ms
memory: 5860kb

input:

10 18
9 6 3 9 5 3 9 9 5 1

output:

37800

result:

ok "37800"

Test #8:

score: 0
Accepted
time: 15ms
memory: 5832kb

input:

1 13
5

output:

1

result:

ok "1"

Test #9:

score: 0
Accepted
time: 15ms
memory: 5816kb

input:

4 7
10 4 4 8

output:

1

result:

ok "1"

Test #10:

score: 0
Accepted
time: 16ms
memory: 5848kb

input:

2 4
3 3

output:

1

result:

ok "1"

Test #11:

score: 0
Accepted
time: 12ms
memory: 5928kb

input:

3 12
8 1 6

output:

3

result:

ok "3"

Test #12:

score: 0
Accepted
time: 16ms
memory: 6236kb

input:

9 3
3 10 5 8 9 1 3 1 9

output:

1

result:

ok "1"

Test #13:

score: 0
Accepted
time: 16ms
memory: 6048kb

input:

10 19
8 9 6 8 3 5 8 8 8 2

output:

30240

result:

ok "30240"

Test #14:

score: 0
Accepted
time: 16ms
memory: 5924kb

input:

3 8
3 8 7

output:

1

result:

ok "1"

Test #15:

score: 0
Accepted
time: 12ms
memory: 5844kb

input:

5 6
8 7 6 8 3

output:

1

result:

ok "1"

Test #16:

score: 0
Accepted
time: 12ms
memory: 6108kb

input:

10 4
4 6 7 7 7 5 10 8 9 5

output:

1

result:

ok "1"

Test #17:

score: 0
Accepted
time: 11ms
memory: 5928kb

input:

3 5
9 5 8

output:

1

result:

ok "1"

Test #18:

score: 0
Accepted
time: 15ms
memory: 5784kb

input:

1 15
4

output:

1

result:

ok "1"

Test #19:

score: 0
Accepted
time: 16ms
memory: 5836kb

input:

4 0
9 2 8 9

output:

1

result:

ok "1"

Test #20:

score: 0
Accepted
time: 16ms
memory: 6200kb

input:

7 10
4 1 9 9 7 8 6

output:

7

result:

ok "7"

Test #21:

score: 0
Accepted
time: 12ms
memory: 6208kb

input:

9 15
9 10 10 9 1 2 2 9 3

output:

1512

result:

ok "1512"

Test #22:

score: 0
Accepted
time: 9ms
memory: 6176kb

input:

2 12
4 9

output:

1

result:

ok "1"

Test #23:

score: -100
Wrong Answer
time: 16ms
memory: 5800kb

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:

850069451

result:

wrong answer 1st words differ - expected: '731201602', found: '850069451'