zoukankan      html  css  js  c++  java
  • UVa1346 Songs

    John Doe is a famous DJ and, therefore, has the problem of optimizing the placement of songs on his tapes. For a given tape and for each song on that tape John knows the length of the song and the frequency of playing that song. His problem is to record the songs on the tape in an order that minimizes the expected access time. If the songs are recorded in the order S(s1),..., Ss(n) on the tape then the function that must be minimized is

     

     

    $\displaystyle \sum^{{n}}_{{i=1}}$fs(i)$\displaystyle \sum^{{s(i)}}_{{j=1}}$ls(j)

    where fs(i) is the frequency of playing the i -th song and l is the length of the song. Can you help John?

     

    Input 

    The program input is from a text file. Each data set in the file stands for a particular set of songs that must be recorded on a tape. A data set starts with the number N (fits a 16 bit integer) of songs. Follow Nthe song specifications, and in the end, a number representing the position of a song S on the optimized tape. A song specification consists of the song identifier (fits an integer), the length of the song (fits a 16 bit integer), and the frequency of playing the song (a floating-point number). The program prints the identifier of the song S .

    White spaces can occur freely in the input. The input data are correct and terminate with an end of file.

     

    Output 

    For each set of data the program prints the result to the standard output from the beginning of a line.

     


    Note: An input/output sample is in the table below. There is a single data set that contains 5 song specifications. The first song has the identifier 1, length 10 and playing frequency 45.5 etc. The result for the data set is the identifier of the 3rd song on the optimized tape. It is 2 for the given example.

     

    Sample Input 

     

    5
    1      10     45.5
    2      5      20 
    30     20     10 
    400    50     35 
    15     17     89.9
    3
    

     

    Sample Output 

     

    2
    题解:非常水的贪心,可以用相邻交换法证明.
    View Code
     1 #include<stdio.h>
     2 #define MAXN 0x1001B
     3 typedef struct
     4 {
     5     int id;
     6     int len;
     7     double rate;
     8 } NODE;
     9 NODE a[MAXN];
    10 void qsort(int l,int r)
    11 {
    12     int i,j;
    13     NODE mid;
    14     NODE temp;
    15     i=l;
    16     j=r;
    17     mid=a[(l+r)/2];
    18     while(i<=j)
    19     {
    20         while(a[i].len*mid.rate<mid.len*a[i].rate) i++;
    21         while(a[j].len*mid.rate>mid.len*a[j].rate) j--;
    22         if(i<=j)
    23         {
    24             temp=a[i];
    25             a[i]=a[j];
    26             a[j]=temp;
    27             i++;
    28             j--;
    29 
    30         }
    31     }
    32     if(i<r) qsort(i,r);
    33     if(j>l) qsort(l,j);
    34 }
    35 int n,m;
    36 int main(void)
    37 {
    38     int i;
    39     while(scanf("%d",&n)==1)
    40     {
    41         for(i=0; i<n; i++)
    42             scanf("%d%d%lf",&a[i].id,&a[i].len,&a[i].rate);
    43         scanf("%d",&m);
    44         qsort(0,n-1);
    45         printf("%d\n",a[m-1].id);
    46     }
    47     return 0;
    48 }
     
  • 相关阅读:
    hadoop2.7的目录结构
    ubuntu16.04上Eclipse和hadoop配置
    ubuntu16.04更改源为阿里源
    ubuntu16.04搭建hadoop集群环境
    ubuntu自动登录tty1(shell,text)配置
    Android六大基本布局
    java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.bjsxt.mapper.PeopleMapper
    storm_分组策略
    storm_常用命令
    Storm集群搭建
  • 原文地址:https://www.cnblogs.com/zjbztianya/p/2973825.html
Copyright © 2011-2022 走看看