zoukankan      html  css  js  c++  java
  • 问题 L: Robots

    问题 L: Robots

    时间限制: 1 Sec  内存限制: 128 MB
    提交: 164  解决: 65
    [提交][状态][讨论版][命题人:admin]

    题目描述

    You’re the leading designer of a complex robot, that will explore human unreachable locations. Your job is to design a robot that will go as far as possible. To do this, you have n available energy sources. The ith source is capable of accelerating the robot by a rate of ai (m/s2) and can do this for a total of si seconds. The robot is initially at rest (its initial velocity is zero). 
    You have to decide the order in which to use the sources in order to maximize the total distance traveled by the robot. You will use one source until si seconds have elapsed, then immediately switch to another unused source (the switch is instantaneous). Each source can be used only once.
    Given the accelerations and durations of each source, write an efficient program to determine the optimal order of the sources, in order to maximize the total distance traveled. Your program must compute the difference between the traveled distance in the optimal case and in the default case (the order given by the input data).
    Physics background: if the velocity is v before you start using a source whose acceleration is a then, after t seconds, the robot has traveled a total vt+1/2at2 meters, and the final velocity will be v' = v+at.

    输入

    The input file starts with the number n (1 ≤ n ≤ 104) of sources. Starting from a different line follows the n space-separated acceleration and duration for each source (positive integer numbers).

    输出

    The output file contains the computed difference between the traveled distance in the optimal case and in the default case (the order given by the input data), with one decimal.

    样例输入

    2
    2 1
    30 2
    

    样例输出

    56.0
    ************************************************************************************************************************************
    贪心 水题 只是作为一个记录
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
     
    using namespace std;
     
    struct x
    {
        int a;
        int t;
    };
    int cmp(x p,x q)
    {
        return p.a>q.a;
    }
    int main()
    {
        int n;
        x a[10005];
        x b[10005];
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d %d",&a[i].a,&a[i].t);
            b[i].a = a[i].a;
            b[i].t = a[i].t;
        }
        sort(a,a+n,cmp);
     
        double ans = 0;
        double x1 = 0;
        double x2 = 0;
        double v1 = 0;
        double v2 = 0;
        for(int i=0;i<n;i++)
        {
            x1 = v1*a[i].t+(1.0/2)*(a[i].a)*(a[i].t)*(a[i].t);
            v1 = v1+a[i].a * a[i].t;
            x2 = v2*b[i].t+(1.0/2)*(b[i].a)*(b[i].t)*(b[i].t);
            v2 = v2+b[i].a * b[i].t;
            ans+= x1-x2;  //因为看到很多人有wa 怕超限 所以选择每次都见 而不是最后减
        }
        printf("%.1f",ans);
    }


  • 相关阅读:
    简单三层代码生成器原理剖析
    深入浅出三层架构
    SQL Server数据的批量导入
    BULK INSERT如何将大量数据高效地导入SQL Server
    用扩展方法优化多条件查询
    SQL Server 数据库访问通用类(更新)
    SQL中自增量字段Int型 identity(1,1)与Guid作为主键的性能比较
    MD5加盐实现登录
    Pi币
    SQL Server 分割字符串转列
  • 原文地址:https://www.cnblogs.com/hao-tian/p/8955152.html
Copyright © 2011-2022 走看看