兔崽小孩

兔崽小孩

https://ac.nowcoder.com/acm/contest/23480/I

此题由于二分板子没记清楚,导致循环出错。
后来看了题解才知道c++里有专门二分求>=k的位置的函数
在从小到大的排序数组中:
lower_bound( begin,end,num)。该函数返回的是第一个大于等于num的第一个数字地址,减去一个数组起始地址即可得到数字在数组中的下标。
upper_bound( begin,end,num)则是大于num的第一个数字。
在从大到小的排序数组中:
lower_bound( begin,end,num,greater<type>() ),
upper_bound( begin,end,num,greater<type>() )
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=1e6+10;
  int a[N],b[N],c[N];
int main()
{
    int n,q;
    scanf("%d%d",&n,&q);
    for(int i=0,j=0;i<n;i++)
    {
      scanf("%d",&a[i]);
      if(i!=0){
       b[j++]=a[i]-a[i-1];
    }
    }
    sort(b,b+n-1);
    for(int i=0,j=0;i<=n-1;i++)
        c[++j]=b[i]+c[i];
    while(q--)
    {
        int k,p;
        scanf("%d%d",&k,&p);
        int pos=lower_bound(b, b+n-1, k)-b;
        long long sum=c[n-1]-c[pos]-(n-pos-1)*k;
        if(sum>=p)printf("Yes\n");
        else printf("No\n");
    }
}


全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 17:10
点赞 评论 收藏
分享
06-25 09:33
厦门大学 Java
程序员饺子:现在日常估计没啥hc了,等到八月多估计就慢慢有了。双九✌🏻不用焦虑的
投递快手等公司7个岗位
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务