[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 ...
随机推荐
- 使用工具Android Studio实现一个简单的Android版的新闻APP
目的: 这是我学完Android课程后所写的一个小的.简单版的新闻APP 技术概要: 用到了SQLite数据库,用它来存储每篇新闻下的评论 新闻的来源是新浪新闻,我通过使用Fiddler来对新浪新闻A ...
- 灵光一现的trick
感觉平时会丢掉好多挺好的trick…… 图论 1.图G,固定S,T.可以将任意一条边加上权值$k(k>0)$,求最大化加权后最短路. 2.图G,固定S,T.可以将任意一条边乘以权值$k(k> ...
- Target runtime Apache Tomcat v8.5 is not defined.
Target runtime Apache Tomcat v8.5(或者其它版本) is not defined. 这个错误通常是在从文件夹中导入别人的项目的时候发生,因为 在 .setting 中有 ...
- ethereum(以太坊)(一)
从这周开始,开始学习以太坊开发--solidity,开始决定往区块链方向发展,毕竟区块链技术应用广泛.一开始接触solidity开发语言不太习惯,毕竟一直在学习python语法,有很多都不能接受.有难 ...
- Elasticsearch和Head插件安装
环境: CentOS7 Elasticsearch-6.3.2 JDK8 准备: JDK8 下载地址:http://www.oracle.com/technetwork/java/javase/do ...
- Python中的not, and, or
logical_operator_lst = [ ('and 与运算',), ('or 或运算',), ('not 非运算',), ('逻辑运算符的优先级',), ('实例',), ('练习',), ...
- Git-Git库管理
对象和引用哪里去了? 从GitHub上克隆一个示例版本库,这个版本库在"历史穿梭"一章就已经克隆过一次了,现在要重新克隆一份.为了和原来的克隆相区别,克隆到另外的目录.执行下面的命 ...
- HDFS HA 的 hdfs-site.xml
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <!-- Licens ...
- P2344 奶牛抗议
P2344 奶牛抗议 题目背景 Generic Cow Protests, 2011 Feb 题目描述 约翰家的N 头奶牛正在排队游行抗议.一些奶牛情绪激动,约翰测算下来,排在第i 位的奶牛的理智度为 ...
- 如何让button保持点击状态
http://blog.csdn.net/u010957508/article/details/38385207 他的原理就是在代码里面: view.setSelected(true); 而其他的: ...