[codeforces] 585D Lizard Era: Beginning || 双向dfs
原题
有n(n<=2)个任务和三个人,每次任务给出每个人能得到的值,每次任务选两个人,使n个任务结束后三个人得到的值是一样的。输出每次要派哪两个人,如果不行输出Impossible。
n<=25,325肯定不行,所以考虑折半3(n/2)。前一半我们得到a,b,c,后一半我们得到x,y,z,我们要得到a+x=b+y=c+z。将式子变形为a-b=y-x和b-c=z-y,所以用map记录a-b和b-c,以及对应的最大的a和状态(三进制表示)。然后查找y-x和z-y是否存在,得到答案即可。
#include<cstdio>
#include<map>
#include<vector>
typedef long long ll;
using namespace std;
map < pair<int,int>, pair<int,int> > mp;
map < pair<int,int>, pair<int,int> > :: iterator it;
int ans=-97797977,n,a[30][4];
ll front,back;
vector <int> v;
int read()
{
int ans=0,fu=1;
char j=getchar();
for (;(j<'0' || j>'9') && j!='-';j=getchar()) ;
if (j=='-') j=getchar(),fu=-1;
for (;j>='0' && j<='9';j=getchar()) ans*=10,ans+=j-'0';
return ans*fu;
}
void dfs1(int x,int l,int m,int w,int f)
{
if (x>n/2)
{
it = mp.find(make_pair(l-m,w-m));
if (it!=mp.end())
{
if(l > it -> second.first)
mp[make_pair(l-m,w-m)]=make_pair(l,f);
}
else mp[make_pair(l-m,w-m)]=make_pair(l,f);
return ;
}
dfs1(x+1,l+a[x][1],m+a[x][2],w,f*3+0);
dfs1(x+1,l+a[x][1],m,w+a[x][3],f*3+1);
dfs1(x+1,l,m+a[x][2],w+a[x][3],f*3+2);
}
void dfs2(int x,int l,int m,int w,int f)
{
if (x>n)
{
it=mp.find(make_pair(m-l,m-w));
if (it!=mp.end())
{
if (it->second.first+l>ans)
{
ans=it->second.first+l;
front=it->second.second;
back=f;
}
}
return ;
}
dfs2(x+1,l+a[x][1],m+a[x][2],w,f*3+0);
dfs2(x+1,l+a[x][1],m,w+a[x][3],f*3+1);
dfs2(x+1,l,m+a[x][2],w+a[x][3],f*3+2);
}
void print(int x)
{
if (!x) printf("LM\n");
else if (x==1) printf("LW\n");
else printf("MW\n");
}
int main()
{
n=read();
for (int i=1;i<=n;i++)
{
a[i][1]=read();
a[i][2]=read();
a[i][3]=read();
}
dfs1(1,0,0,0,0);
dfs2(n/2+1,0,0,0,0);
if (ans>-97797977)
{
for (int i=n-n/2;i>=1;i--)
{
v.push_back(back%3);
back/=3;
}
for (int i=n/2;i>=1;i--)
{
v.push_back(front%3);
front/=3;
}
for (int i=v.size()-1;i>=0;i--)
print(v[i]);
}
else printf("Impossible");
return 0;
}
[codeforces] 585D Lizard Era: Beginning || 双向dfs的更多相关文章
- Codeforces 585D Lizard Era: Beginning
Lizard Era: Beginning 折半之后搜就完事了, 直接存string字符串卡空间, 随便卡卡空间吧. #include<bits/stdc++.h> #define LL ...
- Codeforces 585D. Lizard Era: Beginning(meet in the middle)
一眼题...这个数据范围也太明显了吧... suma1==suma2 && sumb1==sumb2 && sumc1==sumc2 相当于suma1-sumb1==s ...
- Codeforces 585D Lizard Era: Beginning | 折半搜索
参考这个博客 #include<cstdio> #include<algorithm> #include<cstring> #include<map> ...
- (中等) 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. 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 585.D Lizard Era: Beginning
D. Lizard Era: Beginning time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Codeforces Round #325 (Div. 1) D. Lizard Era: Beginning
折半搜索,先搜索一半的数字,记录第一个人的值,第二个人.第三个人和第一个人的差值,开个map哈希存一下,然后另一半搜完直接根据差值查找前一半的答案. 代码 #include<cstdio> ...
- CF585D Lizard Era: Beginning
嘟嘟嘟 题面我是不会咕的(没有真香):有\(n(n \leqslant 25)\)个任务和三个人,每次任务给出每个人能得到的值,每次任务选两个人,使\(n\)个任务结束后三个人得到的值是一样的,且尽量 ...
- HDU 1269.迷宫城堡-Tarjan or 双向DFS
迷宫城堡 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
随机推荐
- 【模板时间】◆模板·I◆ 倍增计算LCA
[模板·I]LCA(倍增版) 既然是一篇重点在于介绍.分析一个模板的Blog,作者将主要分析其原理,可能会比较无趣……(提供C++模板) 另外,给reader们介绍另外一篇非常不错的Blog(我就是从 ...
- poj_2689_Prime Distance
The branch of mathematics called number theory is about properties of numbers. One of the areas that ...
- 私有maven库发布及使用流程
## 私有maven库发布流程 ### 环境配置 - idea环境下,如果使用内置maven,需要手动生成settings.xml,并关联. - 操作如下 - 生成settings.xml 右键pom ...
- CCS选择器基础
上周学习了HTML和css的一些基础,今天来夯实一下基础 选择器有: 1.标签选择器 :就是HTML 中的标签 如<p> <h1> <body>等 2.类选择器: ...
- tomcat日志切割脚本
tomcat日志每俩小时切割的脚本如下(这是用定时任务来完成的,此方法无需重启tomcat): time=$(date +%H) end_time=`` a=$end_time BF_TIME=$(- ...
- Pagehelper介绍
本文引自:https://my.oschina.net/zudajun/blog/745232 摘要: com.github.pagehelper.PageHelper是一款好用的开源免费的Mybat ...
- P1414 又是毕业季II (数学?
题目背景 “叮铃铃铃”,随着高考最后一科结考铃声的敲响,三年青春时光顿时凝固于此刻.毕业的欣喜怎敌那离别的不舍,憧憬着未来仍毋忘逝去的歌.1000多个日夜的欢笑和泪水,全凝聚在毕业晚会上,相信,这一定 ...
- HyperLedger Fabric 1.4 区块链技术定义(2.1)
区块链技术指使用点对点传输.共识机制.加密算法等技术,保证分布式数据库区块写入链中数据的一致性,达到去中心化和不可篡改的目的. 区块链就是一种特殊的分布式数据库,使用现有的各种成熟的技术, ...
- PHP.36-TP框架商城应用实例-后台12-商品管理-主分类添加、修改、搜索(连表查询)
需求:一个商品必须有一个主分类,一个主分类可以有多个商品 [一对多] 修改表p39_goods,增加外键约束,增加索引 主分类添加[控制器->页面] 1.在控制器GoodsController. ...
- 小白日记1:kali环境Wpscan渗透Wordpress
一.什么是Wpscan?什么是Wordpres? 1.Wpscan WPScan是一款针对wordpress的安全扫描软件:可以扫描出wordpress的版本,主题,插件,后台用户以及爆破后台用户密码 ...