OCAC Summer field B title game of the second daily watering solution to a problem

Daily watering
original title link: http: //codeforces.com/problemset/problem/149/A
[title Description
December 2018 31 evening, when Cong Cong parents after the flowers finish pouring water on the balcony, which the couple ready to go to Antarctica to see the aurora, and then go home in the morning of January 2020 1.
They left one at home Cong Cong, Cong Cong and to arrange a mission: to water the flowers!
Cong Cong know, in the i-th month, if it insists to spend every morning, noon, night, each pouring water, then at the end when the president of the flower ai cm high;
if the i-th month, Cong Cong We do not insist on this, then (such as oversleeping in the morning the day), when the high end of the flower is not the president.
Cong Cong know if when parents come back, spend long high height less than k cm, its parents will find that he did not take to water the flowers.
Will Cong Cong need at least a few months pouring flowers, make flower-growing height can be greater than equal to k cm (that is to say: Mom and Dad came back after the discovery took taller least k cm).
[Input format
of the first line of the input contains an integer k (0 <= k <= 100).
The second line 12 comprises the integer input, with a space between two positive numbers, the first of which is used to represent integer i ai (1 <= ai <= 100).
[] Output format
output contains an integer that indicates the number of months Congcong requires a minimum of watering.
If you can not let Cong Cong matter how flowers grow by at least k cm, output -1.
Sample input [1]
. 5
1 1 1 1 2 2 2 1 1 1 2. 3
[1] Output Sample
2
[2] Sample input
0
000000011230
[Sample 2] outputs
0
[3] Sample input
. 11
. 1. 1. 1. 1. 5. 1. 1. 4. 4. 1. 1. 1
[3] Output Sample
3
[Title] Analysis
of this problem involving algorithms: greedy.
We just need to set a variable tot, tot initially equal to 0,
then a decreasing order of the array, then an array of values successively added to a TOT, known tot> = k.
If k == 0, then there is no need Congcong watering directly outputs 0;
if the end of the traversal tot or smaller than k, it outputs -1.
Codes are as follows:

#include <bits/stdc++.h>
using namespace std;

int k, tot, a[12];
inline bool cmp(int a, int b) { return a > b; }

int main() {
    cin >> k;
    for (int i = 0; i < 12; i ++) cin >> a[i];
    sort(a, a+12, cmp);
    if (k == 0) {
        cout << 0 << endl;
        return 0;
    }
    for (int i = 0; i < 12; i ++) {
        tot += a[i];
        if (tot >= k) {
            cout << i + 1 << endl;
            return 0;
        }
    }
    cout << -1 << endl;
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/ocac/p/11130963.html