• CF Round 481 (Div. 3)--E. Bus Video System(二分)


    The busses in Berland are equipped with a video surveillance system. The system records information about changes in the number of passengers in a bus after stops.

    If x is the number of passengers in a bus just before the current bus stop and y is the number of passengers in the bus just after current bus stop, the system records the number y−x. So the system records show how number of passengers changed.

    The test run was made for single bus and n bus stops. Thus, the system recorded the sequence of integers a1,a2,…,an (exactly one number for each bus stop), where ai is the record for the bus stop i. The bus stops are numbered from 1 to n in chronological order.

    Determine the number of possible ways how many people could be in the bus before the first bus stop, if the bus has a capacity equals to w (that is, at any time in the bus there should be from 00 to w passengers inclusive).

    Input

    The first line contains two integers n and w (1≤n≤1000,1≤w≤10^9) — the number of bus stops and the capacity of the bus.

    The second line contains a sequence a1,a2,…,an (−10^6≤ai≤10^6), where ai equals to the number, which has been recorded by the video system after the i-th bus stop.

    Output

    Print the number of possible ways how many people could be in the bus before the first bus stop, if the bus has a capacity equals to w. If the situation is contradictory (i.e. for any initial number of passengers there will be a contradiction), print 0.

    input 1

    1. 3 5
    2. 2 1 -3

    output 1

    3

    input 2

    1. 2 4
    2. -1 1

    output 2

    4

    Note

    In the first example initially in the bus could be 0, 1 or 2 passengers.

    In the second example initially in the bus could be 1, 2, 3 or 4 passengers.

    题意:给定n个车站的上下车人数(负数表示下车人数),公交车的最大容量为m,问你公交车初始发车前已经存在的人数有多少种可能。

    解析:分析可知,满足条件的人数肯定是一段区间,但上下界不知道,但是下界我们是好求的,因此我们求出下界,再二分答案即可,此题细节比较多,许多地方需要判断,尤其是判断下界的一处,详见代码。

    1. #include
    2. using namespace std;
    3. const int N=1e3+5;
    4. int a[N],n,m;
    5. bool check(int x)//判断是否合法
    6. {
    7. if(x>m) return false;//特判1,已经超出最大容量
    8. for(int i=1;i<=n;i++)
    9. {
    10. x+=a[i];
    11. if(x<0||x>m) return false;//基础的不合法情况
    12. }
    13. return true;
    14. }
    15. void solve()
    16. {
    17. scanf("%d%d",&n,&m);
    18. for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    19. int l=0,sum=0,mx=0;
    20. for(int i=1;i<=n;i++)
    21. {
    22. sum+=a[i];
    23. mx=max(mx,sum);
    24. if(sum<0) l+=-sum,sum=0;
    25. if(l+mx>m)//特判2,如果初始人数+之前最大人数>m就不合法了
    26. {
    27. printf("0\n");
    28. return;
    29. }
    30. }
    31. int r=1e9,ansl=l,ansr=l;//分别为答案上下界
    32. while(l<=r)
    33. {
    34. int mid=l+r>>1;
    35. if(check(mid)) ansr=mid,l=mid+1;
    36. else r=mid-1;
    37. }
    38. printf("%d\n",ansr-ansl+1);
    39. }
    40. int main()
    41. {
    42. int t=1;
    43. //scanf("%d",&t);
    44. while(t--) solve();
    45. return 0;
    46. }
  • 相关阅读:
    分享从零开始学习网络设备配置(华为ensp版本)------任务1.2 使用eNSP搭建和配置网络
    实践总结:一篇搞懂链表——单链表和双指针技巧
    go-zero 微服务实战系列(二、服务拆分)
    vite的使用
    java 中的Object类与Objects类
    【Python】五、程序循环结构
    uniapp编译微信小程序富文本rich-text的图片样式不生效原因
    Mac本安装objection
    SLAM面经整理
    tidb-cdc同步到kafka报错cdc报错CDC:ErrKafkaNewSaramaProducer
  • 原文地址:https://blog.csdn.net/qq_63739337/article/details/132741618