嘟嘟嘟




题面我是不会咕的(没有真香):有\(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的更多相关文章

  1. 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 ...

  2. (中等) CF 585D Lizard Era: Beginning,中途相遇。

    In the game Lizard Era: Beginning the protagonist will travel with three companions: Lynn, Meliana a ...

  3. Codeforces 585D Lizard Era: Beginning

    Lizard Era: Beginning 折半之后搜就完事了, 直接存string字符串卡空间, 随便卡卡空间吧. #include<bits/stdc++.h> #define LL ...

  4. Codeforces 585.D Lizard Era: Beginning

    D. Lizard Era: Beginning time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  5. Codeforces Round #325 (Div. 1) D. Lizard Era: Beginning

    折半搜索,先搜索一半的数字,记录第一个人的值,第二个人.第三个人和第一个人的差值,开个map哈希存一下,然后另一半搜完直接根据差值查找前一半的答案. 代码 #include<cstdio> ...

  6. Codeforces 585D. Lizard Era: Beginning(meet in the middle)

    一眼题...这个数据范围也太明显了吧... suma1==suma2 && sumb1==sumb2 && sumc1==sumc2 相当于suma1-sumb1==s ...

  7. [codeforces] 585D Lizard Era: Beginning || 双向dfs

    原题 有n(n<=2)个任务和三个人,每次任务给出每个人能得到的值,每次任务选两个人,使n个任务结束后三个人得到的值是一样的.输出每次要派哪两个人,如果不行输出Impossible. n< ...

  8. Codeforces 585D Lizard Era: Beginning | 折半搜索

    参考这个博客 #include<cstdio> #include<algorithm> #include<cstring> #include<map> ...

  9. Windows Programming ---- Beginning Visual C#

    span.kw { color: #007020; font-weight: bold; } code > span.dt { color: #902000; } code > span. ...

随机推荐

  1. 数组的strong copy理解

      一.数组的不同情况下的copy,mutablecopy分析 1.不可变数组的copy(没有创建新对象,复制的只是指针)       2.不可变数组的mutable copy(创建新对象)     ...

  2. 浅谈JVM-图解类加载机制

    一.目录 二.类加载机制流程 1.什么是类加载机制? JVM把class文件加载到内存里面,并对数据进行校验.准备.解析和初始化,最终能够被形成被JVM可以直接使用的Java类型的过程. 2.类加载流 ...

  3. SpringMVC作用、SpringMVC核心组件、创建项目流程

    SpringMVC框架 1. 作用 解决了V-C的交互问题,即视图与控制器的交互问题. 在原生的Java EE技术中,使用Servlet作为项目中的控制器,用于接收用户的请求,并给予响应结果.这种做法 ...

  4. Airbnb JavaScript Style Guide

      Airbnb JavaScript Style Guide() { 用更合理的方式写 JavaScript    ES5 的编码规范请查看版本一,版本二. 翻译自 Airbnb JavaScrip ...

  5. 2017年CCF大数据与计算智能大赛,梳理总结新鲜出炉啦~~~

    0 序言 比赛已经过去一段时间,现在才来写总结似乎有点儿晚,但是挡不住内心发出的强烈呼唤的声音,所以决定静下心来梳理一遍,查缺补漏. 参赛契机: 2017年9月偶然在学校的官方微信推送中看到2017年 ...

  6. Python的正则表达式与JSON

    Python的正则表达式需要导入re模块 菜鸟教程:http://www.runoob.com/python/python-reg-expressions.html 官方文档:https://docs ...

  7. ionic开发中遇到的问题

    开发调试过程中,会遇到这样的问题:同源策略请求url禁止请求. 一   网上搜的结果基本是2类: 1. 同源策略请求被阻止, 跨域问题,大家建议添加Access-Control-Allow-Origi ...

  8. 日期选择器(DatePicker)

    日期选择器(DataPicker) 显示一个可供日期选择的界面 监听器方法init(year,month,day,OnDateChangedListener) 监听器 DataPicker.OnDat ...

  9. 你写的什么垃圾代码让Vsync命令不能及时处理呢?(2)

    接上篇 1.TraceView Traceview看起来复杂,其实很简单: 上部分图中,X代表时间消耗,Y轴代表各个线程中的方法,且使用了不同颜色表示.面积越款,时间越长. 下部分为分析面板,分析面板 ...

  10. 机器学习数学知识中令人费解的notation符号注解

    $argmin_xf(x), min(f(x))$ $min(f(x))$的意思是函数$f(x)$的最小值 $argmin$的意思是返回使得表达式取得最小值时对应的输入变量值.例如$argmin_xf ...