RocksonLee's Blog
avatar
RocksonLee
2022-03-21 13:44:08

Problem

Luogu

Codeforces

Solution

反转一个序列使得相邻两个数最大

那么你可以直接找两个数,一个第一大,一个第二大即可

然后相加就是答案

Code

#include <bits/stdc++.h>
using namespace std;

#define il inline
#define INF 0x3f3f3f3f
#define cl(a, b) memset(a, b, sizeof(a))
typedef long long ll;
typedef unsigned long long ull;

int t, n, temp, max1, max2;
int main()
{
    scanf("%d", &t);
    for (int kkz = 0; kkz < t; kkz++)
    {
        max1 = max2 = 0;
        scanf("%d", &n);
        for (int i = 0; i < n; i++)
        {
            scanf("%d", &temp);
            if (temp > max1)
                max2 = max1, max1 = temp;
            else if (temp > max2)
                max2 = temp;
        }
        printf("%d\n", max1 + max2);
    }
    return 0;
}
CF 1654A Maximum Cake Tastiness
comment评论
Search
search