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

OpenSoucre發表於2014-06-09

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

 

相關文章