[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 ...
随机推荐
- Decrypt.java
import java.io.PrintStream;import weblogic.security.internal.*;import weblogic.security.internal.enc ...
- react搭建一个框架(react-redux-axios-antd Designs)
废话不多说,先给一个github案例:前往.. 1.create-react-app <文件名> 安装router:npm i react-router-dom -S; npm inst ...
- lintcode_69_二叉树的层次遍历
二叉树的层次遍历 描述 笔记 数据 评测 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 您在真实的面试中是否遇到过这个题? Yes 哪家公司问你的这个题? LinkedIn Airb ...
- pip安装第三方包总失败
第一步:升级pip python -m pip install -U pip 第二布:安装想下载的第三方包 python -m pip install xx 一般来说pip安装不会失败的,失败的话就尝 ...
- myql简单语法测试
删除某一行 delete from name1 where agee=10 limit 1; insert into name1(agee,namee)values(10,'wwww'),(10,' ...
- uniqueidentifier数据类型转换
cast(id as varchar(36))
- Fruits【水果】
Fruits Many of us love July because it's the month when nature's berries and stone fruits are in abu ...
- Multiplication Puzzle ZOJ - 1602
Multiplication Puzzle ZOJ - 1602 传送门 The multiplication puzzle is played with a row of cards, each c ...
- POJ:3279-Fliptile(矩阵反转)
Fliptile Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14701 Accepted: 5381 Description ...
- 一个比较良好的flask项目结构
一个比较良好的flask项目结构 project/ app/ # 整个程序的包目录 static/ # 静态资源文件 js/ ...