zoukankan      html  css  js  c++  java
  • bzoj4590: [Shoi2015]自动刷题机(二分答案)

    4590: [Shoi2015]自动刷题机

    题目:传送门 


    题解:

       很明显的一道二分题。

       对于二分性的判断:如果n越大,那么AC的题就越少,n越小,AC的题就越多,那么最大最小值都满足单调性,直接瞎搞。

       PS:上界有点大...搞得我WA了一次


    代码:

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<cstdlib>
     4 #include<cmath>
     5 #include<algorithm>
     6 using namespace std;
     7 typedef long long LL;
     8 LL n,m,l,r;
     9 LL a[110000];
    10 LL check(LL x)
    11 {
    12     LL sum=0,cnt=0;
    13     for(int i=1;i<=n;i++)
    14     {
    15         sum+=a[i];if(sum<=0)sum=0;
    16         if(sum>=x)cnt++,sum=0;
    17     }
    18     return cnt;
    19 }
    20 int main()
    21 {
    22     scanf("%lld%lld",&n,&m);
    23     for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
    24     LL ans1=0,ans2=0;
    25     l=1;r=1e15;
    26     while(l<=r)
    27     {
    28         LL mid=(l+r)/2;
    29         if(check(mid)>m)l=mid+1,ans1=mid;
    30         else r=mid-1;
    31     }
    32     l=1;r=1e15;
    33     while(l<=r)
    34     {
    35         LL mid=(l+r)/2;
    36         if(check(mid)<m)r=mid-1,ans2=mid;
    37         else l=mid+1;
    38     }
    39     ans1++,ans2--;
    40     if(check(ans1)!=m || check(ans2)!=m)printf("-1
    ");
    41     else printf("%lld %lld
    ",ans1,ans2);
    42     return 0;
    43 }
  • 相关阅读:
    iOS 上线流程
    静态库和动态库的区别
    iOS如何生成.a文件
    苹果公司软件
    iOS 的主要框架
    多线程图解
    判断屏幕横屏/竖屏
    最大堆构建和堆排序
    hadoop2.6.0 + hbase-1.0.0 伪分布配置
    centos6 名字服务dnsmasq配置
  • 原文地址:https://www.cnblogs.com/CHerish_OI/p/8637496.html
Copyright © 2011-2022 走看看