zoukankan      html  css  js  c++  java
  • 【例题 8-10 UVA

    【链接】 我是链接,点我呀:)
    【题意】

    在这里输入题意

    【题解】

    二分最后的最大值的最小值。 得到ans 然后从后往前尽量划分。 如果发现不够分成k个。 那么就从第一个开始接着分restk个(每隔1个分1块 中间遇到之前分了的就直接跳过

    【代码】

    /*
      	1.Shoud it use long long ?
      	2.Have you ever test several sample(at least therr) yourself?
      	3.Can you promise that the solution is right? At least,the main ideal
      	4.use the puts("") or putchar() or printf and such things?
      	5.init the used array or any value?
      	6.use error MAX_VALUE?
      	7.use scanf instead of cin/cout?
      	8.whatch out the detail input require
    */
    /*
        一定在这里写完思路再敲代码!!!
        二分最后的最大值的最小值。
        得到ans
        然后从后往前尽量划分。
        如果发现不够分成k个。
        那么就从第一个开始接着分restk个(每隔1个分1块
        中间遇到之前分了的就直接跳过
    */
    #include <bits/stdc++.h>
    #define ll long long
    using namespace std;
    
    const int N = 500;
    
    int n,k,a[N+10];
    bool tag[N+10];
    
    bool ok(ll up){
        ll now = 0;
        int times = 1;
        for (int i = 1;i <= n;i++)
            if (a[i]>up) return false;
                else
                    {
                        now+=a[i];
                        if (now > up){
                            now = a[i];
                            times++;
                        }
                    }
        return times<=k;
    }
    
    int main(){
    	#ifdef LOCAL_DEFINE
    	    freopen("rush_in.txt", "r", stdin);
    	#endif
    	ios::sync_with_stdio(0),cin.tie(0);
        int T;
        cin >> T;
        while (T--){
            memset(tag,0,sizeof tag);
            cin >> n >> k;
            for (int i = 1;i <= n;i++) cin >> a[i];
            ll l = 0,r = 50e8,temp = -1;
            while (l <= r){
                ll mid = (l+r)>>1;
                if (ok(mid)){
                    temp = mid;
                    r = mid-1;
                }else l = mid + 1;
            }
            k--;
            ll now = a[n];
            for (int i = n-1;k>0 && i >= 1;i--){
                now += a[i];
                if (now > temp){
                    k--;
                    now = a[i];
                    tag[i] = 1;
                }
            }
            if (k > 0){
                for (int i = 1;k>0 && i <= n;i++)
                    if (!tag[i]){
                        tag[i] = 1;
                        k--;
                    }
            }
            for (int i = 1;i <= n;i++)
            {
                cout << a[i];
                if (tag[i]) cout <<" /";
                if (i!=n) cout << ' ';else cout << endl;
            }
        }
    	return 0;
    }
    
    
  • 相关阅读:
    《数据结构》2.2顺序表(sequence list)
    《算法竞赛入门经典》6.3.1二叉树-小球下落
    java_时间戳与Date_相互转化
    java事物
    Mysql如何向存在外键的数据表中插入数据
    git基本配置
    mysql时间属性之时间戳和datetime之间的转换
    【转】变量命名(简短且无歧义)
    【转】mybatis实战教程(mybatis in action),mybatis入门到精通
    [转]DAO层,Service层,Controller层、View层
  • 原文地址:https://www.cnblogs.com/AWCXV/p/8185301.html
Copyright © 2011-2022 走看看