zoukankan      html  css  js  c++  java
  • 「SHOI2015」自动刷题机

    /*
    有理有据的二分答案
    因为在过程中最多减到零  所以n越小显然就能刷更多的题 
    
    无解时就是无论如何也无法得到k , 这个特判一下即可 
    */ 
    
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<iostream>
    #define M 100010
    #define ll long long 
    #define inf 100000000000ll
    using namespace  std;
    ll read()
    {
        ll nm = 0, f = 1;
        char c = getchar();
        for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
        for(; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
        return nm * f;
    }
    ll note[M], n, k;
    
    ll check(ll x)
    {
        ll sum = 0, ans = 0;
        for(int i = 1; i <= n; i++)
        {
            sum += note[i];
            if(sum >= x) ans++, sum = 0;
            if(sum < 0) sum = 0;
        }
        return ans;
    }
    
    int main()
    {
        n = read(), k = read();
        for(int i = 1; i <= n; i++) note[i] = read();
        ll l = 1, r = inf, ln, rn;
        while(l + 1 < r)
        {
            ll mid = (l + r) >> 1;
            if(check(mid) <= k) r = mid;
            else l = mid;
        }
        if(check(l) == k) r = l;
        ln = r;
        l = 1, r = inf;
        while(l + 1 < r)
        {
            ll mid = (l + r) >> 1;
            if(check(mid) >= k) l = mid;
            else r = mid;
        }
        if(check(r) == k) l = r;
        rn = l;
        if(ln > rn || check(ln) != k) return puts("-1");
        cout << ln << " " << rn << "
    ";
        return 0;
    }
  • 相关阅读:
    Python中的编码
    编译gcc
    内存的非法读写操作的检查
    Git合并特定commits 到另一个分支
    局部静态变量是如何做到只初始化一次的?
    how-to-redirect-cin-and-cout-to-files
    Time series database
    Linux System Calls Hooking Method Summary
    tomcat 创建虚拟主机
    oracle查锁表SQL
  • 原文地址:https://www.cnblogs.com/luoyibujue/p/9246240.html
Copyright © 2011-2022 走看看