zoukankan      html  css  js  c++  java
  • SWUST OJ Gold Nuggets Distribution(0490)

    Gold Nuggets Distribution(0490)

    Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 421 Accepted: 234
     
    Description
    Bosses have a bag of gold. Each month there will be two employees to their outstanding performance were a gold award. In line with the rituals, ranking first employees will receive the heaviest nugget, ranked second employee will be the lightest gold. Under this method, unless the Nuggets to a new bag, otherwise employees received the first gold always be employees than the second-Nuggets. If there is a new cyclical Nuggets joined bag, every month to find the lightest and most of the Nuggets. Assuming that there is a comparison of weight machines, we hope that at least by comparison to identify the number and the lightest weight of gold.
    (题目描述就不翻译了,简而言之的意思就是,找出数组中的最大元素和最小元素,有个要求就是,比较的次数最少)
     
    Input
    two lines 
    the first line is a number of gold nuggets and less than 50000 
    the second line are weight of every gold nugget.
     
    Output
    two numbers 
    the first is weight of the heaviest nugget 
    the second is weight of the lightest gold
     
    Sample Input
    6
    5 2 7 6 3 4
     
    Sample Output
    7 2
     
     Hint
    Source
    MrYang
     
     1 #include<iostream>
     2 using namespace std;
     3 
     4 int a[2000000];
     5 
     6 int main()
     7 {
     8     int n,i,j,max=0,min=99999;
     9     cin>>n;
    10     for(i=0;i<n;i++)
    11         cin>>a[i];
    12     for(i=0,j=n-1;i<=j;i++,j--)
    13     {
    14         if(a[i]<a[j])
    15         {
    16             if(a[i]<min)
    17                 min=a[i];
    18             if(a[j]>max)
    19                 max=a[j];
    20         }
    21         else
    22         {
    23             if(a[j]<min)
    24                 min=a[j];
    25             if(a[i]>max)
    26                 max=a[i];
    27         }
    28     }
    29     cout<<max<<' '<<min<<endl;
    30     return 0;
    31 }

    注:两个数比较,则那个大的数,不可能成为这组数中最小的数,同理,那个小的数,不可能成为这组数中最大的数。从常用的 2*n 次比较,变成了(n/2)*3=1.5*n 次比较。

  • 相关阅读:
    屏蔽/捕获并输出错误
    物理机转Hyper-V虚拟机
    Windows Server 2012无法安装 .NET3.5-安装角色或功能失败,找不到源文件
    IDRAC 固件升级操:
    网卡启动安装dell服务器OS
    服务器指定网卡进行备份数据避免影响业务口
    【转载】用户通过WEB方式更改AD域帐户密码
    Windows运维之Windows8.1-KB2999226-x64安装提示 此更新不适用你的计算机
    Exchange 退信550 5.1.11 RESOLVER.ADR.ExRecipNotFound
    优酷kux视频转MP4
  • 原文地址:https://www.cnblogs.com/haveyoueverbeen/p/4488793.html
Copyright © 2011-2022 走看看