Give the coordinates of n trees, the height of the tree and the value of the tree. Cut some (the whole tree) from these trees and make a fence to enclose the remaining trees, minimize the value of the tree consumed. Output the desired tree and the length of the remaining material. (If the cut-off value is the same, the cut-off number is smaller) (2 <= n <= 15)
--> The binary ing is used to enumerate the situations of each type of tree cutting. In each case, the convex packet is obtained, the perimeter of the convex packet is obtained, and the result is judged. (Submit with G ++ here)
Note: 1. If the cut value is the same and the number is the same, the cut number should be small;
2. "%. 2f" is used for final output, and "%. 2lf" is never used. The difference between one more l and one more l is big !!!
# Include <cstdio> # include <cmath> # include <algorithm> using namespace std; const int maxn = 15 + 1; const int INF = 1000000000; struct Tree {double x; double y; int v; int l; Tree () {} Tree (int xx, int yy): x (xx), y (yy) {} bool operator <(const Tree & e) const {return x <e. x | (x = e. x & y <e. y) ;}} t [maxn]; Tree operator-(Tree A, Tree B) {return Tree (. x-B. x,. y-B. y);} double Cross (Tree A, Tree B) {return. x * B. y-. y * B. x;} double Dis (Tree A, Tree B) {return hypot (. x-B. x,. y-B. y);} int ConvexHull (Tree * p, int n, Tree * ch) // returns the convex hull {sort (p, p + n); int m = 0; for (int I = 0; I <n; I ++) {while (m> 1 & Cross (m-1]-ch [m-2], p [I]-ch [m-2]) <0) m --; ch [m ++] = p [I];} int k = m; for (int I = n-2; I> = 0; I --) {while (m> k & Cross (m-1]-ch [m-2], p [I]-ch [m-2]) <0) m --; ch [m ++] = p [I];} if (n> 1) m --; return m;} double Perimeter (Tree * ret, int m) {double perimeter = 0; for (int I = 1; I <m; I ++) perimeter + = Dis (ret [I], ret [I-1]); perimeter + = Dis (ret [0], ret [s-1]); return perimeter;} int main () {int n, I, Case = 1, bit; while (scanf ("% d", & n) = 1 & n) {for (I = 0; I <n; I ++) scanf ("% lf % d", & t [I]. x, & t [I]. y, & t [I]. v, & t [I]. l); int min_val = INF; // minimum value int min_cnt = INF; // number of trees cut down when the minimum value is cut down; double exc_len = 0; // int ans = 0 after the barrier is finished; // The method for cropping the remaining wood length. The binary ing is for (bit = 0; bit <(1 <n); bit ++) // enumerate each method of Tree cutting {Tree buf [maxn], ret [2 * maxn]; // buf buffer. ret calculates the vertex group after the convex packet, double cut_len = 0; // The total length of the tree cut down by this method int cut_val = 0; // The total value of the tree cut down by this method int cut_cnt; // The total number of trees cut down by this method int m = 0; // The number of remaining trees after using this method for (I = 0; I <n; I ++) // scan this method {if (bit & (1 <I) // The cut {cut_len + = t [I]. l; cut_val + = t [I]. v;} else // The remaining {buf [m]. x = t [I]. x; buf [m ++]. y = t [I]. y ;}} if (cut_val> min_val) continue; cut_cnt = n-m; int cnt = ConvexHull (buf, m, ret ); // double perimeter = Perimeter (ret, cnt); // if (cut_len> = perimeter) // The cut-down tree meets the length requirement {if (cut_val <min_val | (cut_val = min_val & cut_cnt <min_cnt) {min_val = cut_val; min_cnt = cut_cnt; exc_len = cut_len-perimeter; ans = bit ;}}if (Case> 1) printf ("\ n"); printf ("Forest % d \ n ", case ++); printf ("Cut these trees:"); for (I = 0; I <n; I ++) if (ans & (1 <I )) printf ("% d", I + 1); printf ("\ n"); printf ("Extra wood: %. 2f \ n ", exc_len);} return 0 ;}