3900: Exchanging antlersDescriptionthere are n elk in the zoo. Each elk has two antlers, each with a weight. However, once one of the ElkThe difference in weight between the two antlers is too large for the elk to lose its balance and fall. In order for this tragedy to happen, the zoo chief decidedthe two horns of any elk are not more than C. However, it is very troublesome to exchange two antlers, notonly because the antlers need more people to carry, and will cause pain to the elk. Therefore, you need to calculate the minimum number of exchanges so thatthe two horns of an elk are not heavier than C. Note that the two antlers can only be exchanged between the elk. Because it doesn't make sense to exchange two horns of the same elk. InputThe first behavior of the integer n,c. The next n lines, two integers per line, represent the two-point weight of each elk at the beginning. Outputa number, which is the minimum number of exchanges. If in any case not to make each moose balance, output-1. Sample Input3 0
3 3
2 5
2 5Sample Output1HINT
For 100% of the data, n <=, C <= 1000000, the weight of each angle not exceeding 1000000.
No one on the Internet to write a puzzle, or write it yourself ...
How to say, N<=16 is a breakthrough, we definitely want to go to the shape of the pressure to think.
At first I was directly enumerated in the state of which two were exchanged, but finally found a counter example, such as:
3 1
1 3
2 4
3 5
A deer can be exchanged several times.
Then change the idea, found that the problem and some of the topcoder juice a bit like, first preprocess the I state let each deer exchange the minimum number of exchanges, if feasible, then the answer is the number of deer minus 1 (think about), then it is very simple.
#include <stdio.h>#include<iostream>#include<stdlib.h>#include<algorithm>using namespacestd;intn,m,i,j,k,p,c[ +],a[ -],b[ -],f[(1<< -)+5];intMain () {scanf ("%d%d",&n,&m); for(i=1; i<=n;i++) scanf ("%d%d",&a[i],&B[i]); for(i=1;i< (1<<N); i++) {k=0;p =0; for(j=0;(1<<J) <=i;j++) if(1<<j&i) {if(ABS (a[j+1]-b[j+1]) >m) p=1; K++; C[K]=a[j+1]; K++; C[K]=b[j+1]; } if(p==0) f[i]=0;Else{sort (c+1, c+k+1); P=0; for(j=1; j<=k;j+=2) if(c[j+1]-c[j]>m) {p=1; Break;} if(p==1) F[i]=1e9;Elsef[i]=k/2-1; } } for(i=1;i< (1<<N); i++) for(J= (i1) &i;j>0; J= (J-1) &i) f[i]=min (f[i],f[j]+f[i^J]); if(F[(1<<n)-1]==1E9) cout<<"-1";Elsecout<<f[(1<<n)-1]; return 0;}
Bzoj 3900: Exchanging the Antlers