1. 程式人生 > >【Codeforces 342A】Xenia and Divisors

【Codeforces 342A】Xenia and Divisors

using div size sync .com problems [1] 題解 efi

【鏈接】 我是鏈接,點我呀:)
【題意】

【題解】


最後a,b,c只有以下3種情況
1,2,4
1,2,6
1,3,6
那麽用cnt[8]統計每個數字出現的次數.
輸出cnt[4]次1,2,4
(如果1或2不夠,那麽無解
緊接著
如果6的個數和1的個數不同,那麽無解
如果2的次數+3的次數和6出現的次數不同,那麽無解.
否則
輸出cnt[2]個1,2,6
cnt[3]個1,3,6就ok了。
看看有沒有輸出夠n/3組
不夠的話就無解(說明有其他無用的數字出現

【代碼】

#include <bits/stdc++.h>
#define rep1(i,a,b) for (int i = a;i <= b;i++)
using namespace std;

const int N = 1e5;

int n,m;
int cnt[10];
vector<pair<int,pair<int,int> > > v;

int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin >> n;
    for (int i = 1;i <= n;i++) {
        int x;
        cin >> x;
        cnt[x]++;
    }
    rep1(i,1,cnt[4]) {
        v.push_back({1,{2,4}});
        if (cnt[1]==0) return cout<<"-1"<<endl,0;
        if (cnt[2]==0) return cout<<"-1"<<endl,0;
        cnt[1]--;cnt[2]--;
        n-=3;
    }
    cnt[4] = 0;
    if (cnt[1]!=cnt[6] || (cnt[2]+cnt[3])!=(cnt[6])){
        cout<<"-1"<<endl;
        return 0;
    }
    for (int i = 1;i <= cnt[2];i++){
        v.push_back({1,{2,6}});
        n-=3;
    }
    for (int i = 1;i <= cnt[3];i++){
        v.push_back({1,{3,6}});
        n-=3;
    }
    if (n!=0) return cout<<-1<<endl,0;
    for (auto temp:v){
        cout<<temp.first<<' '<<temp.second.first<<' '<<temp.second.second<<endl;
    }
    return 0;
}

【Codeforces 342A】Xenia and Divisors