分类:贪心
题目链接:LA3303-Songs
对于某两首歌曲song[i]+song[i+1],差别只有l[i]*f[i+1]或l[i+1]*f[i]按这个排序即可
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
struct node
{
ll id,le;
double fr;
bool operator < (const node &rhs) const
{
return fr*rhs.le<rhs.fr*le;
}
}a[maxn];
int n,t;
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
{
scanf("%lld %lld %lf",&a[i].id,&a[i].le,&a[i].fr);
}
sort(a+1,a+1+n);
scanf("%d",&t);
printf("%d\n",a[t].id);
}
return 0;
}
本文介绍了一种使用贪心算法解决特定歌曲排序问题的方法。通过对歌曲的特性进行比较,设计了一种排序策略,使得相邻歌曲之间的差异最大化。代码实现使用了C++,并详细展示了如何定义和比较歌曲节点,以及如何进行排序。


被折叠的 条评论
为什么被折叠?



