Lizard Era: Beginning

Lizard Era: Beginning

题面翻译

n ( n ⩽ 25 ) n(n \leqslant 25) n(n25)个任务和三个人,每次任务给出每个人能得到的值,每次任务选两个人,使 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

分析

三个人,折半搜索,所以存在
l 1 + r 1 = l 2 + r 2 = l 3 + r 3 l_1+r_1=l_2+r_2=l_3+r_3 l1+r1=l2+r2=l3+r3
l 1 + r 1 = l 2 + r 2 → r 1 − r 2 = l 2 − l 1 → a 1 = b 1 , a 1 = r 1 − r 2 , b 1 = l 2 − l 1 l_1+r_1=l_2+r_2 \to r_1-r_2=l_2-l_1 \to a_1=b_1,a_1=r_1-r_2,b_1=l_2-l_1 l1+r1=l2+r2r1r2=l2l1a1=b1,a1=r1r2,b1=l2l1
l 1 + r 1 = l 3 + r 3 → r 1 − r 3 = l 3 − l 1 → a 2 = b 2 , a 2 = r 1 − r 3 , b 2 = l 3 − l 1 l_1+r_1=l_3+r_3 \to r_1-r_3=l_3-l_1 \to a_2=b_2,a_2=r_1-r_3,b_2=l_3-l_1 l1+r1=l3+r3r1r3=l3l1a2=b2,a2=r1r3,b2=l3l1
a n s = max ⁡ { l 1 + r 1 } ans=\max\{l_1+r_1\} ans=max{l1+r1}

代码

#include <bits/stdc++.h>
using namespace std;
const int M = 30;
int n;
int a[M], b[M], c[M];
int opp[M];
int now[M];
char ANS[][3] = { "xx","LM","MW","LW" };
struct node {
	int xu[M]; int l1 = 0;
};
map<pair<int, int>, node> mp;
int maxn = -1e9;
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];
}
void dfs1(int i, int l1, int l2, int l3) {
	if (i == n / 2 + 1) {
		int a1 = l2 - l1, a2 = l3 - l1;
		pair<int, int> t = make_pair(a1, a2);
		if (mp.count(t))
			if (mp[t].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]);
}
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);
		if (mp.count(t) == 0) return;
		if (mp[t].l1 + r1 > maxn) {
			copy(opp, mp[t].xu, 1, n / 2);
			copy(opp, now, n / 2 + 1, n);
			maxn = mp[t].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]);
}
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;
}

分析

void copy(int* a, int* b, int bg, int ed) {
	for (int i = bg; i <= ed; i++) a[i] = b[i];
}

更新opp与maxn时的函数,用来覆盖opp数组
由于折半搜索基本相同,这里只说dfs2

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);
		if (mp.count(t) == 0) return;
		if (mp[t].l1 + r1 > maxn) {
			copy(opp, mp[t].xu, 1, n / 2);
			copy(opp, now, n / 2 + 1, n);
			maxn = mp[t].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]);
}

参数以及b1,b2的意义已经说过,笔者在此不过多赘述
生成一个pair,方便在dfs1所搜索到的值中查询,若查询到比答案优,则覆盖答案即可

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值