POJ 3063 Sherlock Holmes 随机化

----------------

int n,m;
int w[maxn],b[maxn];
int s1[maxn],s2[maxn];
int w1,b1,w2,b2;
double ans,mo;
bool flag;
void work(int x,int y) {
    w1=w1-w[s1[x]]+w[s2[y]];
    w2=w2-w[s2[y]]+w[s1[x]];
    b1=b1-b[s1[x]]+b[s2[y]];
    b2=b2-b[s2[y]]+b[s1[x]];
    swap(s1[x],s2[y]);
}
void cal() {
    if (w1>b1&&w2>b2) {
        double tmp=min(w1/mo,w2/mo);
        if (tmp>ans) {
            flag=true;
            ans=tmp;
        }
    }
    if (w1<b1&&w2<b2) {
        double tmp=min(b1/mo,b2/mo);
        if (tmp>ans) flag=0,ans=tmp;
    }
}
int main(){
    srand(time(0));
    while (~scanf("%d",&n)) {
        ans=0;
        w1=b1=w2=b2=0;
        if (n&1){
            printf("No solution\n");
            continue;
        }
        scanf("%d",&m);
        int mid=n/2;
        for (int i=1;i<=n;i++) scanf("%d%d",&w[i],&b[i]);
        for (int i=1;i<=mid;i++) {
            s1[i]=i;
            w1+=w[i];
            b1+=b[i];
            s2[i]=i+mid;
            w2+=w[s2[i]];
            b2+=b[s2[i]];
        }
        mo=(double)mid*(double)m;
        for (int i=0;i<10000;i++){
            int r1=rand()%mid+1;
            int r2=rand()%mid+1;
            work(r1,r2);
            cal();
        }
        if (ans<=0.5) printf("No solution\n");
        else {
            if (flag) printf("W %0.2f\n",ans*100);
            else printf("B %0.2f\n",ans*100);
        }
    }
	return 0;
}


----------------

你可能感兴趣的:(POJ 3063 Sherlock Holmes 随机化)