码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1015

时间:2015-06-09 15:36:14      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:

#include<iostream>
#include<algorithm>
#define MAXN 201
#define count C_ount
using namespace std;

int _m[20][801];
int count[20][801];
int a[20];

struct _node
{
    int first;
    int second;
};

_node _cand[MAXN];
int n;
int main()
{
    //freopen("acm.acm","r",stdin);
    int m;
    int i;
    int j;
    int time;
    int max;
    int u;
    int v;
    int u_v;
    int _i;
    int _j;
    int _test_time = 0;
    int index;
    int ans;
    while(cin>>n>>m,n||m)
    {
        memset(count,-1,sizeof(count));
        memset(_m,-1,sizeof(_m));
        max = m*20;
        for(i = 0; i < n; ++ i)
        {
            cin>>u>>v;
            _cand[i].first = u - v;
            _cand[i].second = u + v;
        }
        for(i = 0; i < n; ++ i)
        {
            if(count[0][max+_cand[i].first] < _cand[i].second)
            {
                _m[0][max+_cand[i].first] = i;
                count[0][max+_cand[i].first] = _cand[i].second;
            }
        }
        for(time = 0; time < m-1; ++ time)
        {
            for(u_v = 0; u_v <= max*2; ++ u_v)
            {
                if(_m[time][u_v] != -1)
                {
                    for(i = 0; i < n; ++ i)
                    {
                        if(count[time+1][u_v + _cand[i].first] < count[time][u_v] + _cand[i].second)
                        {
                            _j = u_v;
                            for(_i = time; _i >= 0; -- _i)
                            {
                                if(_m[_i][_j] == i)
                                    break;
                                _j -= _cand[_m[_i][_j]].first;
                            }
                            if(_i < 0)
                            {
                                _m[time+1][u_v+_cand[i].first] = i;
                                count[time+1][u_v+_cand[i].first] = count[time][u_v] + _cand[i].second;
                            }
                        }
                    }
                }
            }
        }

        /////////////
        
        for(i = 0; i <= max; ++ i)
        {
            int tem_1 = count[m-1][max+i];
            int tem_2 = count[m-1][max-i];
            if( (tem_1 = count[m-1][max+i]) >= 0 || (tem_2 = count[m-1][max-i]) >= 0)
            {
                if(tem_1 > tem_2)
                {
                    ans = max+i;
                }
                else
                {
                    ans = max-i;
                }
                break;
            }
        }
        cout<<"Jury #"<<++ _test_time<<endl;
        cout<<"Best jury has value "<<(count[m-1][ans] + (ans - max))/2<<" for prosecution and value "<<(count[m-1][ans] - (ans - max))/2<<" for defence:"<<endl;
        j = ans;
        index = 0;
        for(i = m-1; i >= 0; -- i)
        {
            a[index ++] = _m[i][j];
            j -= _cand[_m[i][j]].first;
        }
        sort(a,a+index);
        for(i = 0; i < index; ++ i)
        {
            cout<<" "<<a[i]+1;
        }
        cout<<endl;
        cout<<endl;
    }
}


POJ 1015

标签:

原文地址:http://www.cnblogs.com/gavinsp/p/4563208.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!