Time limit : 1 s Memory limit : 32 mb
where fs(i) is the frequency of playing the ith song and l is the length of the song. Can you help John?
Input
The program input is from standard 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 given 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
心路历程:
考试的时候并没有看这道题,考完后发现并不太难(我不会告诉你我当时是
读不懂题才没做出来的)。题目的大概意思是有一个dj大佬,控制播放不
同的音乐,每首音乐s含有频率f和长度音乐l,播放的次序可以变化,进而导
致的结果是总的播放时间不同。觉定
总时间的函数如上(我不会告诉你考完试后这个公式我依旧看不懂感谢学
长),注意这个公式的意思不是两个和取乘积,而是一个数乘上一个乘积后
,对这些结果再进行取和(这句话是给自己强调的orz)。既然没有总的时
间限制,我们可以用贪心来解决这道题。和之前在洛谷上国王分金币的题
类似,因为有两个影响因素,所以我们需要先确定这两个影响因素对答案
的影响。
我们不妨设现在有两首音乐1,2,他们的频率为a1,a2,长度为b1,b2
假设我们让1先播放,2后播放
那么可得这种情况的总时间t1=a1*b1+a2(b1+b2)
假设我们让2先播放,1后播放,
那么可得这种情况的总时间t2=a2*b2+a1(b1+b2)
两式做差为t1-t2=a2b1-a1b2
如果我们让第一种情况的总时间最小,那么t1-t2<0,所以可得a1/b1>a2/b2,
由此可得我们把ai/bi的值较大的排在前面,那么总时间将会越小。
代码:
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
struct song//注意到因为f和l会相除,所以我们可以直接double f,或者int f,后面1.0
{
int num;
double f,l;
}s[70000];
bool cmp(song a,song b)//由大到小排序
{
return (a.f/a.l)>(b.f/b.l);
}
int main()
{
int n;
while(scanf("%d",&n)==1)
{
int i,k;
for(i=0;i<n;i++)
{
scanf("%d%lf%lf",&s[i].num,&s[i].l,&s[i].f);
}
scanf("%d",&k);
sort(s,s+n,cmp);
printf("%d\n",s[k-1].num);//注意角标
}
return 0;
}
期末不挂
![奋斗](http://static.blog.csdn.net/xheditor/xheditor_emot/default/struggle.gif)