UVa 1346 Songs (greedy good question)

Source: Internet
Author: User

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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.