您的位置:首页 > 其它

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 升序排列的。

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: