首页 > 代码库 > Codeforces Round #251 (Div. 2) C. Devu and Partitioning of the Array

Codeforces Round #251 (Div. 2) C. Devu and Partitioning of the Array

注意p的边界情况,p为0,或者 p为k

奇数+偶数 = 奇数

奇数+奇数 = 偶数

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>

using namespace std;

int main(){
    int n,k,p;
    long a;
    cin >> n >> k >> p;
    vector<long> even, odd;
    vector<set<long> > parts(k);
    for(int i = 0 ; i < n ; ++ i){
        cin >> a;
        a%2 ? odd.push_back(a): even.push_back(a);
    }
    int even_cnt = p, odd_cnt = k - p;
    if(odd.size() < odd_cnt  || (odd.size() -odd_cnt)%2 ) cout<<"NO"<<endl;
    else{
        for(int i = p; i < k ; ++ i)  parts[i].insert(odd[i-p]);
        if(even.size()+(odd.size()-odd_cnt)/2 < p) cout<<"NO"<<endl;
        else{
            if(even.size() >= p){
                for(int i = 0 ; i < p; ++ i) parts[i].insert(even[i]);
                for(int i = p; i < even.size(); ++ i) parts[p-1 >= 0 ? p-1 : p].insert(even[i]);
                for(int i = k-p; i < odd.size() ; ++ i ) parts[p-1 >= 0 ? p-1 : p].insert(odd[i]);

            }else{
                for(int i = 0 ; i < even.size(); ++ i) parts[i].insert(even[i]);
                int j = k-p;
                for(int i = even.size(); i < p-1; ++ i) parts[i].insert(odd[j++]),parts[i].insert(odd[j++]);
                while( j < odd.size()) parts[p-1 >= 0 ? p-1 : p].insert(odd[j++]);
            }
            cout<<"YES"<<endl;
            for(int i = 0 ; i < k; ++ i){
                set<long> tmp = parts[i];
                cout<<tmp.size();
                for(set<long>::iterator iter = tmp.begin(); iter!=tmp.end(); ++ iter) cout<<" "<<*iter;
                cout<<endl;
            }
        }
    }
}