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. nodejs express 加载html模板

    在nodejs中如使用express框架,她默认的是ejs和jade渲染模板.由于我在使用的时候觉得她的代码书写方式很不爽还是想用html的形式去书写,于是我找了使用了html模板. 直接上代码,主要 ...

  2. 如何在DCS管理控制台将两个Redis主备实例建立全球灾备。

    华为云分布式缓存服务DCS,具有强大的功能,现在小编教大家如何在DCS管理控制台将两个Redis主备实例建立全球灾备. 建立全球灾备,会对主实例和备实例进行升级,实例进程会重启,连接会中断.同时备实例 ...

  3. 【python 3.6】使用itertools.product进行排列组合

    #python 3.6 #!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = 'BH8ANK' import itertools colo ...

  4. 利用Tensorflow进行自然语言处理(NLP)系列之二高级Word2Vec

    本篇也同步笔者另一博客上(https://blog.csdn.net/qq_37608890/article/details/81530542) 一.概述 在上一篇中,我们介绍了Word2Vec即词向 ...

  5. gopherjs

    An example implementation of a GopherJS client and a Go server using the Improbable gRPC-Web impleme ...

  6. “Hello World!”团队第六周的第五次会议

    今天是我们团队“Hello World!”团队第六周召开的第五次会议.博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.todo list 六.会议照片 七.燃尽图 八.代码 一 ...

  7. 20162320MyOD重做版

    博客说明 由于上次的MyOD.java没有得分,所以这次我重做了这个java,代码是自己完成的,请教了一些同学的思路.故补交一篇博客来说明我对每一步代码的编写的想法以及理解. 代码片段及理解 1.先创 ...

  8. Numpy and Pandas

    安装 视频链接:https://morvanzhou.github.io/tutorials/data-manipulation/np-pd/ pip install numpy pip instal ...

  9. ASLR/DEP绕过技术概览

    在经典的栈溢出模型中,通过覆盖函数的返回地址来达到控制程序执行流程(EIP寄存器),通常将返回地址覆盖为0x7FFA4512,这个地址是一条JMP ESP指令,在函数返回时就会跳转到这个地址去执行,也 ...

  10. c99标准的restrict关键字

    参考自restrict restrict解释 restrict关键字出现于C99标准,wiki上的解释restrict from wiki. In the C programming language ...