请问这个代码为什么爆零了(自己做的数据全过了+样例过了)
查看原帖
请问这个代码为什么爆零了(自己做的数据全过了+样例过了)
95897
judy1741楼主2020/4/29 21:54
#include <iostream>
#include "stdio.h"
#include <algorithm>
#include <string>
#include "string.h"
#include "math.h"
#include <queue>
long long a[200001]={0};
int n,l,v;
using namespace std;
bool cmp(long long n1,long long n2)
{
	return n1>n2;
}
int bs(int num)
{
	//printf("num=%d ",num);
	int l=1,r=n;
	int cut;
	while(l<r)
	{
		cut=(r-l)/2+l;
		if(num<a[cut]) r=cut;
		if(num>a[cut]) l=cut+1;
		if(num==a[cut]) return cut+1;
	}
	return cut+1;
}
int main()
{
	//freopen("endless.in","r", stdin);
	//freopen("endless.out","w", stdout);
	scanf("%d %d %d",&n,&l,&v);
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
	}
	int q;
	scanf("%d",&q);
	
	int t;
	sort(a+1,a+n+1,cmp);
	for(int i=2;i<=n;i++){
		a[i]+=a[i-1];
		//printf("a=%lld ",a[i]);
	}
	//printf("\n");
	
	for(int i=1;i<=q;i++){
		scanf("%d",&t);
		long long x=t*v-l;
		//printf("t=%d, x=%d\n", t, x);
		if(x<0)
		{
			printf("0\n");
			continue;
		}
		if(x < a[1]){
			printf("1\n");
			continue;
		}
		if(x >= a[n]){
			printf("-1\n");
			continue;
		}
		printf("%d\n",bs(x));
		
	}
	return 0;
}
2020/4/29 21:54
加载中...