• [CF349B] Color the Fence


    [CF349B] Color the Fence - 贪心

    Description

    写每个数字会花费一定油漆 ai,没有数字 0,只有 v 升油漆,问能写出的最大数字是多少。

    Solution

    首先我们肯定会最大化数字的长度

    然后我们会从前到后依次最大化数字,即试图让第一个数字取最大,然后……

    #include <bits/stdc++.h>
    using namespace std;
    
    #define int long long
    
    const int N = 1000005;
    
    int a[N], s[N], v;
    
    signed main()
    {
        ios::sync_with_stdio(false);
    
        cin >> v;
        for (int i = 1; i <= 9; i++)
            cin >> a[i];
    
        int min_value = 1e9, min_id = 0;
        for (int i = 1; i <= 9; i++)
        {
            if (a[i] <= min_value)
            {
                min_value = a[i];
                min_id = i;
            }
        }
    
        int len = 0;
        while (v >= min_value)
        {
            s[++len] = min_id;
            v -= min_value;
        }
    
        for (int i = 1; i <= len; i++)
        {
            int max_id = 0;
            for (int j = min_id + 1; j <= 9; j++)
            {
                if (a[j] > min_value && a[j] - min_value <= v)
                    max_id = j;
            }
            if (max_id)
            {
                v -= a[max_id] - min_value;
                s[i] = max_id;
            }
            cout << s[i];
        }
    
        if (len == 0)
            cout << -1;
    }
    
  • 相关阅读:
    POJ 2955
    POJ 1276 多重背包
    UVA 11584 划分回文字串
    Uva 11400 照明系统
    POJ 2677 Tour
    Uva 437 巴比伦塔 && UVA10003
    12563 Jin Ge Jin Qu hao
    最小公共祖先 (Tarjan) POJ1470
    DB2
    SQLserver数据库
  • 原文地址:https://www.cnblogs.com/mollnn/p/14428288.html
Copyright © 2020-2023  润新知