zoukankan      html  css  js  c++  java
  • P1182 数列分段 Section II

    二段性:如果对于一个值x,存在一种M分段方法,能使得分段中的最大值满足(le x)那么所有大于等于x的值t都存在M分段的方法使得分段最大值小于等于t。
    题目要求分段最大值的最小值,所以可以用二分。
    检查一个x值,能不能通过分M个段达到分段最大值(le x), 这个用贪心来做,让每一个分段尽可能大,并且小于等于x,得到分段数的最小值cnt,再比较cnt和M的大小,如果cnt <= M则满足。
    复杂度:(O(nlog(1e9)) = O(n), n le 10^5)

    #include<iostream>
    using namespace std;
    
    const int N = 100010;
    
    #define LL long long
    
    int n, m;
    int a[N];
    
    int check(int x){
        int sum = 0, cnt = 1;
        for(int i = 1; i < n; i ++){
            if(a[i] > x) return 0; // 不加特判会导致对于一个x误判正确,导致答案更小,第4个测试点得到454的离谱答案
            sum += a[i];
            if(sum + a[i + 1] > x){
                cnt ++;
                sum = 0;
            }
        }
        
        return cnt <= m;
    }
    
    int main(){
        cin >> n >> m;
        
        for(int i = 1; i <= n; i ++) cin >> a[i];
        
        int l = 0, r = 1e9;
        
        while(l < r){
            int mid = l + r >> 1;
            if(check(mid)) r = mid;
            else l = mid + 1;
        }
        
        cout << l << endl;
        
        return 0;
    }
    
  • 相关阅读:
    Oracle(日期函数)
    Oracle(数值函数)
    Oracle(字符函数)
    Oracle(order by)
    Oracle(限定查询2)
    Oracle(限定查询1)
    Oracle其他简单查询
    Oracle简单语句查询
    SQLPlus
    解决方案
  • 原文地址:https://www.cnblogs.com/tomori/p/13801725.html
Copyright © 2011-2022 走看看