Maximizing the minimum

POJ3258

Meaning of the questions:

There are a bunch of cattle to cross the river, river length is L, there are N middle of the river stone, only bovine stepping stones, Q M after removing stones (stone removing the M random manner), could bovine minimum spacing stone to go, the biggest one is that how much output distance.

First and last two stones can not be removed.

solution:

To maximize the minimum pitch stone

Half of the distance traversed from left to right all the stones (inclusive), is determined within the move distance kept stone, the stone when the total number of errors that is greater than M.

code show as below:

 1 int L, M, N;
 2 int pos[MAXN];
 3 
 4 bool C(int x) {
 5     int sum = 0, last = 0;
 6     for (int i = 1; i <= N + 1; i++) {
 7         if (pos[i] - pos[last] < x) {
 8             sum++;
 9             if (sum > M) {
10                 return false;
11             }
12         } else {
13             last = i;
14         }
15     }
16     return true;
17 }
18 
19 void jojo() {
20     sort(pos, pos + N + 1);
21     int lb = 0, ub = L + 1;
22     while (ub - lb > 1) {
23         int mid = (ub + lb) / 2;
24         if (C(mid)) {
25             // cout << "true: " << mid << endl;
26             lb = mid;
27         } else {
28             // cout << "false: " << mid << endl;
29             ub = mid;
30         }
31     }
32     cout << lb << endl;
33 }
34 
35 int main() {
36 #ifndef ONLINE_JUDGE
37     freopen("input.txt", "r", stdin);
38 #endif  // !ONLINE_JUDGE
39     L = READ(), N = READ(), M = READ();
40     for (int i = 1; i <= N; i++) pos[i] = READ();
41     pos[0] = 0;
42     pos[N + 1] = L;
43     jojo();
44     return 0;
45 }

POJ3273

Meaning of the questions:

There are number N, the order required into M groups, so that the number of M and the maximum value of minimum group.

solution:

Minimize the maximum value

Binary number M and, through all the numbers, accumulation, greater than two when it increments the counter value of 1, when the last if the counter is greater than M, was illegal.

In fact, there are two cases here, if you want to accurately divided into 7 groups:

1. After then, if the front is divided into three groups, the number behind the total of three, each less than two scores, divided into 4 groups but not how to do?

2. Or already become divided into six groups, and there are three numbers, each of which exceeds the two scores, how do?

In the first case: our determination is true, then explain two scores can be further reduced.

For the second case: less certain than the M-1, it returns false

Another point to note is that for half of the value, if we want to narrow the range, then the upper and lower bounds of the range of 1 to expand, to avoid WA.

Gee, is simply a.

 1 int N, M;
 2 int d[MAXN];
 3 int mx, mn;
 4 
 5 bool C(int x) {
 6     int last = 0, num = 0, sum = 0;
 7     for (int i = 0; i < N; i++) {
 8         if (sum + d[i] <= x) {
 9             sum += d[i];
10         } else {
11             num++;
12             sum = d[i];
13         }
14     }
15     if (num + 1 > M) {
16         return false;
17     } else {
18         return true;
19     }
20 }
21 
22 void solve() {
23     int ub = mx + 1, lb = mn;
24     while (ub - lb > 1) {
25         int mid = (ub + lb) >> 1;
26         if (C(mid)) {
27             ub = mid;
28         } else {
29             lb = mid;
30         }
31     }
32     cout << ub << endl;
33     return;
34 }
35 
36 int main() {
37 #ifndef ONLINE_JUDGE
38     freopen("input.txt", "r", stdin);
39 #endif  // !ONLINE_JUDGE
40     while (scanf("%d%d", &N, &M) != EOF) {
41         mx = 0, mn = 0;
42         for (int i = 0; i < N; i++) {
43             scanf("%d", &d[i]);
44             mx += d[i];
45             mn = min(mn, d[i]);
46         }
47         solve();
48     }
49     return 0;
50 }

POJ3104

Meaning of the questions:

Each piece of clothing has a certain unit of water, does not apply in the case of the dryer, each piece of clothing natural wastage per unit of water a minute, but the loss of K units of water per minute if you use a dryer, but unfortunately is only one station in the dryer, each dryer while only a clothes dryer, dry N pieces of clothing in order to ask how long it takes a minimum?

solution:

First-half first consider whether or not there is a monotonic relationship is more like how the how.

This question, more time, more can be drying, so half the time.

Sequence of a first row of water, each two divided time t, that is to say in the absence of the dryer, the clothes can be reduced per t water. So smaller than t would not drying clothes, bigger than t will certainly be dried.

Calculated for each piece of clothing drying time, i.e. $ \ frac {a [i] -t} {k-1} $, because this minute drying is selected, based on the original subtracting means k- 1 water, dried and then a remainder will more one minute. The last statistics about the drying time is not greater than t on the line.

Complexity is bipartite log1e9, each iteration is O (n), it can be over.

Pot this question: the first use of LL, because the worst case k is 2, the amount of water all 1e9. Then special judge at k = 1, or in addition to 0 will re

 1 LL N, K;
 2 LL a[MAXN];
 3 LL mx = -1;
 4 
 5 bool C(LL t) {
 6     int i = lower_bound(a, a + N, t) - a;
 7     LL sum = 0;
 8     for (; i < N; i++) {
 9         if ((a[i] - t) % (K - 1) == 0) {
10             sum += (a[i] - t) / (K - 1);
11         } else {
12             sum += ((a[i] - t) / (K - 1) + 1);
13         }
14     }
15     if (sum > t) {
16         return false;
17     } else {
18         return true;
19     }
20 }
21 
22 void solve() {
23     sort(a, a + N);
24     if (K == 1) {
25         printf("%lld\n", mx);
26         return;
27     }
28     LL ub = LLINF, lb = 0;
29     while (ub - lb > 1) {
30         LL mid = (ub + lb) >> 1;
31         if (C(mid)) {
32             ub = mid;
33         } else {
34             lb = mid;
35         }
36     }
37     printf("%lld\n", ub);
38     return;
39 }
40 
41 int main() {
42 #ifndef ONLINE_JUDGE
43     freopen("input.txt", "r", stdin);
44 #endif  // !ONLINE_JUDGE
45     scanf("%lld", &N);
46     for (int i = 0; i < N; i++) {
47         scanf("%lld", &a[i]);
48         mx = max(a[i], mx);
49     }
50     scanf("%lld", &K);
51     solve();
52     return 0;
53 }

 

Guess you like

Origin www.cnblogs.com/romaLzhih/p/12333352.html