QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#216575#7567. Joining CatsEastKingTL 220ms102312kbC++172.0kb2023-10-15 20:05:582023-10-15 20:05:58

Judging History

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

  • [2023-10-15 20:05:58]
  • 评测
  • 测评结果:TL
  • 用时:220ms
  • 内存:102312kb
  • [2023-10-15 20:05:58]
  • 提交

answer

	#include<bits/stdc++.h>
	using namespace std;
	const int M=5005;
	int n,K;
	int w[M],s[M],ms[M];
	long long sum[M];
	int dis[M][M];
	int nxt[M][13];
	int calcl(int l,int r,int lim){
		int ans=r+1;
		while(l<=r){
			int mid=l+r>>1;
			if(sum[r]-sum[mid-1]<=lim){
				ans=mid;
				r=mid-1;
			}else {
				l=mid+1;
			}
		}
		return ans;
	}
	int calcr(int l,int r,int lim){
		int ans=l-1;
		while(l<=r){
			int mid=l+r>>1;
			if(sum[mid]-sum[l-1]<=lim){
				ans=mid;
				l=mid+1;
			}else {
				r=mid-1;
			}
		}
		return ans;
	}
	struct node{
		int d,l,r;
		bool operator <(const node &tmp)const{
			return d>tmp.d;
		}
	};
	int get_K(int p,int lim){
		if(s[p]>=lim)return p;
		for(int i=12;i>=0;i--){
			if(s[nxt[p][i]]>=lim)continue;
			p=nxt[p][i]; 
		}
		return nxt[p][0];
	}
	void bfs(){
		memset(dis,63,sizeof(dis));
		priority_queue<node>Q; 
		for(int i=1;i<=n;i++){
			dis[i][i]=0;
			Q.push({0,i,i});
		}
		while(!Q.empty()){
			node now=Q.top();
			Q.pop();
			int l=now.l,r=now.r;
			if(now.d>dis[l][r])continue;
			//printf("dis[%d][%d]=%d\n",l,r,dis[l][r]);
			int k=dis[l][r]+1;
			if(k>K)continue;
			if(l>1){
				int p=get_K(k,w[l-1]);
				//printf("pl=%d\n",p);
				if(p<=K){
					int ql=calcl(1,l-1,s[p]);
					if(dis[ql][r]>p){
						dis[ql][r]=p;
						Q.push({p,ql,r});
					}
				}
			}
			if(r<n){
				int p=get_K(k,w[r+1]);
				//printf("pr=%d\n",p);
				if(p<=K){
					int qr=calcr(r+1,n,s[p]);
					if(dis[l][qr]>p){
						dis[l][qr]=p;
						Q.push({p,l,qr});
					} 
				}
			}
		}
	}
	int main(){
		scanf("%d %d",&n,&K);
		for(int i=1;i<=n;i++){
			scanf("%d",&w[i]);
			sum[i]=sum[i-1]+w[i];
		}
		for(int i=1;i<=K;i++){
			scanf("%d",&s[i]);
		}
		for(int i=K;i>=0;i--){
			int x=i+1;
			while(x<K+1){
				if(s[i]>=s[x])x=nxt[x][0];
				else break;
			}
			nxt[i][0]=x;
		}
		for(int j=1;j<13;j++){
			for(int i=0;i<=n;i++){
				nxt[i][j]=nxt[nxt[i][j-1]][j-1];
			}
		}
		bfs();
		if(dis[1][n]<=K)printf("Yes\n");
		else printf("No\n");
		return 0; 
	}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 101872kb

input:

5 2
1 1 1 1 1
2 2

output:

Yes

result:

ok answer is YES

Test #2:

score: 0
Accepted
time: 3ms
memory: 101664kb

input:

6 7
3 2 1 1 2 3
2 2 2 2 2 2 2

output:

No

result:

ok answer is NO

Test #3:

score: 0
Accepted
time: 0ms
memory: 101712kb

input:

7 4
1 2 3 4 3 2 1
3 3 3 3

output:

Yes

result:

ok answer is YES

Test #4:

score: 0
Accepted
time: 3ms
memory: 101556kb

input:

5 1
5 4 3 2 1
10

output:

Yes

result:

ok answer is YES

Test #5:

score: 0
Accepted
time: 220ms
memory: 102312kb

input:

5000 5000
775487425 856128884 277783434 903422359 477267301 475103384 297595527 426167697 732858986 408894759 274205836 78265305 841664344 827278645 235744961 539622829 661053351 709331224 497285040 688977639 794889854 890450616 730989757 164925481 519732355 5132018 793806705 617096813 966338860 838...

output:

No

result:

ok answer is NO

Test #6:

score: -100
Time Limit Exceeded

input:

5000 5000
719129447 937392296 350445117 783330021 802155515 695380072 535475671 613171233 926763173 500405367 828284512 931492995 720877462 919465915 260912626 876806990 884762137 576596567 928561233 974405439 891740632 540536614 879167622 725668608 801467926 601260355 706621299 926987536 994204742 ...

output:

Yes

result: