A-Canoe (greedy)
n individuals, who are known to weigh each person. The canoe is load-bearing, with a maximum of two persons per canoe, and one person or two persons can be seated. It is clear that the total weight does not exceed the load of the canoe, assuming that each person weighs less than the canoe load, how many canoes do I need at least?
Input
The first line consists of two positive integers N (0
Output
An integer line indicates the minimum number of canoes required.
Sample Input
3 6
1
2
3
Sample Output
2
Solve:
greedy water problem , each time just take the biggest with the smallest combination of comparison on the line
Code:
1#include <bits/stdc++.h>2 using namespacestd;3typedefLong LongLL;4 Static Const intMAXN = 1e4 +Ten;5 LL N, m;6LL DATA[MAXN] = {0};7 intMain ()8 {9scanf"%lld%lld", &n, &m);Ten for(inti =0; I < n; ++i) Onescanf"%lld", Data +i); ASort (data, data +n); - intL =0, R = N-1; - intAns =0; the while(L <=R) - { - if(Data[l] + data[r] <=m) -++l; +--r, + +ans; - } +printf"%d\n", ans); A}
View CodeB-Walk lattice (water problem, simulation)
with a numbered 1-n of n squares, the robot goes backwards from the 1th-cell order to the N-grid and needs to go out of the N-grid. The robot has an initial energy, each lattice corresponds to an integer a[i], which represents the energy value of the lattice. If A[i] > 0, the robot goes to this lattice can get a[i] energy, if a[i] < 0, go to this lattice need to consume the corresponding energy, if the robot's energy < 0, can not continue to move forward. Ask the robot how much initial energy it needs at least to complete the journey.
For example:n = 5. {1,-2,-1,3,4} requires a minimum of 2 initial energy to go from number 1th to Grid 5th. The energy changes on the way are as follows 3 1 0 3 7.
Input
Line 1:1 number n, indicating the number of squares. (1 <= N <= 50000) 2-n + 1 lines: 1 numbers per line a[i], indicating the energy value in the lattice ( -1000000000 <= a[i] <= 1000000000)
Output
output 1 numbers, the minimum amount of initial energy required to go from 1 to N.
Sample Input
5
1
-2
-1
3
4
Sample Output
2
Solve:
Real water problem , Direct simulation can be
Code:
1#include <bits/stdc++.h>2 using namespacestd;3typedefLong LongLL;4 intN;5 intMain ()6 {7scanf"%d", &n);8LL oil =0; LL ans =0; LL x;9 for(inti =0; I < n; ++i)Ten { Onescanf"%lld", &x); AOil + =x; - if(Oil <0) - { theAns + = (-Oil ); -Oil =0; - } - } +printf"%lld\n", ans); -}
View Code
2017-4-14: Algorithm Path # # (greedy, drawer principle, computational geometry)