POJ 3063 Sherlock Holmes

Sherlock Holmes
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 662 Accepted: 213

Description

Sherlock Holmes, the well known detective, must solve a puzzling situation. He has n boxes B1, B2, .., Bn , where n is even, each containing m balls. The balls are white and black. Let Bi = (Wi, Bi) denote a box with Wi white balls and Bi black balls. He must partition the boxes into two sets, each consisting of n/2 boxes, such that either the white balls or the black balls hold amajority in both sets. If there is such a majority, let m1 and m2 denote the percentage of the majority balls in each set. Holmes must find very quickly the maximum possible value of min(m1, m2). Can you help Holmes? 

Input

There are several testcases. Each data set in the file stands for a particular set of boxes. A data set starts with the number n (n < 10000) of boxes. Follows the number m (m < 10000) of balls, and for each box the number (< 10000) of white and black balls in this specific order. White spaces can occur freely in the input. The input data are correct and terminate with an end of file.

Output

For each set of data the program prints the result to the standard output from the beginning of a line. The program prints the color (W or B) of the balls that hold the majority followed by the maximum value - if there is such a majority, or "No solution" (without quotes) if no majority can be obtained. An input/output sample is in the table below. There is a single data set that contains 4 boxes each containing 30 balls. The first box, for example, contains 17 white balls and 13 black balls. There is only one possible partition of the boxes (B1, B4), (B2, B3), the white balls holding majority. The result for the data set is the identifier W and the maximum value.

Sample Input

4
30
17 13
12 18
20 10
14 16

Sample Output

W 51.67

Source

Southeastern Europe 2006




一开始给的中文题目 居然没有说平均分成两组。。。
只能rand()%2分组+卡时 Wa了3个点

后来平均分后 随机交换两组中的两个+卡次数

写得挺丑的。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<ctime>
using namespace std;
struct self{int x,y,side;}s[10001];
int m,n,a,b,c;
double ans,z;
int side=-1;
int baizong,heizong,num;
int l[2],r[2];
int x,y;
void tongji()
{
    if(l[0]>r[0]&&l[1]>r[1])
    {
        double k=min((double)l[0]/(m/2*n),(double)l[1]/(m/2*n));
        if(k>ans)
        {
            ans=k;
            side=0;
        }
    }
    if(l[0]<r[0]&&l[1]<r[1])
    {
        double k=min((double)r[0]/(m/2*n),(double)r[1]/(m/2*n));
        if(k>ans)
        {
            ans=k;
            side=1;
        }
    }
}

void exchange(int x,int y)
{
    l[0]-=s[x].x;
    r[0]-=s[x].y;
    l[1]+=s[x].x;
    r[1]+=s[x].y;
    s[x].side=1;
    
    l[1]-=s[y].x;
    r[1]-=s[y].y;
    l[0]+=s[y].x;
    r[0]+=s[y].y;
    s[y].side=0;   
}

int main()
{
    
    srand(time(NULL));
    while(scanf("%d",&m)==1)
    {
        scanf("%d",&n);
        num=0;
        ans=0;
        baizong=0;
        heizong=0;
        side=-1;
        l[0]=r[0]=l[1]=r[1]=0;
        for(a=1;a<=m;a++)
        {
            scanf("%d%d",&s[a].x,&s[a].y);
            baizong+=s[a].x;
            heizong+=s[a].y;
        }
        if(baizong>heizong)side=0;
        else side=1;
    
        for(a=1;a<=m/2;a++)
        {
            s[a].side=0;
            l[0]+=s[a].x;
            r[0]+=s[a].y;
        }
        for(a=m/2+1;a<=m;a++)
        {
            s[a].side=1;
            l[1]+=s[a].x;
            r[1]+=s[a].y;
        }  
        while(num<=50000)
        {
            num++;
            x=rand()%m+1;
            while(s[x].side!=0)x=rand()%m+1;
            y=rand()%m+1;
            while(s[y].side!=1)y=rand()%m+1;
            z=ans;
            exchange(x,y);
            tongji();
            if(ans>z)if(rand()%1000+1<=50)exchange(y,x);else;
            else if(rand()%1000+1>=50)exchange(y,x);
        }
        if(ans==0)
        cout<<"No solution"<<'\n';
        else
        if(side==0)
        cout<<"W ";
        else cout<<"B ";
        if(ans!=0)cout<<fixed<<setprecision(2)<<ans*100<<'\n';
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值