• 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;
    }
  • 相关阅读:
    掩膜操作手写+API(第二天)
    图像的加载+修改+显示+保存(第一天)
    VS2015+Opencv半永久配置
    关于QT中“崩溃”问题
    关于QT内部16进制、十进制、QByteArray,QString
    QT按键(Qbutton)改变颜色
    QT中使用自己定的类和Vector出现错误
    Spring Boot 集成Jsp与生产环境部署
    SpringBoot入门篇--关于properties和yml两种配置文件的一些事情
    简单的linux使用命令
  • 原文地址:https://www.cnblogs.com/zbtrs/p/8179153.html
Copyright © 2020-2023  润新知