D. Lizard Era: Beginning
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

In the game Lizard Era: Beginning the protagonist will travel with three companions: Lynn, Meliana and Worrigan. Overall the game has nmandatory 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.

Input

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 li, mi, wi — 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 107 in absolute value.

Output

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.

Examples
input
3
1 0 0
0 1 0
0 0 1
output
LM
MW
MW
input
7
0 8 9
5 9 -2
6 -8 -7
9 4 5
-4 -9 9
-4 5 2
-6 8 -7
output
LM
MW
LM
LW
MW
LM
LW
input
2
1 0 0
1 1 0
output
Impossible

大致题意:一开始3个人的数值为0,有n次操作,每次选两个人将对应的数值增加,最后要求3个人的数值相等.输出方案,如果有多种方案,输出所有数值最大的一种.

分析:n非常小,但是直接搜的话3^25还是会炸.观察到如果指数减小一半,就刚好在能接受的复杂度里了.很容易想到meet in the middle.

应用meet in the middle的方程模型,先列出方程:a + d = b + e = c + f,也就是a - b = e - d 且 a - c = f - d.那么从开头搜,搜到中点就将a-b和a-c的结果放到map里,接着从末尾搜,如果找到了e-d和f-d的值,就更新答案.

两次dfs用三进制数记录答案,注意:第二次dfs记录的答案要倒着输出!

#include <map>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int inf = 0x7fffffff; int n,A[],B[],C[],maxn,ans = -inf,printx,printy,num[],cntt,cnt;
struct node
{
int zhuangtai,x;
}e[]; struct node2
{
int x,y;
bool operator < (const node2 &a) const
{
return a.x == x ? a.y < y : a.x < x;
}
}; map <node2,int> a;
void dfs1(int dep,int x,int y,int z,int sta)
{
if (dep == maxn + )
{
y -= x;
z -= x;
node2 temp;
temp.x = y;
temp.y = z;
if (a.find(temp) == a.end())
{
a[temp] = ++cnt;
e[cnt].zhuangtai = sta;
e[cnt].x = x;
}
else
{
if (e[a[temp]].x < x)
{
e[a[temp]].x = x;
e[a[temp]].zhuangtai = sta;
}
}
return;
}
for (int i = ; i < ; i++)
{
if (i == )
dfs1(dep + ,x + A[dep],y + B[dep],z,sta * );
if (i == )
dfs1(dep + ,x + A[dep],y,z + C[dep],sta * + );
if (i == )
dfs1(dep + ,x,y + B[dep],z + C[dep],sta * + );
}
} void dfs2(int dep,int x,int y,int z,int sta)
{
if (dep == maxn)
{
y -= x;
z -= x;
y = -y;
z = -z;
node2 temp;
temp.x = y;
temp.y = z;
if (a.find(temp) != a.end())
{
if (e[a[temp]].x + x > ans)
{
ans = e[a[temp]].x + x;
printx = e[a[temp]].zhuangtai;
printy = sta;
}
}
return;
}
for (int i = ; i < ; i++)
{
if (i == )
dfs2(dep - ,x + A[dep],y + B[dep],z,sta * );
if (i == )
dfs2(dep - ,x + A[dep],y,z + C[dep],sta * + );
if (i == )
dfs2(dep - ,x,y + B[dep],z + C[dep],sta * + );
}
} void print()
{
for (int i = ; i <= maxn; i++)
{
num[++cntt] = printx % ;
printx /= ;
}
for (int i = cntt; i >= ; i--)
{
if (num[i] == )
puts("LM");
if (num[i] == )
puts("LW");
if (num[i] == )
puts("MW");
}
cntt = ;
for (int i = maxn + ; i <= n; i++)
{
num[++cntt] = printy % ;
printy /= ;
}
for (int i = ; i <= cntt; i++)
{
if (num[i] == )
puts("LM");
if (num[i] == )
puts("LW");
if (num[i] == )
puts("MW");
}
} int main()
{
scanf("%d",&n);
for (int i = ; i <= n; i++)
scanf("%d%d%d",&A[i],&B[i],&C[i]);
if (n % == )
maxn = n / ;
else
maxn = n / + ;
dfs1(,,,,);
dfs2(n,,,,);
if (ans != -inf)
print();
else
puts("Impossible"); return ;
}

Codeforces 585.D Lizard Era: Beginning的更多相关文章

  1. Codeforces Round #325 (Div. 2) F. Lizard Era: Beginning meet in the mid

    F. Lizard Era: Beginning Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

  2. Codeforces 585D Lizard Era: Beginning

    Lizard Era: Beginning 折半之后搜就完事了, 直接存string字符串卡空间, 随便卡卡空间吧. #include<bits/stdc++.h> #define LL ...

  3. (中等) CF 585D Lizard Era: Beginning,中途相遇。

    In the game Lizard Era: Beginning the protagonist will travel with three companions: Lynn, Meliana a ...

  4. Codeforces Round #325 (Div. 1) D. Lizard Era: Beginning

    折半搜索,先搜索一半的数字,记录第一个人的值,第二个人.第三个人和第一个人的差值,开个map哈希存一下,然后另一半搜完直接根据差值查找前一半的答案. 代码 #include<cstdio> ...

  5. Codeforces 585D. Lizard Era: Beginning(meet in the middle)

    一眼题...这个数据范围也太明显了吧... suma1==suma2 && sumb1==sumb2 && sumc1==sumc2 相当于suma1-sumb1==s ...

  6. [codeforces] 585D Lizard Era: Beginning || 双向dfs

    原题 有n(n<=2)个任务和三个人,每次任务给出每个人能得到的值,每次任务选两个人,使n个任务结束后三个人得到的值是一样的.输出每次要派哪两个人,如果不行输出Impossible. n< ...

  7. Codeforces 585D Lizard Era: Beginning | 折半搜索

    参考这个博客 #include<cstdio> #include<algorithm> #include<cstring> #include<map> ...

  8. CF585D Lizard Era: Beginning

    嘟嘟嘟 题面我是不会咕的(没有真香):有\(n(n \leqslant 25)\)个任务和三个人,每次任务给出每个人能得到的值,每次任务选两个人,使\(n\)个任务结束后三个人得到的值是一样的,且尽量 ...

  9. Codeforces Round 585

    Codeforces Round 585 浅论如何发现自己是傻子的-- 反正今天是完全蒙的,水了签到题就跑了-- A. Yellow Cards 签到题. 众所周知,CF的签到题一般是一道神神奇奇的数 ...

随机推荐

  1. HDU-4055:Number String

    链接:HDU-4055:Number String 题意:给你一个字符串s,s[i] = 'D'表示排列中a[i] > a[i+1],s[i] = 'I'表示排列中a[i] < a[i+1 ...

  2. Docker虚拟机172.17网段冲突,导致网络访问问题

    在虚拟机中安装docker,linux ubuntu16 ,安装完公司172.17网段被docker0覆盖,导致ssh无法连接到ubuntu. 经过官网的这篇build your own bridge ...

  3. Rest-Assured 测试框架

    Rest-Assured 是一个测试 Restful Web Service 的 Java 类库,我们能够测试各种各样的请求组合,依次测试核心业务逻辑的不同组合. 它是通过发送特定的rest api, ...

  4. Tree - XGBoost with parameter description

    In the previous post, we talk about a very popular Boosting algorithm - Gradient Boosting Decision T ...

  5. 性能度量RMSE

    回归问题的典型性能度量是均方根误差(RMSE:Root Mean Square Error).如下公式. m为是你计算RMSE的数据集中instance的数量. x(i)是第i个实例的特征值向量 ,y ...

  6. 20172326『Java程序设计』课程结对编程练习_四则运算第二周阶段总结

    20172326『Java程序设计』课程结对编程练习_四则运算第二周阶段总结 小组成员 20172313 余坤澎 20172332 于欣月 20172326 康皓越 小组编程照片 设计思路 通过一个E ...

  7. python中 try、except、finally 的执行顺序

        def test1(): try: print('to do stuff') raise Exception('hehe') print('to return in try') return ...

  8. (Miller Rabin算法)判断一个数是否为素数

    1.约定 x%y为x取模y,即x除以y所得的余数,当x<y时,x%y=x,所有取模的运算对象都为整数. x^y表示x的y次方.乘方运算的优先级高于乘除和取模,加减的优先级最低. 见到x^y/z这 ...

  9. 如何通过JAVA让DB2调用操作系统命令

    引言:我们在工作中常用操作系统命令和DB2命令对数据库做数据的导入.导出等操作,但是DB2不支持复合SQL 语句调用操作系统命令,因此我们需要利用 UDF 来执行SQL 中不可用的操作(例如:执行一些 ...

  10. Node.js系列——(2)发起get/post请求

    服务器与浏览器的交互主要方式有get/post请求. 下面,我们来看一下node.js发起get/post请求. 1.get 由于get请求的参数在url后面,所以相对比较简单.node.js中的ur ...