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的签到题一般是一道神神奇奇的数 ...
随机推荐
- 152.[LeetCode] Maximum Product Subarray
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- sql注入waf绕过简单入门
0x1 白盒 0x2 黑盒 一.架构层 1.寻找源站==> 2.利用同网段==> 3.利用边界漏洞==> ssrf只是一个例子 二.资源限制 Waf为了保证业务运行,会忽略对大的数 ...
- 关于XSS的一些知识点
安全套接层(SSL)无助于减少XSS攻击.当Web浏览器使用SSL的时候,在网络中传送的数据是经过加密的,但是因为XSS攻击是在客户机器上发生的,所以数据已经被解密了,这时,攻击者仍然能够利用XSS安 ...
- 15 分钟用 ML 破解一个验证码系统
人人都恨验证码——那些恼人的图片,显示着你在登陆某网站前得输入的文本.设计验证码的目的是,通过验证你是真实的人来避免电脑自动填充表格.但是随着深度学习和计算机视觉的兴起,现在验证码常常易被攻破. 我拜 ...
- Intense Heat(前缀和或尺取)
The heat during the last few days has been really intense. Scientists from all over the Berland stud ...
- Alpha发布——美工+文案
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2283 一.功能介绍 本团队(可以低头,但没必要)开发的是一款基于腾讯微信 ...
- CentOS6.5 重启网络报错:Bringing up interface eth0: Error: Connection activation failed: Device not managed by NetworkManager or unavailable
CentOS6.5 重启网络报错: Bringing up interface eth0: Error: Connection activation failed: Device not manage ...
- Privoxy
Privoxy 前沿: 这个玩意我以前都没听说过,今天在别人的帮助下试了试,只想说:谁还能阻挡我,我就是要USA....ps:在此感谢每一个帮助我的人 介绍: Privoxy是一款带过滤功能的代理服务 ...
- python之enumerate()学习
X = 'abcdefghijklmn' for (index,char) in enumerate(X): print (index, char) 利用enumerate()函数,可以在每次循环中同 ...
- 第157天:canvas基础知识详解
目录 一.canvas简介 1.1 什么是canvas?(了解) 1.2 canvas主要应用的领域(了解) 二.canvas绘图基础 2.0 sublime配置canvas插件(推荐) 2.1 Ca ...