zoukankan      html  css  js  c++  java
  • 湖南大学ACM程序设计新生杯大赛(同步赛)G

    题目描述

    BSD is a lazy boy. He doesn't want to wash his socks, but he will have a data structure called 'socks heap'.By maintaining this structure, he can quickly pick out two of the most smelless socks from millions of socks everyday. As one of his good friends, you know the data structure of BSD, and want to predict 'the air pollution level' of a certain day. Of course, it's BSD's socks that pollute the air.

    We will enter numbers that indices for 'socks smell level' and expect you to output the total amount of pollution that he will wear on the day of BSD design.

    输入描述:

    First line contain an integer T 1<T<=10 ,means the sum of test cases.
    for every test cases,we will firstly give N(1<N<1000000) in a single line,means the count of socks.Next line Contains N numbers (you can use int to save all of them) ,means 'smell level' for every socks.

    输出描述:

    please putout answer in a single line for every test cases.
    示例1

    输入

    3
    5
    1 1 2 3 5
    3
    1 4 7
    3
    200 4 10000

    输出

    2
    5
    204

    题解

    找到最小值、次小值输出即可。

    #include<cstdio>
    using namespace std;
     
    const double eps=1e-8;
    #define zero(x)(((x)>0?(x):(-x))<eps)
     
    const int maxn = 1000000 + 10;
    long long a[maxn];
    int n;
    long long INF = 0x7FFFFFFF;
     
    int main() {
      int T;
      scanf("%d", &T);
      while(T --) {
        scanf("%d", &n);
        for(int i = 1; i <= n; i ++) {
          scanf("%lld", &a[i]);
        }
        long long ans = 0;
        long long num;
        int pos1 = -1, pos2 = -1;
     
        for(int i = 1; i <= n; i ++) {
          if(pos1 == -1) pos1 = i;
          if(a[i] < a[pos1]) pos1 = i;
        }
        for(int i = 1; i <= n; i ++) {
          if(pos1 == i) continue;
          if(pos2 == -1) pos2 = i;
          if(a[i] < a[pos2]) pos2 = i;
        }
        ans = a[pos1] + a[pos2];
        printf("%lld
    ", ans);
      }
      return 0;
    }
    

      

  • 相关阅读:
    java单例类
    java构造方法-this关键字的用法
    java封装
    Bean的装配方式
    scope的范围
    Bean实例化(三种方法)
    依赖注入
    建立Spring项目的基础
    Androids中数据库的使用SQLite
    SharedPreferences
  • 原文地址:https://www.cnblogs.com/zufezzt/p/8099005.html
Copyright © 2011-2022 走看看