题意:给你n个数,和m,让我们从1-m选数字,使得n个数奇数和偶数个数相同切各不相同
思路:模拟
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 5 int n,m; 6 set<ll > s; 7 map<ll ,int > ma; 8 ll b[200005]; 9 ll a[200005]; 10 int main(){ 11 cin>>n>>m; 12 ll x; 13 for(int i=1;i<=n;i++){ 14 scanf("%lld",&x); 15 a[i]=x; 16 s.insert(x); 17 } 18 int s1=0,s2=0; 19 for(set<ll >::iterator it=s.begin();it!=s.end();it++){ 20 ma[*it]++; 21 if(*it&1) s1++; 22 else s2++; 23 } 24 s1=n/2-s1; 25 s2=n/2-s2; 26 if(s1<0) s1=0; 27 if(s2<0) s2=0; 28 int sum=0; 29 int l=0; 30 for(int i=1;i<=m;i+=2){ 31 if(s1==0) break; 32 if(!ma[i]){ 33 s1--; 34 b[++l]=i; 35 sum++; 36 } 37 38 } 39 for(int i=2;i<=m;i+=2){ 40 if(s2==0) break; 41 if(!ma[i]){ 42 b[++l]=i; 43 s2--;sum++; 44 } 45 46 } 47 48 if(s1||s2) cout<<-1<<endl; 49 else { 50 int r=1; 51 int sum1=0,sum2=0; 52 cout<<sum<<endl; 53 for(int i=1;i<=n;i++){ 54 int t=0; 55 if(ma[a[i]]){ 56 if(a[i]&1){ 57 if(sum1<n/2) { 58 printf("%d ",a[i]); 59 sum1++;ma[a[i]]=0;t=1; 60 } 61 } 62 else { 63 if(sum2<n/2){ 64 printf("%d ",a[i]); 65 sum2++;ma[a[i]]=0;t=1; 66 } 67 } 68 } 69 if(!t) printf("%d ",b[r++]); 70 } 71 printf(" "); 72 } 73 }