CF585D Lizard Era: Beginning
嘟嘟嘟
题面我是不会咕的(没有真香):有\(n(n \leqslant 25)\)个任务和三个人,每次任务给出每个人能得到的值,每次任务选两个人,使\(n\)个任务结束后三个人得到的值是一样的,且尽量大。输出每次要派哪两个人,如果不行输出\(Impossible\)。
暴力是\(O(3 ^ {25})\),必定过不去,但是如果一半\(O(3 ^ {13})\)就刚好可以过了,因此想到折半搜索。
令搜到的前一半的结果为\(a, b, c\),后一半为\(x, y, z\),那么我们需要的是\(a + x = b + y = c + z\),其中\(a + x\)要尽量大。
根据折半搜索的方程模型,把上式变形,得到\(a - c = z - x, a - b = y - x\)。
因此我们搜前一半,记录\(a - c\)和\(a - b\)的值,并且如果有相同的,取\(a\)最大的。
然后搜后一半,看\(z - x\)和\(y - x\)这一对出没出现过,有的话就尝试用\(a + z\)更新答案。
还有一个问题,就是输出方案:采用三进制即可。
实现的时候开一个\(map\),下标是一个\(pair\)型,两个参数是\(a - c, a - b\),值也是一个\(pair\)型,记录此时最大的\(a\)和三进制方案\(f\)。总复杂度\(O(n ^ {\frac{n}{2}} * \log{\frac{n}{2}})\)
输出方案的时候别忘了前一半倒叙。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<map>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 30;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
int n, m, a[maxn], b[maxn], c[maxn];
struct Node
{
int b, c;
bool operator < (const Node& oth)const
{
return b < oth.b || (b == oth.b && c < oth.c);
}
};
map<Node, Node> mp;
void dfs(int stp, int x, int y, int z, int f)
{
if(stp > m)
{
Node tp1 = (Node){x - z, x - y};
if(mp.find(tp1) != mp.end())
{
if(mp[tp1].b < x) mp[tp1] = (Node){x, f};
}
else mp[tp1] = (Node){x, f};
return;
}
dfs(stp + 1, x + a[stp], y + b[stp], z, f * 3);
dfs(stp + 1, x + a[stp], y, z + c[stp], f * 3 + 1);
dfs(stp + 1, x, y + b[stp], z + c[stp], f * 3 + 2);
}
int ans = -INF, path1, path2;
void dfs2(int stp, int x, int y, int z, int f)
{
if(stp <= m)
{
Node tp1 = (Node){z - x, y - x};
if(mp.find(tp1) != mp.end())
{
if(mp[tp1].b + x > ans)
{
ans = mp[tp1].b + x;
path1 = mp[tp1].c; path2 = f;
}
}
return;
}
dfs2(stp - 1, x + a[stp], y + b[stp], z, f * 3);
dfs2(stp - 1, x + a[stp], y, z + c[stp], f * 3 + 1);
dfs2(stp - 1, x, y + b[stp], z + c[stp], f * 3 + 2);
}
const char ch[3][3] = {"LM", "LW", "MW"};
int num[maxn], cnt = 0;
void print()
{
cnt = 0;
for(int i = 1; i <= m; path1 /= 3, ++i) num[++cnt] = path1 % 3;
for(int i = cnt; i; --i) puts(ch[num[i]]);
for(int i = m + 1; i <= n; path2 /= 3, ++i) puts(ch[path2 % 3]);
}
int main()
{
n = read(); m = n >> 1;
for(int i = 1; i <= n; ++i) a[i] = read(), b[i] = read(), c[i] = read();
dfs(1, 0, 0, 0, 0);
dfs2(n, 0, 0, 0, 0);
if(ans == -INF) puts("Impossible");
else print();
return 0;
}
CF585D 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 ...
- (中等) CF 585D Lizard Era: Beginning,中途相遇。
In the game Lizard Era: Beginning the protagonist will travel with three companions: Lynn, Meliana a ...
- Codeforces 585D Lizard Era: Beginning
Lizard Era: Beginning 折半之后搜就完事了, 直接存string字符串卡空间, 随便卡卡空间吧. #include<bits/stdc++.h> #define LL ...
- 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> ...
- 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> ...
- Windows Programming ---- Beginning Visual C#
span.kw { color: #007020; font-weight: bold; } code > span.dt { color: #902000; } code > span. ...
随机推荐
- Linux root用户不能通过SSH连接的问题
http://jingyan.baidu.com/article/fd8044fad48fc95031137a85.html 最近在虚拟机安装Ubuntu之后,通过普通ssh远程连接的时候明明输入了正 ...
- 啰里吧嗦jvm
一.为什么要了解jvm 有次做项目的时候,程序run起来的时候,总是报OutOfMemoryError,有老司机教我们用jconsole.exe看内存溢出问题 就是这货启动jconsole后,发现一个 ...
- spring data jpa 的简单使用
先说简单一下JPA 概念:JPA(Java Persistence API)是Sun官方提出的Java持久化规范.它为Java开发人员提供了一种对象/关联映射工具来管理Java应用中的关系数据. 影响 ...
- 微信小程序之雪碧图(css script)
今天有朋友问我关于微信小程序中如何在不占用大量网络带宽的情况下快速加载图片,我给他推荐了两种方式 1.雪碧图(css script),有过前端经验的朋友应该都有接触过. 2.懒加载. 由于时间关系我就 ...
- 洛谷P3437 [POI2006]TET-Tetris 3D(二维线段树 标记永久化)
题意 题目链接 Sol 二维线段树空间复杂度是多少啊qwqqq 为啥这题全网空间都是\(n^2\)还有人硬要说是\(nlog^2n\)呀.. 对于这题来说,因为有修改操作,我们需要在外层线段树上也打标 ...
- webpack打包遇到过的问题
1.打包后html文件打开是空白页面,报错信息如图所示: 解决办法:这里主要是将assetsPublicPath的路径从'/'改为'./'就好了. ('/'表示根目录:'./'表示当前目录) 2.运行 ...
- [转]c# winform tcp connect timeout 连接超时设置
转自:https://www.cnblogs.com/jhlong/p/5622336.html 简单的c# TCP通讯(TcpListener) C# 的TCP Socket (同步方式) C# 的 ...
- redis list 查询、下标查询、删除、裁剪、压入弹出、队列实现
查询 lrange list 0 1 // 注意0和1之间是空格:这个命令和pop命令不一样,不会删除里面的值lrange list 0 -1 // 所有的 下标查询 lpush person zs ...
- 1 Java程序文件中函数起始行和终止行在程序文件位置中的判定__抽象语法树方法
应用需求: 实现对BigCloneBench中函数体的克隆检测,必须标注出起始行号和终止行号. 问题: 给定一个Java文件,从中提取出每个函数的起始行和终止行. 难点: 这个问题的难点在于,对于Ja ...
- springboot学习入门之三---启动原理
3启动原理 3.1启动类 @SpringBootApplication public class Application { public static void main(String[] args ...