[Programmers] 예산

Time Lapse :1hour 10min 0sec

solution.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <string>
#include <vector>
#include <algorithm>
//1:10:00
using namespace std;
vector<long long> psum;
vector<long long> need_total;
void is_Possible(vector<int> budgets){
long long total = 0;
for(int i=0;i<budgets.size();i++){
total += budgets[i];
psum.push_back(total);
need_total.push_back(total+(budgets[i]*(budgets.size()-1-i)));
}
}
int solution(vector<int> budgets, int M) {
sort(budgets.begin(),budgets.end());
is_Possible(budgets);
if(psum.back()<=(long long)M){
return budgets.back();
}
if(need_total[0]>(long long)M){
long long min_Val = need_total[0];
for(int i=1;i<100000;i++){
min_Val -= budgets.size();
if(min_Val<=M)
return budgets[0]-i;
}
}
int idx, left;
long long cur_total;
for(int i=0;i<need_total.size();i++){
if(need_total[i]>M){
idx = i-1;
left = need_total.size()-i;
cur_total = need_total[idx];
break;
}
}
for(int i=0;i<10000;i++){
cur_total += left;
if(cur_total>M)
return budgets[idx]+i;
}
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/Programmers/budget/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.