Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=4092 thought: Directly look at this and the formula, remember a (i) =f (i) Σl (j) (1<=j<=i), then Sum=σa (i) (not to be confused by subscript, S (i) can be completely replaced by i).
Set sum has reached the minimum,
Note Sum1 = A (i) +a (i+1) = (f (i) +f (i+1)) * (L (1) +...+l (i-1)) + f (i) *l (i) + f (i+1) *l (i) + f (i+1) *l (i+1)
Swap f (i) and F (i+1), Exchange L (i) and L (i+1), sum2= (f (i) +f (i+1)) * (L (1) +...+l (i-1)) + f (i) *l (i) + f (i) *l (i+1) + f (i+1) *l (i+1)
Because the sum is the smallest, there must be sum1<=sum2, so f (i+1) *l (i) <=f (i) *l (i+1)
(It can also be understood that the greater the frequency, the shorter the length, that is, f (i)/L (i) the larger the position, the more forward)
Complete code:
01./*0.032s*/
.
#include <bits/stdc++.h>
04.using namespace std;
06.struct node
07.{
an int id; double L, F; BOOL Operator < (const node& a) const
. {a
. return A.F * L < F * A.L;
a[65540];
16.int Main ()
17.{ int n, I, S;
While (~scanf ("%d", &n))
. {for (i = 1; I <= n; ++i)
. scanf ("%d%lf%lf", &a[i].id, &A[I].L, &A[I].F); scanf ("%d", &s); Nth_element (A + 1, a + s, a + n + 1); printf ("%d\n", a[s].id);
. return 0;
28.}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/