POJ 2675 Songs (贪心)

Songs

Time Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u

Description

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), ..., S s(n) on the tape then the function that must be minimized is
  n	s(i) 

 ∑fs(i)∑ls(j)

 i=1	j=1 

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

Input

Each data set in the input 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 N the 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. 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




题意:给你一份歌单,每首歌都有各自的序号、时长和被演奏的频率,要让歌单上面的所有歌都演奏完,且保证所需时间最少,问第k个演奏的歌曲序号是多少。


心得:只要看懂了题意,其实很简单的,直接贪心就搞了。。。但是一直WA,吐血。。。又一次不小心啊,频率必须为double,否则肯定过不了!!!


分析:很简单的贪心,只要把所有的歌按频率跟长度之比从大到小排序即可,这就是所要的顺序,直接输出第k个的序号即可。






AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define INF 0x7fffffff
using namespace std;
const int maxn = 100000 + 10;
typedef struct{ int num,len;  double f; }S;              //注意频率为double
S s[maxn];

bool cmp(S a,S b)
{
    return (a.f/(a.len*1.0)) > (b.f/(b.len*1.0));       // 按频率/长度从大到小排序 
}

int main()
{
    int n,m;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i = 0; i < n; i++)
            scanf("%d%d%lf",&s[i].num,&s[i].len,&s[i].f);
        scanf("%d",&m);
        sort(s,s+n,cmp);
        printf("%d\n",s[m-1].num);            //直接输出
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值