zoj3464 Rugby Football(贪心)

来源:互联网 发布:php面向对象编程 编辑:程序博客网 时间:2024/03/28 22:07
Rugby Football

Time Limit: 2 Seconds      Memory Limit: 65536 KB

CM is a member of Rugby football club of ZJU. He loves to play the game. Every Friday afternoon there is a club training of skills. CM wants to make it more effective.

In the training, N club members including CM stand at staring line in a row. The maximum velocity of theith player isVi. The distance between the line and touchdown zone isL. The goal is to send the ball to touchdown zone. They can pass the ball to others but forward passing is illegal. If someone reaches touchdown zone with ball, the team scores and it will be an effective training.

This picture illustrate the rule of passing ball.

picture

But the way to scoring is not easy because of crazy opponents. Any player with the ball cannot rush more thanT seconds or he will be tackled. And he cannot be passed again because he will be very tired after sprinting; even have not forT seconds enough. At the beginning CM can choose who takes the ball first. Now CM wants to know whether they can score and how fast they can.

Input

The first line is an integer RP. Then RP cases follow. There are no more than 20 cases.

For each case, there are two lines. The first line contains three integers N, T, L (1 ≤ N,T ≤ 10000, 1 ≤ L ≤ 109). The second line has N integers indicating V1, V2 ... Vn. (1 ≤ Vi ≤ 10000)

Output

A single line with a float number S and correct to two decimal places. It means the total seconds they need to score. If they cannot score, output -1.

Simple Input

23 4 202 3 41 1 102

Simple Output

5.33-1题目意思很简单,思路就是贪心:将序列按从大到小的将序排序,然后从依次寻找看那一次的和(a[i]*t的和)大于l,如果n个序列的总和(a[i]*t的和)都不行,则输出-1,否则输出最少所需要的时间。 小小的心得体会:一开始的时候我输入的时候就用一个sum=sigma(a[i]*t)最后就溢出了,wa了一发,后面发现了后就改了。注意,以后像这种序列求和大于某一个值M的时候,可以将M累减(正常的思路是累加),这也算是一个比较好的习惯吧。。。也不能这么说,关键还是因人而异吧,还是得加强代码能力啊!
#include <cstdio>#include <cstring>#include <algorithm>#include <cstdlib>using namespace std;bool cmp(const int& a,const int& b){    return a>b;}int a[10000+5];int main(){    //freopen("in.txt","r",stdin);   // freopen("out3.txt","w",stdout);    int n,t,l,T,i;    scanf("%d",&T);    while(T--){        scanf("%d%d%d",&n,&t,&l);        for(i=0;i<n;i++){            scanf("%d",&a[i]);        }        sort(a,a+n,cmp);        double time=0;        for(i=0;i<n;i++){            if(l<=a[i]*t){                time+=(double)l*1.0/a[i];                break;            }else{                l-=a[i]*t;                time+=t;            }        }        if(i==n)    printf("-1\n");        else    printf("%.2f\n",time);    }    return 0;}


/*    @author : liuwen*/#include <iostream>#include <cstdio>#include <cstring>#include <climits> //INT_MAX INT_MIN LONG_LONG_MAX LONG_LONG_MIN#include <cstdlib>#include <queue>#include <stack>#include <map>#include <vector>#include <cmath>#include <algorithm>using namespace std;const int maxn=10000+5;int a[maxn];bool cmp(const int& a,const int& b){    return a>b;}int main(){    //freopen("in.txt","r",stdin);    //freopen("out1.txt","w",stdout);    int T;    scanf("%d",&T);    while(T--){        int n,t,l,i;        scanf("%d%d%d",&n,&t,&l);        memset(a,0,sizeof(a));        for(int i=0;i<n;i++){            scanf("%d",&a[i]);        }        sort(a,a+n,cmp);        int sum=0;        double time=0;        for(i=0;i<n;i++){            if(sum+t*a[i]>l){                time+=(l-sum*1.0)/a[i];                break;            }else{                sum+=t*a[i];                time+=t;            }        }        if(i==n)    printf("-1\n");        else    printf("%.2f\n",time);    }    return 0;}

0 0