• 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 次比较。

  • 相关阅读:
    Go语言实现:【剑指offer】剪绳子
    delphi10.3安装使用mySQL
    uniGUI学习之把窗口分成左,右边(上下)三部分,并且在运行中可以动态调节其相对大小(36)
    uniGUI学习之UniStringGrid(35)
    uniGUI之主窗口折叠UI之UniTreeMenu(32-2)
    好网站
    ios图片
    ios启动图的相关问题
    自学php
    Parse error: syntax error, unexpected $end in diguoclassfunctions.php on line 1246
  • 原文地址:https://www.cnblogs.com/haveyoueverbeen/p/4488793.html
Copyright © 2020-2023  润新知