zoj3464 Rugby Football(贪心)

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 is Vi. 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
2
3 4 20
2 3 4
1 1 10
2
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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值