Saruman's army code (c)
Address: http://blog.csdn.net/caroline_wendy
Question: There are N points in a straight line. Each point must be marked with a distance of 0 within the area of R.
Add as few markup points as possible. How many points should be marked at least?
Greedy Algorithm, FromLeftmost VertexStart, search in sequenceDistance is RThe vertex to be marked until the end.
Code:
/* * main.cpp * * Created on: 2014.7.17 * Author: spike *//*eclipse cdt, gcc 4.8.1*/#include <stdio.h>#include <limits.h>#include <utility>#include <queue>#include <algorithm>using namespace std;class Program {static const int MAX_N = 10000;int N=6, R=10;int X[MAX_N] = {1, 7, 15, 20, 30, 50};public:void solve() {sort(X, X+N);int i=0, ans=0;while (i<N) {int s = X[i++];while (i<N&&X[i]<=s+R) i++;int p = X[i-1];while (i<N&&X[i]<=p+R) i++;ans++;}printf("result = %d\n", ans);}};int main(void){Program P;P.solve(); return 0;}
Output:
result = 3