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. nyoj 42

    #include <iostream> #include <stdio.h> #include <cstring> #include <algorithm&g ...

  2. uploadify在asp.net中的试用小结

    花了差不多一下午的时间,总算把uploadify插件运行起来,在此对自己遇到的问题以及过程做一个小结. 一.使用步骤 1.在官网下载最新的插件包,并将包解压. 2.新建asp.net web项目,将解 ...

  3. SharePoint中获取当前登录的用户名几种方式

    第一种方法: System.Web.HttpContext.Current.User.Identity.Name.ToString();或者: SPContext.Current.Site.OpenW ...

  4. Ecstore获取dbschema内容?

    有时候在使用dbschema的时候,需要获取dbschema的结构.例如: 那么,我们可以这样写: 这样我就能获得 称呼 这个数组    

  5. HTML5触摸屏touch事件使用实例1

    1.源码: <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> ...

  6. MySQL查看数据库大小、表大小和最后修改时间

    查看数据库表基本信息. select * from information_schema.TABLES where information_schema.TABLES.TABLE_SCHEMA = ' ...

  7. JavaAppArguments.java程序的更改

       此程序模仿JvaAppArgyments.java编写,从命令行接受多个数字,求和之后输出结果.  设计思想:命令行参数都是字符串,可以考虑用 Integer.parseInt(args[]) ...

  8. linux内核学习之进程管理------task_struct结构体

    struct task_struct { volatile long state;    /* -1 unrunnable, 0 runnable, >0 stopped */ struct t ...

  9. UML中聚合和组合的区别

    聚合 聚合其实是关联的一个特例,表示了两个对象之间“part of(部分-整体)”的关系,是一种弱关联,部分的生命周期可以超越整体.在UML中,用整体端有空心菱形箭头的双向关联修饰聚合.聚合的示例如下 ...

  10. discuz 和 wordpress 整合注意问题

    要先在应用列表里添加自己本身的应用即要保证同步操作,本身也是一个应用,并保证通信成功: 应用和ucenter不在一个机器上,IP都要设定: discuz/uc_client/ 下要有一个接收数据的da ...