POJ1015搜索做法,TLE但是思路没有错

Jury Compromise
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 20163 Accepted: 5086 Special Judge

Description

In Frobnia, a far-away country, the verdicts in court trials are determined by a jury consisting of members of the general public. Every time a trial is set to begin, a jury has to be selected, which is done as follows. First, several people are drawn randomly from the public. For each person in this pool, defence and prosecution assign a grade from 0 to 20 indicating their preference for this person. 0 means total dislike, 20 on the other hand means that this person is considered ideally suited for the jury.
Based on the grades of the two parties, the judge selects the jury. In order to ensure a fair trial, the tendencies of the jury to favour either defence or prosecution should be as balanced as possible. The jury therefore has to be chosen in a way that is satisfactory to both parties.
We will now make this more precise: given a pool of n potential jurors and two values di (the defence's value) and pi (the prosecution's value) for each potential juror i, you are to select a jury of m persons. If J is a subset of {1,..., n} with m elements, then D(J ) = sum(dk) k belong to J
and P(J) = sum(pk) k belong to J are the total values of this jury for defence and prosecution.
For an optimal jury J , the value |D(J) - P(J)| must be minimal. If there are several jurys with minimal |D(J) - P(J)|, one which maximizes D(J) + P(J) should be selected since the jury should be as ideal as possible for both parties.
You are to write a program that implements this jury selection process and chooses an optimal jury given a set of candidates.

Input

The input file contains several jury selection rounds. Each round starts with a line containing two integers n and m. n is the number of candidates and m the number of jury members.
These values will satisfy 1<=n<=200, 1<=m<=20 and of course m<=n. The following n lines contain the two integers pi and di for i = 1,...,n. A blank line separates each round from the next.
The file ends with a round that has n = m = 0.

Output

For each round output a line containing the number of the jury selection round ('Jury #1', 'Jury #2', etc.).
On the next line print the values D(J ) and P (J ) of your jury as shown below and on another line print the numbers of the m chosen candidates in ascending order. Output a blank before each individual candidate number.
Output an empty line after each test case.

Sample Input

4 2 
1 2 
2 3 
4 1 
6 2 
0 0 

Sample Output

Jury #1 
Best jury has value 6 for prosecution and value 4 for defence: 
 2 3 

Hint

If your solution is based on an inefficient algorithm, it may not execute in the allotted time.
 
即对于n个人,每个人有dj值和pj值,从中选择m个人组成个集合,这个集合的DJ值即为集合中所有人的dj值的和,而PJ值即为集合中所有人PJ值的和。那么选取m个人,使得集合的|DJ-PJ|最小,而|DJ+PJ|值最大。
输出DJ值,PJ值以及集合中每个人的序号。
最近写搜索上了瘾,一上手就写了个搜索。这次暴力搜索不起作用了,对于n<30的情况还是非常管用的,但是对于n>30非常卡了,起码几秒才能出结果....虽然是对的....但是就不能AC了。
重要是没有剪枝的条件....估计搜索走不通了。
在此记一笔,算作一种思路吧。
#include<stdio.h>
#include<stdio.h>
#define NNUM 201
#define MNUM 21
//n为总人数,m为选择的人数 
int n,m,i;
int dj[NNUM],pj[NNUM];
int select[MNUM],record[MNUM];
int vis[NNUM];
int recorddj,recordpj,mydj,mypj;
int selectIndex=0;
void search(int currentIndex)
{
     if(currentIndex>n)
     {
           return; 
     } 
     if(selectIndex==m)
     {
            //判断是否比前一次探寻的好
            if((recorddj==0&&recordpj==0)||abs(mydj-mypj)<abs(recorddj-recordpj)||(abs(abs(mydj-mypj)-abs(recorddj-recordpj))<1e-6&&mypj+mydj>recordpj+recorddj))
            {
                    recorddj=mydj;
                    recordpj=mypj;
                    for(i=1;i<=m;i++)
                    {
                           record[i]=select[i]; 
                    } 
            }
          
            return; 
     }
     int k;
     for(k=currentIndex;k<=n;k++)
     {
        if(vis[k]==0)
        {
            vis[k]=1;
            mydj+=dj[k];
            mypj+=pj[k];
            select[++selectIndex]=k;
            search(k);
            selectIndex--;
            mydj-=dj[k];
            mypj-=pj[k]; 
            vis[k]=0;
        }
     }
}
int main()
{
   int count=0;
   while(scanf("%d%d",&n,&m)!=EOF)
   {
       if(n==0&&m==0)
       {
           break; 
       } 
       count++;
       
       for(i=1;i<=n;i++)
       {
           scanf("%d%d",&pj[i],&dj[i]); 
           vis[i]=0;
       }
       int mytag=0;
       for(i=2;i<=n;i++)
       {
           if(pj[i-1]!=pj[i]||dj[i-1]!=dj[i])
           {
               mytag=1;
               break; 
           } 
       } 
       if(!mytag)
       {
            //压缩
            n=m; 
       }
       
       selectIndex=0;
       recorddj=0;
       recordpj=0;
       mydj=0;
       mypj=0;
       search(1);
       
       printf("Jury #%d\n",count);
       printf("Best jury has value %d for prosecution and value %d for defence:\n",recordpj,recorddj);
       for(i=1;i<=m;i++)
       {
               printf(" %d",record[i]); 
       }
       printf("\n\n");
   }
   
   system("pause");
   return 0; 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值