CodeForces 1474C: Pensamiento de destrucción de matrices + dicotomía

Portal

Descripción del Título

Darle un conjunto de números, primero determine una x, luego encuentre dos números ai y aj de este conjunto de números, súmelos ax, elimínelos y luego reemplace x con el valor mayor de los dos números y continúe con la siguiente ronda. Pregunte si puede eliminar todo este conjunto de números

análisis

En primer lugar, en la primera ronda, debemos elegir el más grande, luego enumeramos qué elegir de otro número y luego pasamos a la siguiente ronda.

Código

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <cstring>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
const int INF = 0x3f3f3f3f;
const int N = 1e6 + 10;
int a[N];
bool st[N];
PII ans[N];
int n;
bool cmp(int x,int y){
    
    
    return x > y;
}

int main(){
    
    
    int t;
    scanf("%d",&t);
    while(t--){
    
    
        map<int,int> M;
        scanf("%d",&n);
        for(int i = 1;i <= n * 2;i++) {
    
    
            scanf("%d",&a[i]);
        }
        sort(a + 1,a + 1 + n * 2);
        int res = 0;
        int ppp;
        for(int i = 1;i < 2 * n;i++){
    
    
            for(int j = 1;j <= 2 * n;j++) st[j] = false;
            res = 0;
            int maxv = a[n * 2];
            ans[++res] = {
    
    a[i],a[2 * n]};
            st[2 * n] = true;
            st[i] = true;
            ppp = a[i] + a[2 * n];
            for(int j = 2 * n - 1;j;j--){
    
    
                if(st[j]) continue;
                int p = lower_bound(a + 1,a + 1 + 2 * n,maxv - a[j]) - a;
                while(st[p] && p < 2 * n) p++;
                // cout << maxv << ' ' << a[j] << ' ' << p << ' '<< a[p] << endl;
                if(a[p] == maxv - a[j] && !st[p] && j != p){
    
    
                    st[j] = true;
                    st[p] = true;
                    ans[++res] = {
    
    max(a[j],maxv - a[j]),min(a[j],maxv - a[j])};
                    maxv = max(a[j],maxv - a[j]);
                }
            }
            // cout << res << endl;
            if(res == n) break;
        }
        if(res == n) {
    
    
            puts("YES");
            printf("%d\n",ppp);
            for(int i = 1;i <= n;i++) printf("%d %d\n",ans[i].first,ans[i].second);
        }
        else puts("NO");
    }
    return 0;
}

/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃        ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/


Supongo que te gusta

Origin blog.csdn.net/tlyzxc/article/details/112876950
Recomendado
Clasificación