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

f s(i) l s(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 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.

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

题意:n张唱片,每张都有长度和频率,要求题目中公式值最小。求一个排序,输出该排序下第m张CD的id。

思路:贪心,长度越长放越后面,频率越低放越后面,所以按len/p去排序即可。

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 1<<16; int n, m;
struct CD {
int id;
double len;
double p;
} cd[N]; int cmp(CD a, CD b) {
return (a.len / a.p - b.len / b.p) < 1e-9;
} int main() {
while (~scanf("%d", &n)) {
for (int i = 0; i < n; i ++)
scanf("%d%lf%lf", &cd[i].id, &cd[i].len, &cd[i].p);
sort(cd, cd + n, cmp);
scanf("%d", &m);
printf("%d\n", cd[m - 1].id);
}
return 0;
}

最新文章

  1. Double的精度问题
  2. 关于NODE NPM 输入命令后没反应的问题
  3. ML_R kNN
  4. .NET study collection links
  5. 【linux】常用网站
  6. centos文件权限详解
  7. order by按照指定记录排序
  8. 微信小程序开发 (资料汇总,谁还没被坑过?希望助你绕过一些坑)
  9. [LeetCode] Card Flipping Game 翻卡片游戏
  10. github项目
  11. 在windows下使用jenkins部署docker容器
  12. 刘志梅201771010115.《面向对象程序设计(java)》第六周学习总结
  13. Java中的枚举Enum
  14. android 自定义命名空间 http://schemas.android.com/apk/res-auto
  15. 【Java并发编程】2、无锁编程:lock-free原理;CAS;ABA问题
  16. How to center body on a page?
  17. [转]How to Use xp_dirtree to List All Files in a Folder
  18. Linux下的堆伪造漏洞利用技术(new unlink)
  19. vue-cli项目里npm安装使用elementUI
  20. linux c 执行新程序

热门文章

  1. BlockingQueue接口
  2. 第一章 初识Lucene
  3. java虚拟机涉及内存溢出
  4. Android导入项目时出现红色感叹号
  5. C#操作Excel文件(转)
  6. js插件zClip实现复制到剪贴板功能
  7. 全排列算法之Perm算法实现
  8. 使用django+mysql+scrapy制作的一个小说网站
  9. Html中input标签的使用
  10. asp.net mvc+EF 递归生成树结构返回json