Codeforces 349B 《Color the Fence》

Original Created: 2018-09-30 21:56:37

Blind greed

Title Description

Translation from Los Valley

Igor deeply in love with Tanya. Now, Igor would like to express his love, he wrote a string of numbers on the wall across from Tanya's house. Igor think, write the larger the number, the more Tanya liked him. Unfortunately, he only \ (v \) liters of paint, each number will spend some paint \ (a_i \) . Igor did not like \ (0 \) so the number does not appear in \ (0 \) . Igor asked to get the maximum number is.

Input / Output Format & Sample

Input Format

The first line an integer \ (v \) , such as the title meaning

The second row has nine digits \ (A_1, \ A_2, \ A_3, \ \ DOTS \, \ a_9 \) , represents the \ (I \) digits required \ (a_i \) liters of paint

Output Format

Line An integer that represents the maximum number of Igor can get.

SAMPLE INPUT

Case #1:

5
5 4 3 2 1 2 3 4 5

Case #2:

2
9 11 1 12 5 8 9 10 6

Case #3:

0
1 1 1 1 1 1 1 1 1

Sample Output

Case #1:

55555

Case #2:

33

Case #3:

-1

Resolve

Apparent greed

First sort the sequence (priority queue convenientShortcut), Then to the small number of numbers can be drawn and draw large numbers sequentially calculates

Followed by enumeration from 9-1, there is nothing to look at the alternative, replace the lowest number takes a relatively

Finally, the output array answer

Code

Metaphysics code style (fog

/* -- Basic Headers -- */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>

/* -- STL Iterator -- */
#include <vector>
#include <string>
#include <stack>
#include <queue>

/* -- Defined Functions -- */
#define For(a,x,y) for (int a = x; a <= y; ++a)
#define Bak(a,y,x) for (int a = y; a >= x; --a)
using namespace std;

/*  Constants Start  */

/*  Constants End  */

/*  Variants Start  */

int v;

int q[10]; 
// q[i].first = variant
// q[i].second = id

int ans[10];

priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > pq;

/*  Variants End  */

namespace FastIO {
    void DEBUG(char comment[], int x) {
        cerr << comment << x << endl;
    }

    inline int getint() {
        int s = 0, x = 1;
        char ch = getchar();
        while (!isdigit(ch)) {
            if (ch == '-') x = -1;
            ch = getchar();
        }
        while (isdigit(ch)) {
            s = s * 10 + ch - '0';
            ch = getchar();
        }
        return s * x;
    }
    inline void __basic_putint(int x) {
        if (x < 0) {
            x = -x;
            putchar('-');
        }
        if (x >= 10) __basic_putint(x / 10);
        putchar(x % 10 + '0');
    }

    inline void putint(int x, char external) {
        __basic_putint(x);
        putchar(external);
    }
}

int main(int argc, char *const argv[]) {
    #ifdef HANDWER_FILE
    freopen("testdata.in", "r", stdin);
    freopen("testdata.out", "w", stdout);
    #endif
    v = FastIO::getint();
    For (i, 1, 9) {
        q[i] = FastIO::getint();
        pq.push(std::make_pair(q[i], -i));
    }
    // 贪心选择当前最优
    while (!pq.empty()) {
        pair<int, int> pr = pq.top();
        pq.pop();
        ans[-pr.second] = v / pr.first;
        v %= pr.first;
    }
    q[0] = 2147482333;
    // 进行替换
    Bak (i, 9, 1) {
        int tmp = 0;
        Bak (j, i - 1, 1) {
            if (ans[j] && q[j] < q[tmp]) tmp = j;
        }
        if (!tmp) continue;
        while (ans[tmp] && v && v >= q[i] - q[tmp]) 
            v -= q[i] - q[tmp], ++ans[i], --ans[tmp];
    }
    bool Printed = false;
    Bak (i, 9, 1) {
        while (ans[i]) {
            FastIO::__basic_putint(i);
            --ans[i];
            Printed = true;
        }
    } 
    // 程序并没有正确答案,输出-1
    if (!Printed) puts("-1");
    return 0;
}

Guess you like

Origin www.cnblogs.com/handwer/p/11745409.html