LA 3303 / POJ 2675 Songs (数学证明+排序)

题目链接:http://poj.org/problem?id=2675


题目大意:给定n首歌,求出一种放歌顺序使得下图中的式子值最小,s(i)代表每首歌的标号,f(s(i))表示编号为s(i)的歌播放频率,l(s(i))表示编号为s(i)的歌的播放时长,询问第s首歌的编号。



思路:假设某两首歌A和B,要确定A和B的播放前后关系,假设A要在B之前播放,则有,A.len * A.fre + (A.len + B.len) * B.fre < B.len * B.fre + (A.len + B.len) * a.fre ,化简得A.len * B.fre < B.len * A.fre ,则可以按这种规则对歌曲排序获得最优值。


Code:


/*   W          w           w        mm          mm             222222222       7777777777777    */
/*    W        w w         w        m  m        m  m          222        22              7777    */
/*    w        w w         w        m  m        m  m                     22              777     */
/*     w      w   w       w        m    m      m    m                    22              77      */
/*     w      w    w      w        m    m      m    m                 222                77      */
/*      w    w      w    w        m      m    m      m              222                  77      */
/*      w    w      w    w        m      m    m      m            222                    77      */
/*       w  w        w  w        m        m  m        m         222                      77      */
/*       w  w        w  w        m        m  m        m      222                         77      */
/*        ww          ww        m          mm          m     222222222222222             77      */

//#pragma comment(linker, "/STACK:102400000,102400000")
//C++
//int size = 256 << 20; // 256MB
//char *p = (char*)malloc(size) + size;
//__asm__("movl %0, %%esp\n" :: "r"(p));
//G++
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<ctime>
#include<deque>
#include<cmath>
#include<vector>
#include<string>
#include<cctype>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#define REP(i,s,t) for(int i=(s);i<=(t);i++)
#define REP2(i,t,s) for(int i=(t);i>=s;i--)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned long ul;

const int N=150000;
int n;
struct song
{
  int id;
  double len,fre;
  bool operator < (const song& A) const
  {
    return len*A.fre<A.len*fre;
  }
}s[N];
int main()
{
  #ifdef ONLINE_JUDGE
  #else
    freopen("test.in","r",stdin);
  #endif
  while(~scanf("%d",&n))
  {
    REP(i,1,n)
    {
      scanf("%d%lf%lf",&s[i].id,&s[i].len,&s[i].fre);
    }
    int Q;
    scanf("%d",&Q);
    sort(s+1,s+1+n);
    printf("%d\n",s[Q].id);
  }
  return 0;
}



                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值