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;
}

1346 - Songs (贪心)的更多相关文章

  1. uva 1346 - Songs(贪心)

    题目链接:uva 1346 - Songs 题目大意:John Doe 是一个著名的DJ,现在他有n首播放个曲, 每首歌曲有识别符key,歌曲长度l,以及播放频率q.想在John Doe 想将磁带上的 ...

  2. UVA - 1346 Songs (贪心+排序)

    题意:已知每首歌的标号,长度和播放频率,求一种播放顺序,使得最小,并且输出该播放顺序下第t首歌的标号. 分析: 1.长度越短,播放频率越大的歌排在前面,上式越小. 2.s(i)表示的是当前播放顺序下这 ...

  3. UVa 1346 - Songs

    题目大意:n张唱片,每张都有长度和频率,要求题目中公式值最小.求一个排序,输出该排序下第m张CD的id. 思路:贪心,长度越长放越后面,频率越低放越后面,所以按len/p去排序即可. #include ...

  4. 【CF1015C】Songs Compression(贪心)

    题意: 给定n和m,n组(a[i],b[i]),每一组a[i]可以压缩为b[i],求最少只需要压缩几个,使得m可以存下所有数据,无解输出-1 思路:按差贪心,排序 #include<cstdio ...

  5. C. Songs Compression(简单贪心)

    水题 #include<iostream> #include<algorithm> using namespace std; #define LL long long ; st ...

  6. BZOJ 1029 建筑抢修 贪心+堆

    又搞了一晚上OI,编了两道BZOJ和几道NOI题库,临走之前写两篇感想 noip越来越近了,韩大和clove爷已经开始停课虐我们了... 1029: [JSOI2007]建筑抢修 Time Limit ...

  7. Codeforces Round #375 (Div. 2) A B C 水 模拟 贪心

    A. The New Year: Meeting Friends time limit per test 1 second memory limit per test 256 megabytes in ...

  8. Codeforces Round #164 (Div. 2) E. Playlist 贪心+概率dp

    题目链接: http://codeforces.com/problemset/problem/268/E E. Playlist time limit per test 1 secondmemory ...

  9. C. Playlist Educational Codeforces Round 62 (Rated for Div. 2) 贪心+优先队列

    C. Playlist time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

随机推荐

  1. nginx 1.安装

    nginx 1.安装 nginx的众多优点这里就不多说了,直接开始吧. 基本依赖 yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel ...

  2. java获取字符串格式日期向前或向后n天的日期

    private void setTilteMessage(){          BaseDao dao = new BaseDao();          String titleData = da ...

  3. .net中Web.config文件的基本原理及相关设置

    11.7  使用web.config配置文件 Web配置文件web.config是Web 应用程序的数据设定文件,它是一份 XML 文件,内含 Web 应用程序相关设定的 XML 标记,可以用来简化  ...

  4. iptables里filter表前面几个数字的意思

    一般的linux系统iptables配置文件filter表前面都带下面三行,但是具体是什么意思呢! *filter:INPUT ACCEPT [0:0]:FORWARD ACCEPT [0:0]:OU ...

  5. 客户端JavaScript(window、document、element)

    一.window对象是所有客户端JavaScript特性和API的主要接入点,用window来引用它. 属性:location属性(引用Location对象,当前显示在窗口的URL).document ...

  6. Python文件之----CSV

    # -*- coding:utf-8 -*- ''' Created on 2015年4月20日 @author: liuxue ''' import csv import sys reload(sy ...

  7. oracle单行函数之数字函数

    round--四舍五入函数 trunc--截断函数 mod--求余函数 round和trunc除了可以操作数字外还可以操作日期. Demo SQL),),),),) from dual ; ) ) ) ...

  8. String类扩展

    String s1=new String("fsdfsd"); String s2=new String("fsdfsd"); String a1=" ...

  9. java中的hashcode方法作用以及内存泄漏问题

    本文装载:http://hi.baidu.com/iduany/item/6d66dfc9d5f2da1650505870 hashCode()方法的作用&使用分析 一直以来都想写篇文章来说明 ...

  10. nodejs学习记录

    一.环境搭建 1.安装express 局部安装 npm i express 全局安装 npm i -g express (命令行express无法使用) 环境变量 npm i -g express-g ...