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的签到题一般是一道神神奇奇的数 ...
随机推荐
- 袋鼠云研发手记 | 数栈·开源:Github上400+Star的硬核分布式同步工具FlinkX
作为一家创新驱动的科技公司,袋鼠云每年研发投入达数千万,公司80%员工都是技术人员,袋鼠云产品家族包括企业级一站式数据中台PaaS数栈.交互式数据可视化大屏开发平台Easy[V]等产品也在迅速迭代.在 ...
- 剑指offer-数值的整数方
数值的整数方 一.问题描述 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 二.算法思路 按照指数Exp的情况进行讨论. Exp> ...
- Kafka安装之二 在CentOS 7上安装Kafka
一.简介 Kafka是由Apache软件基金会开发的一个开源流处理平台,由Scala和Java编写.Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据. 这 ...
- 【转】React-Native 实现增量热更新的思路
所谓热更新就是在不重新安装的前提下进行代码和资源的更新,相信在整个宇宙中还不存在觉得热更新不重要的程序猿. 增量热更新就更牛逼了,只需要把修改过和新增的代码和资源推送给用户下载即可,增量部分的代码和资 ...
- ecshop以及一些需要注意的
Deprecated: Assigning the return value of new by reference is deprecated in 定位到出错的那一行: $this->_ol ...
- Shell脚本初学习
第一个shell程序运行,教程来自:http://jingyan.baidu.com/article/8cdccae947f83e315413cd05.html 代码如下: #!/bin/sh tou ...
- 每日Scrum--No.2
Yesterday:找地图 Today: 找到最适合我们软件的地图版本 Problem:找不到特别匹配的版本
- 电梯V2.0
电梯V2.0 GitHub仓库地址 Problem 一栋10层的大楼(楼层编号1-10),设3台无限载重的电梯,初始时电梯停在1层.其中:1号电梯只能停留在奇数层,2号电梯可以各层都停留,3号电梯只停 ...
- CCF——门禁系统201412-1
问题描述 涛涛最近要负责图书馆的管理工作,需要记录下每天读者的到访情况.每位读者有一个编号,每条记录用读者的编号来表示.给出读者的来访记录,请问每一条记录中的读者是第几次出现. 输入格式 输入的第一行 ...
- 数据输出保存生成word文档
ob_start(); //打开缓冲区 $header_str = '<html xmlns:o="urn:schemas-microsoft-com:office:office&qu ...