HOJ 2143 - Songs(贪心)
2017-12-04 15:50
323 查看
上周小测的ProblemD,由于上周我去CCPC Final当志愿者去了,所以东西不怎么能更得及时。。。
到时候写一篇志愿者感想,嘿嘿嘿,见到了好多大神!
传送门:http://acm.hit.edu.cn/hojx/showproblem/2143/
2143 - Songs
Time limit : 1 s Memory limit : 32 mb
Submitted : 323 Accepted : 137
Submit
Problem 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 Ss(1), …, Ss(n) on the tape then the function that must be minimized is
where fs(i) is the frequency of playing the ith song and l is the length of the song. Can you help John?
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. 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.
Input
Output
Sample Input
5
1 10 45.5
2 5 20
30 20 10
400 50 35
15 17 89.9
3
Sample Output
2
小测的时候还没有读这道题,要是看了而且看懂了就会很快发现思路。其实这就是一道贪心题。
公式的意思是,一首歌曲的频率越高,长度越短,那这首歌的价值就越大,排在前面。所以只要存好价值 = 频率 / 长度;排序就好了。
AC代码如下:
到时候写一篇志愿者感想,嘿嘿嘿,见到了好多大神!
传送门:http://acm.hit.edu.cn/hojx/showproblem/2143/
2143 - Songs
Time limit : 1 s Memory limit : 32 mb
Submitted : 323 Accepted : 137
Submit
Problem 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 Ss(1), …, Ss(n) on the tape then the function that must be minimized is
where fs(i) is the frequency of playing the ith song and l is the length of the song. Can you help John?
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. 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.
Input
Output
Sample Input
5
1 10 45.5
2 5 20
30 20 10
400 50 35
15 17 89.9
3
Sample Output
2
小测的时候还没有读这道题,要是看了而且看懂了就会很快发现思路。其实这就是一道贪心题。
公式的意思是,一首歌曲的频率越高,长度越短,那这首歌的价值就越大,排在前面。所以只要存好价值 = 频率 / 长度;排序就好了。
AC代码如下:
#include <cstdio> #include <cstring> #include <algorithm> #define maxn 66000 using namespace std; struct song{ int i; int len; double f; double val; }songs[maxn]; int cmp(song a, song b) { return a.val > b.val; } int main() { int n; while(scanf("%d", &n) != EOF && n) { for(int i = 0; i < n; i++) { scanf("%d %d %lf", &songs[i].i, &songs[i].len, &songs[i].f); songs[i].val = songs[i].f / songs[i].len; } int m; scanf("%d", &m); sort(songs, songs+n, cmp); printf("%d\n", songs[m-1].i); } return 0; }
相关文章推荐
- Hoj 2143 Songs
- 1346 - Songs (贪心)
- uva 1346 - Songs(贪心)
- uva 1346 - Songs(贪心)
- HOJ P-2244 Get the Colors(模拟+贪心)
- HOJ 3287 Cables(贪心)
- 【UVA】1346 - Songs(贪心)
- hoj 2430 Counting the algorithms(树状数组,贪心)
- Radar Installation 贪心 hoj
- uva 1346 Songs(贪心)
- UVa 1346 Songs (贪心好题)
- hoj2143 Songs 贪心
- Songs (UVA-1346)贪心
- UVA 1346 - Songs (贪心)
- 1346 - Songs (贪心)
- POJ 2675 Songs (贪心)
- 【hoj】2160 bin packing 二分、贪心
- UVA - 1346 Songs 贪心
- uva 1346 - Songs(贪心)
- UVA - 1346 Songs 简单贪心