题目不难,暴力地dp一下就好,但是不知道我WA在哪里了,对拍了好多的数据都没找出错误= =。估计又是哪里小细节写错了QAQ。。思路是用dp[i][j]表示已经选了i个,差值为j的最大和。转移的话暴力枚举当前选那个即可。代码如下(WA的,以后有机会再找找错在哪里吧0.0):
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <set>
using namespace std;
const int N = + ; int n,m;
int a[N],b[N];
struct node
{
int now,pre,val;
int a,b;
}dp[+][+]; set<int> get(int x)
{
set<int> S;
for(int i=m;i>=;i--)
{
S.insert(dp[i][x].now);
x = dp[i][x].pre;
}
return S;
} int main()
{
int kase = ;
while(scanf("%d%d",&n,&m) == )
{
if(n == & m == ) break;
for(int i=;i<=n;i++) scanf("%d%d",a+i,b+i);
for(int i=;i<=;i++) dp[][i] = (node){-,-,,,};
dp[][] = (node){,-,,,};
for(int i=;i<=m;i++)
{
for(int j=;j<=;j++)
{
dp[i][j] = (node){-,-,,,};
for(int k=;k<=n;k++)
{
int add = a[k] - b[k];
if(j-add < || j - add > ) continue;
int pre = j - add;
if(dp[i-][pre].now == -) continue;
int flag = ;
for(int p=i-;p>=;p--)
{
if(dp[p][pre].now == k)
{
flag = ;
break;
}
else pre = dp[p][pre].pre;
}
if(flag == ) continue;
if(dp[i-][j - add].val + a[k]+b[k] > dp[i][j].val)
{
dp[i][j] = (node){k,j-add,dp[i-][j - add].val + a[k]+b[k],
dp[i-][j - add].a + a[k], dp[i-][j - add].b + b[k]};
}
}
}
}
set<int> S;
int aa, bb;
for(int add=;add<=;add++)
{
if(dp[m][-add].now == - && dp[m][+add].now == -) continue;
int temp = ;
if(dp[m][-add].now != -)
{
temp = dp[m][-add].val;
S = get(-add);
aa = dp[m][-add].a, bb = dp[m][-add].b;
}
if(dp[m][+add].now != -)
{
if(dp[m][+add].val > temp)
{
S = get(+add);
aa = dp[m][+add].a, bb = dp[m][+add].b;
}
}
break;
}
printf("Jury #%d\n",kase++);
printf("Best jury has value %d for prosecution and value %d for defence:\n",aa,bb);
for(set<int>::iterator it=S.begin();it!=S.end();it++) printf(" %d",*it);
puts("\n");
}
return ;
}