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 in the area within R.
Join as few as possible? Mark points. How many points should be marked?
Greedy Algorithm, FromLeftmost VertexStart, search in sequenceDistance is RNeed to join? Marked points 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
Programming Algorithm-Saruman Army (Saruman & #39; s army) code (c)