-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUVA-11413.cpp
66 lines (53 loc) · 1.37 KB
/
UVA-11413.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <cstdio>
#include <iomanip>
#define ERRO 0.001
using namespace std;
int f (int x, int *vessel, int n, int m){
int ves=0;
int bucket=x, nbuckets=1;
while(ves<n){
//printf("%d %d\t", nbuckets, bucket);
if(x<vessel[ves]) return -1;
if(nbuckets>m) return -1;
if(bucket>=vessel[ves]){
bucket-=vessel[ves];
++ves;
}
else{
++nbuckets;
bucket=x-vessel[ves];
++ves;
}
}
if(nbuckets>m) return -1;
return 1;
}
// versao iterativa....
int bisecao(int min, int max, int *vessel, int n, int m){
int mid = min, r=-1;
while (max-min>1){
mid = (max+min)/2;
//printf("min = %d *** max = %d **** mid = %d \n", min, max, mid);
r=f(mid, vessel, n, m);
//cout << "\nresult: " << r << endl;
if (r > 0 ) // AQUI SIM.. FUnciona para QQ função...
max = mid; // o valor está muito grande... diminui !!!!
else min = mid; // o valor esta muito baixo... aumenta !!!!
}
return max;
}
int main(int argc, char const *argv[]){
int max, n, m;
while(scanf("%d", &n)!=EOF){
cin >> m;
int vessel[n];
max=0;
for(int j=0; j<n; ++j){
cin >> vessel[j];
max+=vessel[j];
}
cout << bisecao(0, max, vessel, n, m) << endl;
}
return 0;
}