codeforces1474C Array Destruction (multiset + 枚举)

传送门
题解:题目 n n n 只有 1000 1000 1000 ,这个要看清哦
首先分析一波,必然第一个是最大的和某一个数,然后接下来的答案必须是此时没被选上的最大的,因为不这样的话,那么肯定凉了啊,然后就考虑枚举第一次和最大的肩并肩地那个数,如果每次处理复杂度为 O ( n l o g n ) O(nlogn) O(nlogn) 那么大概就是个 1 e 7 1e7 1e7 的计算量就能过了,其实考虑用个 m u l t i s e t multiset multiset 维护即可
发现这场代码量和模拟量都挺大的,思想倒是不麻烦,各种边界啥的,繁琐~~~~

#include <bits/stdc++.h>
#define rush() int T; cin >> T; while (T--)
using namespace std;
int n;
bool solve(int r, int l, multiset<int> s)
{
    vector<pair<int, int>> ans;
    auto it1 = --s.end(), it2 = s.find(r);
    s.erase(it1); s.erase(it2);
    ans.emplace_back(l, r);
    int cnt = 1;
    while (cnt < n / 2) {
        it1 = --s.end(), it2 = s.find(l - *it1);
        if (it2 == s.end() || it1 == it2) {
            return false;
        } else {
            r = l - *it1; l = *it1;
            ans.emplace_back(l, r);
            s.erase(it1); s.erase(it2);
        }
        cnt++;
    }
    cout << "YES" << endl;
    cout << ans[0].first + ans[0].second << endl;
    for (auto x : ans) {
        cout << x.first << " " << x.second << endl;
    }
    return true;
}
int main()
{
    rush(){
        cin >> n;
        n *= 2;
        vector<int> v(n);
        multiset<int> s;
        for (auto &x : v) {
            cin >> x;
            s.insert(x);
        }
        sort(v.begin(), v.end());
        bool flag = false;
        for (int i = n - 2; i >= 0; i--) {
            if (solve(v[i], v[n - 1], s) == true) {
                flag = true;
                break;
            }
        }
        if (false == flag) {
            cout << "NO" << endl;
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值