入学体检

来源:互联网 发布:淘宝上大拿韩代真品吗 编辑:程序博客网 时间:2024/04/20 03:11
       入学体检 

Num : 3 Time Limit : 1000ms Memory Limit : 65536K

description
马上又要迎接2015级的小鲜肉们了,校医院又要忙一阵子了……
对于入学体检大家想必不会陌生吧,体检的时候要检查很多项目,而且最令人头疼的是每体检一个项目都要排老长时间的队== 现在我们知道每个人总共要体检n个项目,当李华刚来到校医院时每个项目要排的时间为ai秒,如果他选择了其中一个项目先排队,那么对于其他的项目每秒增加额外排队时间为bi,李华想知道他应该怎样选择体检顺序才能使得总体的排队时间最短(mod=365×24×60×60)

input
输入第一行为要输入的组数T,每组第一行为n(1<=n<=100000),接下来n行,每行两个数ai,bi 0<=ai,bi<2^31

output
输出Case #t: 最短的排队时间%mod,t为当前组数

sample_input
1
5
1 2
2 3
3 4
4 5
5 6

sample_output
Case #1: 1419

hint
He spends 1 second in the first queue, 5 seconds in the 2th queue, 27 seconds in the 3th queue, 169 seconds in the 4th queue, and 1217 seconds in the 5th queue. So the total time is 1419s

贪心的一道题目。我们这样分析,如果只有两个队伍,我们若先排第一个队伍,那么总用时:x1+(x1*y2+x2);若先排第二个队伍那么总用时:x2+(x2*y1+x1)。如果第一种优于第二种的话那么x1+x1*y2+x2< x2+x2*y1+x1,我们把他化简一下x1/y1

#include <stdio.h>    #include <string.h>    #include <algorithm>    #include <iostream>    using namespace std;    const int maxn=100005;    typedef long long LL;    const int mod=365*24*60*60;    int n;    struct note    {        LL x,y;        bool operator < (const note &other)const        {            return x*other.y<other.x*y;        }    }a[maxn];    int main()    {      //  freopen("data.in","r",stdin);      //  freopen("data.out","w",stdout);        int T,tt=0;        scanf("%d",&T);        while(T--)        {            scanf("%d",&n);            for(int i=0;i<n;i++)            {                scanf("%I64d%I64d",&a[i].x,&a[i].y);            }            sort(a,a+n);            LL maxx=0;            for(int i=0;i<n;i++)            {                maxx=(maxx+a[i].x+maxx*a[i].y)%mod;            }            printf("Case #%d: %I64d\n",++tt,maxx);        }        return 0;    }    
0 0
原创粉丝点击