Codeforces 585.D Lizard Era: Beginning

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[30],B[30],C[30],maxn,ans = -inf,printx,printy,num[2000010],cntt,cnt;
struct node
{
    int zhuangtai,x;
}e[2000010];

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 + 1)
    {
        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 = 0; i < 3; i++)
    {
        if (i == 0)
            dfs1(dep + 1,x + A[dep],y + B[dep],z,sta * 3);
        if (i == 1)
            dfs1(dep + 1,x + A[dep],y,z + C[dep],sta * 3 + 1);
        if (i == 2)
            dfs1(dep + 1,x,y + B[dep],z + C[dep],sta * 3 + 2);
    }
}

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 = 0; i < 3; i++)
    {
        if (i == 0)
            dfs2(dep - 1,x + A[dep],y + B[dep],z,sta * 3);
        if (i == 1)
            dfs2(dep - 1,x + A[dep],y,z + C[dep],sta * 3 + 1);
        if (i == 2)
            dfs2(dep - 1,x,y + B[dep],z + C[dep],sta * 3 + 2);
    }
}

void print()
{
    for (int i = 1; i <= maxn; i++)
    {
        num[++cntt] = printx % 3;
        printx /= 3;
    }
    for (int i = cntt; i >= 1; i--)
    {
        if (num[i] == 0)
            puts("LM");
        if (num[i] == 1)
            puts("LW");
        if (num[i] == 2)
            puts("MW");
    }
    cntt = 0;
    for (int i = maxn + 1; i <= n; i++)
    {
        num[++cntt] = printy % 3;
        printy /= 3;
    }
    for (int i = 1; i <= cntt; i++)
    {
        if (num[i] == 0)
            puts("LM");
        if (num[i] == 1)
            puts("LW");
        if (num[i] == 2)
            puts("MW");
    }
}

int main()
{
    scanf("%d",&n);
    for (int i = 1; i <= n; i++)
        scanf("%d%d%d",&A[i],&B[i],&C[i]);
    if (n % 2 == 0)
        maxn = n / 2;
    else
        maxn = n / 2 + 1;
    dfs1(1,0,0,0,0);
    dfs2(n,0,0,0,0);
    if (ans != -inf)
    print();
    else
        puts("Impossible");

    return 0;
}

 

转载于:https://www.cnblogs.com/zbtrs/p/8179153.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您提供的链接是Codeforces的一个问题,问题编号为104377。Codeforces是一个知名的在线编程竞赛平台,经常举办各种编程比赛和训练。Gym是Codeforces的一个扩展包,用于组织私人比赛和训练。您提供的链接指向了一个问题的页面,但具体的问题内容和描述无法通过链接获取。如果您有具体的问题或需要了解关于Codeforces Gym的更多信息,请提供更详细的信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [http://codeforces.com/gym/100623/attachments E题](https://blog.csdn.net/weixin_30820077/article/details/99723867)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [http://codeforces.com/gym/100623/attachments H题](https://blog.csdn.net/weixin_38166726/article/details/99723856)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [CodeforcesPP:Codeforces扩展包](https://download.csdn.net/download/weixin_42101164/18409501)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值