zoukankan      html  css  js  c++  java
  • hoj1941超市收银问题

    分析:

       贪心算法

       当n为偶数时,例如:1 2 3 4 5 6,可知,分开的两支队伍为

       1 3 5与2 4 6时等待时间最小。

       当n为奇数时,例如1 2 3 4 5时,可知,分开的两支队伍为

       1 3 4与2 4时总等待时间最小,若n为奇数时,所以每次总把最小的数放在最长的队列中时,

       总等待时间最小

     

    #include <iostream>

    #include <cstdio>

    #include <algorithm>

    using namespace std;

    #define X 1005

    int d[X],n,ans;

    int main()

    {

       freopen("sum.in","r",stdin);

       freopen("sum.out","w",stdout);

       while(cin>>n)

       {

          for(int i=0;i<n;i++)

             scanf("%d",d+i);

          sort(d,d+n);    //排序

          ans = 0;

          int q = (n+1)/2;

          for(int i=0;i<n;i+=2) //计算最长的那支队列

          {

             ans += (q-1)*d[i];

             q--;

          }

          q = n-(n+1)/2;

          for(int j=1;j<n;j+=2)

          {

             ans += (q-1)*d[j];

             q--;

          }

          printf("%.3lf\n",ans*1.0/n);

       }

     

       return 0;

    }

  • 相关阅读:
    PostMessage
    __stdcall
    C++中L和_T()之区别
    号外:百度开源了它的人工智能技术
    最棒的Unity Github 项目收集(2016)
    OpenGL学习笔记——求值器和NURBS
    unity3d四元数和旋转矩阵
    C# System.Timers.Time
    进程地址空间分布
    子进程与父进程
  • 原文地址:https://www.cnblogs.com/yejinru/p/2403965.html
Copyright © 2011-2022 走看看