Codeforces 585.D Lizard Era: Beginning
2 seconds
256 megabytes
standard input
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.
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.
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.
3
1 0 0
0 1 0
0 0 1
LM
MW
MW
7
0 8 9
5 9 -2
6 -8 -7
9 4 5
-4 -9 9
-4 5 2
-6 8 -7
LM
MW
LM
LW
MW
LM
LW
2
1 0 0
1 1 0
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的更多相关文章
- 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 ...
- Codeforces 585D Lizard Era: Beginning
Lizard Era: Beginning 折半之后搜就完事了, 直接存string字符串卡空间, 随便卡卡空间吧. #include<bits/stdc++.h> #define LL ...
- (中等) CF 585D Lizard Era: Beginning,中途相遇。
In the game Lizard Era: Beginning the protagonist will travel with three companions: Lynn, Meliana a ...
- Codeforces Round #325 (Div. 1) D. Lizard Era: Beginning
折半搜索,先搜索一半的数字,记录第一个人的值,第二个人.第三个人和第一个人的差值,开个map哈希存一下,然后另一半搜完直接根据差值查找前一半的答案. 代码 #include<cstdio> ...
- Codeforces 585D. Lizard Era: Beginning(meet in the middle)
一眼题...这个数据范围也太明显了吧... suma1==suma2 && sumb1==sumb2 && sumc1==sumc2 相当于suma1-sumb1==s ...
- [codeforces] 585D Lizard Era: Beginning || 双向dfs
原题 有n(n<=2)个任务和三个人,每次任务给出每个人能得到的值,每次任务选两个人,使n个任务结束后三个人得到的值是一样的.输出每次要派哪两个人,如果不行输出Impossible. n< ...
- Codeforces 585D Lizard Era: Beginning | 折半搜索
参考这个博客 #include<cstdio> #include<algorithm> #include<cstring> #include<map> ...
- CF585D Lizard Era: Beginning
嘟嘟嘟 题面我是不会咕的(没有真香):有\(n(n \leqslant 25)\)个任务和三个人,每次任务给出每个人能得到的值,每次任务选两个人,使\(n\)个任务结束后三个人得到的值是一样的,且尽量 ...
- Codeforces Round 585
Codeforces Round 585 浅论如何发现自己是傻子的-- 反正今天是完全蒙的,水了签到题就跑了-- A. Yellow Cards 签到题. 众所周知,CF的签到题一般是一道神神奇奇的数 ...
随机推荐
- ES6的新特性(10)——Class 的基本语法
Class 的基本语法 简介 JavaScript 语言中,生成实例对象的传统方法是通过构造函数.下面是一个例子. function Point(x, y) { this.x = x; this.y ...
- 按Right-BICEP要求的测试用例
测试方法:Right-BICEP 测试计划 1.Right-结果是否正确? 2.B-是否所有的边界条件都是正确的? 3.P-是否满足性能要求? 4.结果是否有符合要求的20道题目? 5.所得到的最大数 ...
- [hook.js]通用Javascript函数钩子及其他
2013.02.16<:article id=post_content> 最近看Dom Xss检测相关的Paper,涉及到Hook Javascript函数,网上翻了一下,貌似没有什么通用 ...
- 多线程PV
#include <STDIO.H> #include <windows.h> //#include "stdafx.h" #include <pro ...
- 【BioCode】读文件夹以发现缺失文件
代码说明: 使用单个蛋白质的txt计算PSSM生成的结果为单个的PSSM文件. 但是由于一些原因(如蛋白质序列过长),会导致一些蛋白质txt文件无法计算出pssm,为了找到这些没有计算出pssm的蛋白 ...
- java和mysql的length()区别及char_length()
一. mysql里面的有length和char_length两个长度函数,区别在于: length: 一个汉字是算三个字符,一个数字或字母算一个字符. char_length: 不管汉字还是数字或者是 ...
- 【转】史上最浅显易懂的Git教程!
之前一直在找git的学习教程,网上搜到很多,但是大多数写的都非常简单或者混乱,你知道技术男的思维就是以为他抛一个专业术语出来,以为你都懂……或者简单写两句,插个图,他觉得他懂了,你也能懂,事实上初学者 ...
- 【前端学习笔记】JavaScript 小案例合集
获取一个0-9的随机数: Math.round(Math.random()*9); 去除数组中重复的元素: var arr=[1,3,5,4,3,3,1,4] function editArr(arr ...
- 25个Java机器学习工具&库--转载
本列表总结了25个Java机器学习工具&库: 1. Weka集成了数据挖掘工作的机器学习算法.这些算法可以直接应用于一个数据集上或者你可以自己编写代码来调用.Weka包括一系列的工具,如数据预 ...
- 多线程---handlerthread
当我们需要工作线程来操作的时候,很多时候会有同步问题,UI更新问题. Handle机制就是为了解决这个问题而产生的. android允许每个线程都有自己的消息队列,同时也可以是主线程消息队列. 但是很 ...