B. The Third Side【800 / 思维】

45 阅读1分钟

image.png codeforces.com/problemset/… 5.21分钟做完,比较好像

#include<bits/stdc++.h>
using namespace std;
const int N=1e5*5+10;
typedef long long int LL;
LL t,n,a[N];
int main(void)
{
    cin>>t;
    while(t--)
    {
        cin>>n;
        priority_queue<int,vector<int>,greater<int>>q;
        for(int i=1;i<=n;i++) cin>>a[i],q.push(a[i]);
        while(q.size()>=2)
        {
            LL a=q.top(); q.pop();
            LL b=q.top(); q.pop();
            q.push(a+b-1);
        }
        cout<<q.top()<<'\n';
    }
    return 0;
}

直接模拟做法比较好想到,但其实有规律。

image.png 每次合并总和-1,一共合并(n-1)次,故就是sum-(n-1)=sum-n+1