100分,但是最后的两个sub检测点没过
查看原帖
100分,但是最后的两个sub检测点没过
842812
jeremy2022楼主2023/10/12 22:41

为什么我的代码最后的两个sub检测点没过?

#include<bits/stdc++.h>
using namespace std;
const int N=3e5+1;
int n,k;
long long ans=0;
int main(){
	cin>>n>>k;
	int a[N]={};
	int di,mi,max=0;
	for(int i=0;i<n;i++){
		cin>>di>>mi;
		a[di]+=mi;
		max=di>max?di:max;
	}
	long long i,temp=0;//temp用于保存第一天处理不完,要推到第二天的量
	for(i=1;i<=max;i++){
//		if(a[i]<=k){//当天的量不超过k,可以由当天处理,不推到第二天
//			ans+=a[i];
//		}else{
//			ans+=k;
//			temp=ans-k;//计算超出去的量
//			if(i%2==1){
//				a[i+1]+=a[i]-k;
//			}
//			
//		}
		if(temp<=k&&temp>=0){
			ans+=temp;
			if(a[i]<=k-temp){//处理完上一天的剩余量,再处理当天的量
				ans+=a[i];
				temp=0;
			}
			else{
				ans+=k-temp;
				temp=a[i]-(k-temp);
			}
		}
		else if(temp>k){
			ans+=k;
			temp=a[i];
		}
	}
	
	if(temp<=k&&temp>=0){
		ans+=temp;
	}
	else{
		ans+=k;
	}
	cout<<ans;
	return 0;
}

2023/10/12 22:41
加载中...