Lizard Era: Beginning
题面翻译
有 n ( n ⩽ 25 ) n(n \leqslant 25) n(n⩽25)个任务和三个人,每次任务给出每个人能得到的值,每次任务选两个人,使 n n n个任务结束后三个人得到的值是一样的,且尽量大。输出每次要派哪两个人,如果不行输出 I m p o s s i b l e Impossible Impossible。
题目描述
In the game Lizard Era: Beginning the protagonist will travel with three companions: Lynn, Meliana and Worrigan. Overall the game has $ n $ mandatory quests. To perform each of them, you need to take exactly two companions.
The attitude of each of the companions to the hero is an integer. Initially, the attitude of each of them to the hero of neutral and equal to 0. As the hero completes quests, he makes actions that change the attitude of the companions, whom he took to perform this task, in positive or negative direction.
Tell us what companions the hero needs to choose to make their attitude equal after completing all the quests. If this can be done in several ways, choose the one in which the value of resulting attitude is greatest possible.
输入格式
The first line contains positive integer $ n $ ( $ 1<=n<=25 $ ) — the number of important tasks.
Next $ n $ lines contain the descriptions of the tasks — the $ i $ -th line contains three integers $ l_{i},m_{i},w_{i} $ — the values by which the attitude of Lynn, Meliana and Worrigan respectively will change towards the hero if the hero takes them on the $ i $ -th task. All the numbers in the input are integers and do not exceed $ 10^{7} $ in absolute value.
输出格式
If there is no solution, print in the first line “Impossible”.
Otherwise, print $ n $ lines, two characters is each line — in the $ i $ -th line print the first letters of the companions’ names that hero should take to complete the $ i $ -th task (‘L’ for Lynn, ‘M’ for Meliana, ‘W’ for Worrigan). Print the letters in any order, if there are multiple solutions, print any of them.
样例 #1
样例输入 #1
3
1 0 0
0 1 0
0 0 1
样例输出 #1
LM
MW
MW
样例 #2
样例输入 #2
7
0 8 9
5 9 -2
6 -8 -7
9 4 5
-4 -9 9
-4 5 2
-6 8 -7
样例输出 #2
LM
MW
LM
LW
MW
LM
LW
样例 #3
样例输入 #3
2
1 0 0
1 1 0
样例输出 #3
Impossible
思路
由题意可知,总共的n个任务,可以分为两部分进行处理,左面的划分为l1,l2,l3,右面的划分为r1,r2,r3
由题意可得,l1+r1=l2+r2,l1+r1=l3+r3可以推出l1-l2=r2-r1,l1-l3=r3-r1我们的目的就是在等式成立的基础上使得l1+r1的和最大
我们将等式两边相等的数设为a1=b1,a2=b2
完整代码
#include<bits/stdc++.h>
using namespace std;
int n;
int a[40],b[40],c[40];//a,b,c三者不同任务可得到的价值
int opp[40],now[40],maxn=-1e9;//opp储存最后答案的序号,now是当前储存的答案序号,maxn用于储存最大的l1+r1方便后期比较
char ans[][3]={"xx","LM","MW","LW"};//答案序号所对应的操作
struct node{
int xu[40],l1=0;//xu表示的是当前状态下所对应的选择的序号,l1则表示在当前的状态下所求得的l1
};
map<pair<int,int>,node> mp;//pair储存的是a1,a2或者b1,b2的值,node存储在前面pair的状态下所得到的答案操作序号以及l1的值
void read()
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i]>>b[i]>>c[i];
}//读入
void copy(int* a,int* b,int bg,int ed)
{
for(int i=bg;i<=ed;i++)a[i]=b[i];
}//用于将now中的临时答案序号储存到map中或者储存到最后答案数组中
void dfs1(int i,int l1,int l2,int l3)//i表示目前在处理第几个任务,l1,l2,l3储存着在前一半中已经做好抉择后的a,b,c的值
{
if(i==n/2+1)//如果前一半已经搜完
{
int a1=l2-l1,a2=l3-l1;
pair<int,int> t=make_pair(a1,a2);//将a1,a2储存下来
if(mp.count(t))//如果能找到a1,a2这个状态
if(mp[t].l1>l1)//并且之前这个状态所储存的l1大于现在刚找到的l1,
return;//就跳过储存这个状态
node tmp;
tmp.l1=l1;
copy(tmp.xu,now,1,n/2);
mp[t]=tmp;//将新找到的状态储存下来
return;
}
now[i]=1;
dfs1(i+1,l1+a[i],l2+b[i],l3);
now[i]=2;
dfs1(i+1,l1,l2+b[i],l3+c[i]);
now[i]=3;
dfs1(i+1,l1+a[i],l2,l3+c[i]);//当做出不同的决策时,储存下来不同的答案序号,并更新a,b,c的值
}
void dfs2(int i,int r1,int r2,int r3)
{
if(i==n+1)//如果已经搜完了
{
int b1=r1-r2,b2=r1-r3;
pair<int,int> t=make_pair(b1,b2);//将b1,b2储存下来
if(mp.count(t)==0)
return;//在前一半中找不到所对应的状态,也就是不满足相等的条件,就跳过
if(mp[t].l1+r1>maxn)//如果刚找到的l1,r1相加最大
{
copy(opp,mp[t].xu,1,n/2);
copy(opp,now,n/2+1,n);//更新最终答案序号
maxn=mp[t].l1+r1;//更新l1+r1的最大值
}
return;
}
now[i]=1;
dfs2(i+1,r1+a[i],r2+b[i],r3);
now[i]=2;
dfs2(i+1,r1,r2+b[i],r3+c[i]);
now[i]=3;
dfs2(i+1,r1+a[i],r2,r3+c[i]);//当做出不同的决策时,储存下来不同的答案序号,并更新a,b,c的值
}
int main()
{
read();//读入
dfs1(1,0,0,0);//先储存好前二分之一的任务数
dfs2(n/2+1,0,0,0);//计算好后二分之一的答案并与前二分之一的答案进行比较
if(maxn==-1e9)
{
cout<<"Impossible";
return 0;
}//如果没有找到答案的输出
for(int i=1;i<=n;i++)
cout<<ans[opp[i]]<<endl;//输出选择
return 0;
}