• 2014-2015 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)


    I. Sale in GameStore(贪心)
    time limit per test
    2 seconds
    memory limit per test
    512 megabytes
    input
    standard input
    output
    standard output

    A well-known Berland online games store has announced a great sale! Buy any game today, and you can download more games for free! The only constraint is that the total price of the games downloaded for free can't exceed the price of the bought game.

    When Polycarp found out about the sale, he remembered that his friends promised him to cover any single purchase in GameStore. They presented their promise as a gift for Polycarp's birthday.

    There are n games in GameStore, the price of the i-th game is pi. What is the maximum number of games Polycarp can get today, if his friends agree to cover the expenses for any single purchase in GameStore?

    Input

    The first line of the input contains a single integer number n (1 ≤ n ≤ 2000) — the number of games in GameStore. The second line contains n integer numbers p1, p2, ..., pn (1 ≤ pi ≤ 105), where pi is the price of the i-th game.

    Output

    Print the maximum number of games Polycarp can get today.

    Sample test(s)
    input
    5
    5 3 1 5 6
    output
    3
    input
    2
    7 7
    output
    2
    Note

    In the first example Polycarp can buy any game of price 5 or 6 and download games of prices 1 and 3 for free. So he can get at most 3 games.

    In the second example Polycarp can buy any game and download the other one for free.

    代码能力还是太弱了(这题是贪心),排序后找

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 using namespace std;
     5 
     6 const int max_size = 2010;
     7 
     8 void quick_sort(int arry[], int l, int r)
     9 {
    10     if(l < r)
    11     {
    12         int i = l, j = r, k = arry[l];
    13         while(i < j)
    14         {
    15             while(i < j && arry[j] >= k)
    16                 j--;
    17             if(i < j)
    18                 arry[i++] = arry[j];
    19             while(i < j && arry[i] < k)
    20                 i++;
    21             if(i < j)
    22                 arry[j--] = arry[i];
    23         }
    24 
    25         arry[i] = k;
    26         quick_sort(arry, l, i-1);
    27         quick_sort(arry, i+1, r);
    28     }
    29 }
    30 
    31 int main()
    32 {
    33     int n;
    34     int arry[max_size];
    35     /// (1 ≤ n ≤ 2000)
    36     /// (1 ≤ pi ≤ 105)
    37     while(scanf("%d", &n) != EOF)
    38     {
    39         memset(arry, 0, sizeof(arry));
    40         for(int i = 0; i < n; ++i)
    41         {
    42             scanf("%d", &arry[i]);
    43         }
    44 
    45         quick_sort(arry, 0, n-1);
    46 
    47         int max_price = arry[n-1];
    48         int total = 0;
    49         int num = 0;
    50         int j = 0;
    51          while(j < n-1)
    52          {
    53              total += arry[j++];
    54              if(total <= max_price)
    55                 num++;
    56          }
    57         printf("%d
    ", num+1);
    58     }
    59     return 0;
    60 }
    View Code

    F题感觉学长代码写得太风骚了,看不太懂,再加油

  • 相关阅读:
    面向对象的程序设计-2-创建对象
    面向对象的程序设计-1-理解对象
    react组件的生命周期
    react-router 组件式配置与对象式配置小区别
    mobx @computed的解读
    十分钟介绍mobx与react
    less学习
    git-简单流程(学习笔记)
    几种视频编码器的编译及使用方法
    一位程序员工作10年总结的13个忠告
  • 原文地址:https://www.cnblogs.com/ya-cpp/p/4050425.html
Copyright © 2020-2023  润新知