首页 技术 正文
技术 2022年11月21日
0 收藏 691 点赞 4,844 浏览 2309 个字

嘟嘟嘟


题面我是不会咕的(没有真香):有\(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;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918