1346 - Songs (贪心)
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 (贪心)的更多相关文章
- uva 1346 - Songs(贪心)
题目链接:uva 1346 - Songs 题目大意:John Doe 是一个著名的DJ,现在他有n首播放个曲, 每首歌曲有识别符key,歌曲长度l,以及播放频率q.想在John Doe 想将磁带上的 ...
- UVA - 1346 Songs (贪心+排序)
题意:已知每首歌的标号,长度和播放频率,求一种播放顺序,使得最小,并且输出该播放顺序下第t首歌的标号. 分析: 1.长度越短,播放频率越大的歌排在前面,上式越小. 2.s(i)表示的是当前播放顺序下这 ...
- UVa 1346 - Songs
题目大意:n张唱片,每张都有长度和频率,要求题目中公式值最小.求一个排序,输出该排序下第m张CD的id. 思路:贪心,长度越长放越后面,频率越低放越后面,所以按len/p去排序即可. #include ...
- 【CF1015C】Songs Compression(贪心)
题意: 给定n和m,n组(a[i],b[i]),每一组a[i]可以压缩为b[i],求最少只需要压缩几个,使得m可以存下所有数据,无解输出-1 思路:按差贪心,排序 #include<cstdio ...
- C. Songs Compression(简单贪心)
水题 #include<iostream> #include<algorithm> using namespace std; #define LL long long ; st ...
- BZOJ 1029 建筑抢修 贪心+堆
又搞了一晚上OI,编了两道BZOJ和几道NOI题库,临走之前写两篇感想 noip越来越近了,韩大和clove爷已经开始停课虐我们了... 1029: [JSOI2007]建筑抢修 Time Limit ...
- 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 ...
- Codeforces Round #164 (Div. 2) E. Playlist 贪心+概率dp
题目链接: http://codeforces.com/problemset/problem/268/E E. Playlist time limit per test 1 secondmemory ...
- 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 ...
随机推荐
- easyui-tree绑定数据的几种方式
没想到easyui对json数据格式要求的那么严谨,折腾了半天 第一种直接使用标签方式,很容易就加载出来了: <ul class="easyui-tree"> < ...
- GridView and DropDownList
<form id="form1" runat="server"> <div> <asp:GridView runat=" ...
- js获取当前事件键盘按钮
用户名:<input type="text" name="username" id="username"><br /> ...
- error: expected constructor, destructor, or type conversion before '.' token
今天写代码是遇到这样一个问题error: expected constructor, destructor, or type conversion before '.' token:立马网上查,原来是 ...
- [转]链接分析算法之:主题敏感PageRank
原文引自:http://blog.csdn.net/hguisu/article/details/8005192,感谢 前面的讨论提到.PageRank忽略了主题相关性,导致结果的相关性和主题性降低, ...
- 实现DIV拖动
Ajax的到来让B/S中的客户端中开发有火了一把,网上出现了很多优秀的开源框架和UI,比较著名了有prototype,YUI,GWT等,但很多时候发现这些东西很难用到你的系统之中,有时候你的系统仅仅需 ...
- IMAGE_SECTION_HEADER
typedef struct _IMAGE_SECTION_HEADER { BYTE Name[IMAGE_SIZEOF_SHORT_NAME]; union { DWORD PhysicalAdd ...
- iOS 3D Touch功能 3 -备
新的触摸体验——iOS9的3D Touch 一.引言 二.在模拟器上学习和测试3D Touch 附.SBShortcutMenuSimulator的安装和使用 三.3D Touch的主要应用 四.3D ...
- js 比较日期大小
//1获取当前时间 var curTime = new Date(); //2把字符串格式转换为日期类 var startTime = new Date(Date.parse(kc.begintime ...
- P0口上拉电阻选择
如果是驱动led,那么用1K左右的就行了.如果希望亮度大一些,电阻可减小,最小不要小于200欧姆,否则电流太大:如果希望亮度小一些,电阻可增大,增加到多少呢,主要看亮度情况,以亮度合适为准,一般来说超 ...