Hoj 2143 Songs
2013-01-28 16:29
92 查看
题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=2143
若有两相邻的song,准备先听的那个时间为L1,听的频率为F1,后听的那个分别为L2和F2
设他们前面的所有歌的长度和为S。
他们两个产生的代价为 S * F1 + ( S + L1 ) * F2 = S * ( F1 + F2 ) + L1 * F2
若交换,那么新代价为 S * F2 + ( S + L2 ) * F1 = S * ( F1 + F2 ) + L2 * F1
那么显然,L1 * F2 <= L2 * F1 ( 因为我们假设这个排列已经最优 )
两边都除以 F1 * F2 ,那么有 L1 / F1 <= L2 / F2
对任意的相邻的song都有这个性质,那么对整个排列方案而言,那么整个排列都是按 Li / Fi 升序排列的。
若有两相邻的song,准备先听的那个时间为L1,听的频率为F1,后听的那个分别为L2和F2
设他们前面的所有歌的长度和为S。
他们两个产生的代价为 S * F1 + ( S + L1 ) * F2 = S * ( F1 + F2 ) + L1 * F2
若交换,那么新代价为 S * F2 + ( S + L2 ) * F1 = S * ( F1 + F2 ) + L2 * F1
那么显然,L1 * F2 <= L2 * F1 ( 因为我们假设这个排列已经最优 )
两边都除以 F1 * F2 ,那么有 L1 / F1 <= L2 / F2
对任意的相邻的song都有这个性质,那么对整个排列方案而言,那么整个排列都是按 Li / Fi 升序排列的。
#include <iostream> #include <math.h> #include <stdio.h> #include <string.h> #include <queue> #include <algorithm> using namespace std; struct Song { int num; double len; double f; }; Song so[100000]; bool cmp(Song a,Song b) { return a.len * b.f <b.len * a.f; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif int n; int r; while(scanf("%d",&n)!=EOF) { for(int i=0;i<n;i++) { scanf(" %d %lf %lf",&so[i].num,&so[i].len,&so[i].f); } scanf(" %d",&r); sort(so,so+n,cmp); printf("%d\n",so[r-1].num); } return 0; }
相关文章推荐
- HOJ 2143 - Songs(贪心)
- 100 best english songs
- HOJ1102 计算几何 判断两个线段是否会相交
- HOJ 1056
- Darts --HOJ 11960
- [DFS/动态规划DP] HDU/HOJ 3779 Railroad 暴力解 Zipper加强版
- hoj2434 going to know him
- hoj1991 Happy 2005
- poj 1611 The Suspects // hoj 1564 The Suspects 并查集
- [动态规划+数学结论]HOJ_12301_Eleven Lover
- hoj Set S
- hoj 2578 Super_Stack 模拟栈
- Radar Installation 贪心 hoj
- HOJ 1447 Compromise (DP)
- hoj 1015 NearlyPrimerNumber 的思考
- HOJ 1811 Freckles (Prim 最小生成树)
- 树状数组题目总结三( 上)( HOJ 2275 Number sequence )
- HOJ 2782 Alfredo's Pizza Restaurant
- HOJ 2105 Bungee Jumping
- HOJ 1951 Beavergnaw