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;
}

猜你喜欢

转载自blog.csdn.net/zhouzi2018/article/details/112909966