• 2017 Hackatari Codeathon H-card


    H. Card
    time limit per test
    0.5 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    After Hasan ruined the Worm puzzle, Rula brought a new game to the office, the Card game.

    The game has a group of cards, each card has an integer value Vi written on it.

    Initially, N cards are placed side by side in a straight line (refer to the picture above). In each turn you will pick two adjacent card sets (a single card is also considered a card set) and merge the cards of the two sets together to form one new set.

    After each turn, you will add the maximum card value in each card set to your score. The game is over when no more merges could be performed.

    What is the maximum score you can get at the end of the game, given that your initial score is 0?

    Input

    The first line of input contains a single integer N (2 ≤ N ≤ 105), the number of cards in the game.

    The second line contains N space-separated integers, each integer is Vi (1 ≤ Vi ≤ 5000), which represents the number written on the ithcard.

    Output

    On a single line, print the maximum score you can get.

    Examples
    input
    5
    1 2 3 2 1
    
    output
    23
    
    input
    6
    7 9 7 5 4 3
    
    output
    108
    
    Note

    One of the possible solutions for the first sample is as follows:

    • Initially, each set will contain a single card: {1} {2} {3} {2} {1}.
    • Merge {1} with {2}, the resulting sets are: {1 2} {3} {2} {1}, which will add (2 + 3 + 2 + 1 = 8) to your score.
    • Merge {2} with {1}, the resulting sets are: {1 2} {3} {2 1}, which will add (2 + 3 + 2 = 7) to your score.
    • Merge {1 2} with {3}, the resulting sets are: {1 2 3} {2 1}, which will add (3 + 2 = 5) to your score.
    • Merge {1 2 3} with {2 1}, the resulting sets are: {1 2 3 2 1}, which will add 3 to your score.

    Final score is 8 + 7 + 5 + 3 = 23.

    思路:看下Note,从高到低排序后,越小的数加的次数递减

    还有就是总和可能会超出int范围,需要long long

    #include <bits/stdc++.h>
    typedef long long ll;
    using namespace std;
    
    const int maxn = 1e6+5;
    
    int a[maxn];
    
    bool cmp(int a,int b)
    {
        return a > b;
    }
    int main()
    {
        memset(a,0,sizeof(a));
        int n;
        cin>>n;
    for(int i = 0; i < n; i++)
            cin>>a[i];
    
    sort(a,a+n,cmp);
    ll sum = 0;
    for(int i = 0; i < n; i++) {
        int m = n-(i+1);
        if(m==0)
            break;
        sum += m*a[i];
    }
    
    cout<<sum<<endl;
    }



  • 相关阅读:
    淘宝破裤子奇遇记--记酷锐哲型旗舰店
    第9章 在实践中使用模板:9.5 后记
    第9章 在实践中使用模板:9.4 破译大篇幅错误信息
    第9章 在实践中使用模板:9.3 预编译头文件
    第9章 在实践中使用模板:9.2 模板和内联
    第9章 在实践中使用模板:9.1 包含模型
    第8章 编译期编程:8.5 编译期if
    第8章 编译期编程:8.4 SFINAE(替换失败并不是错误)
    第8章 编译期编程:8.3 偏特化的执行路径选择
    第8章 编译期编程:8.2 使用constexpr计算
  • 原文地址:https://www.cnblogs.com/zhangmingzhao/p/7256648.html
Copyright © 2020-2023  润新知