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. ...
随机推荐
- Docker网络实践运用
Docker 容器网络概述 要构建具有安全的一致行为的 Web 应用程序,可以使用 Docker 网络特性.根据定义,网络为容器实现了完全隔离.因此,控制应用程序所在的网络很重要.Docker 容器网 ...
- python 历险记(五)— python 中的模块
目录 前言 基础 模块化程序设计 模块化有哪些好处? 什么是 python 中的模块? 引入模块有几种方式? 模块的查找顺序 模块中包含执行语句的情况 用 dir() 函数来窥探模块 python 的 ...
- HDU 2669 Romantic 扩展欧几里德---->解不定方程
Romantic Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- 基于Maven + SSM (Spring、SpringMVC、Mybatis)构建一个简单的测试项目
最近在公司实习期间的培训交流中有机会接触到SSM,然后自己花费1周的时间投入学习.谈不上深刻理解其中原理,所以没有涉及理论知识,只是浅层次的学习如何使用,在此将学习过程记录整理出来,一方面自己备用:另 ...
- 镶嵌数据集 Mosaic Dataset 的常见数据组织方式
镶嵌数据集是ESRI公司推出一种用于管理海量影像数据的数据模型,定义在GeoDatabase数据模型中. 它的常见数据组织方式有两种: 1. 源镶嵌数据集 Source Mosaic Dataset ...
- 最长斐波那契序列-LeetCode-873
英文版A sequence X_1, X_2, ..., X_n is fibonacci-like if: - n >= 3- X_i + X_{i+1} = X_{i+2} for all ...
- 10个经典的Android开源应用项目
Android开发又 将带来新一轮热潮,很多开发者都投入到这个浪潮中去了,创造了许许多多相当优秀的应用.其中也有许许多多的开发者提供了应用开源项目,贡献出他们的智慧和 创造力.学习开源代码是掌握技术的 ...
- 合理选择css3动画实现方式
使用css3实现动画,比js控制DOM属性的方式要高效很多.流畅很多,主要分transition和animation两大方式. transition适用于一次性变换 animation适用于循环动画和 ...
- 【Java】得到本机IP
import java.net.InetAddress; import java.net.UnknownHostException; public class MainProcess { public ...
- LeetCode题解之Flipping an Image
1.题目描述 2.题目分析 使用C++的迭代器 3.代码 vector<vector<int>> flipAndInvertImage(vector<vector< ...