嘟嘟嘟




题面我是不会咕的(没有真香):有\(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. Q:判断链表中是否存在环的相关问题

    问题:如何判断一个单向链表中是否存在环? 例如: 链表中存在环(B-->D): <-- <--^ | | v | A-->B-->C-->D 链表中不存在环: A- ...

  2. sql判断某个字段是否为空

    判断sql某个字段是否为NULL public function dataNull($id){ $sql = 'SELECT * FROM `vvt_company_funcs_user` WHERE ...

  3. Matlab给三维点云添加高斯噪声和随机噪声

    写在前面 在我们进行点云配准一类的模拟实验时,第一步就是对原始点云进行适当的RT变换,并添加一定的噪声,得到测量点云,然后才可以用我们的算法去进行后面的配准操作.在添加噪声这一块,matlab里并没有 ...

  4. canvas验证码 - 随机字母数字

    基于canvas制作随机生成数字英文组合验证码效果,点击或刷新会自动重组.输入验证码提交验证效果代码. <div class="verification"> <i ...

  5. HttpSession implements session

    体验 使用HttpSession进行会话管理,完全可以忽略HTTP无状态的事实. HttpSession会话管理原理 使用HttpSession进行会话管理十分方便,让Web应用程序看似可以“记得”浏 ...

  6. 【Python】Java程序员学习Python(四)— 内置方法和内置变量

    <假如爱有天意> 当天边那颗星出现,你可知我又开始想念,有多少爱恋只能遥遥相望,就像月光洒向海面,年少的我们曾以为,相爱的人就能到永远,当我们相信情到深处在一起,听不见风中的叹息,谁知道爱 ...

  7. 6.Spring MVC SSM整合问题总结

    1.Cannot find class [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter] for ...

  8. PHP匹配当前传入是何种类型

    本文出至:新太潮流网络博客 /** * [is_string_regular_type 正则自动验证传入数据] * @E-mial wuliqiang_aa@163.com * @TIME 2017- ...

  9. 记开发个人图书收藏清单小程序开发(七)DB设计

    前面的书房初始化的前端信息已经完善,所以现在开始实现DB的Script部分. 新增Action:Shelf_Init.sql svc.sql CREATE SCHEMA [svc] AUTHORIZA ...

  10. Windows+Git+TortoiseGit+COPSSH 安装教程及问题收集

    准备工作: 1. git-1.8.1.2-preview20130201.exe 下载地址: https://code.google.com/p/msysgit/downloads/list 2. C ...