Codeforces 585D. Lizard Era: Beginning(meet in the middle)
一眼题...这个数据范围也太明显了吧...
suma1==suma2 && sumb1==sumb2 && sumc1==sumc2
相当于suma1-sumb1==sumb2-suma2 && suma1-sumc1==sumc2-suma2
于是前一半O(3^(N/2))搜出所有情况的suma1-sumb1和suma1-sumc1,后一半搜出sumb2-suma2和sumc2-suma2,都丢到一个数组里作为两个关键字排序,在两个关键字都相同的一段里面找到前一半最大的suma和后一半最大的suma,更新答案即可
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=, inf=1e9+;
struct poi{int x, y, st, a, ty;}v[maxn];
int n, cnt, ans, ansst1, ansst2;
int a[maxn], b[maxn], c[maxn], mi[];
void read(int &k)
{
int f=; k=; char c=getchar();
while(c<'' || c>'') c=='-' && (f=-), c=getchar();
while(c<='' && c>='') k=k*+c-'', c=getchar();
k*=f;
}
inline bool cmp(poi a, poi b){return a.x==b.x?(a.y<b.y):a.x<b.x;}
inline void printans(int x, int num)
{
for(int i=;i<=num;i++)
{
int now=x%;
if(now==) printf("LM\n");
else if(now==) printf("LW\n");
else printf("MW\n");
x/=;
}
}
int main()
{
read(n);
for(int i=;i<=n;i++) read(a[i]), read(b[i]), read(c[i]);
int l=(n+)>>, r=n;
mi[]=; for(int i=;i<=l;i++) mi[i]=mi[i-]*;
for(int i=;i<=mi[l]-;i++)
{
int tmpa=, tmpb=, tmpc=;
for(int j=;j<=l;j++)
{
int tmp=i/mi[j-]%;
if(tmp==) tmpa+=a[j], tmpb+=b[j];
else if(tmp==) tmpa+=a[j], tmpc+=c[j];
else tmpb+=b[j], tmpc+=c[j];
}
v[++cnt].x=tmpa-tmpb; v[cnt].y=tmpa-tmpc; v[cnt].a=tmpa; v[cnt].st=i; v[cnt].ty=;
}
for(int i=;i<=mi[r-l]-;i++)
{
int tmpa=, tmpb=, tmpc=;
for(int j=;j<=r-l;j++)
{
int tmp=i/mi[j-]%;
if(tmp==) tmpa+=a[j+l], tmpb+=b[j+l];
else if(tmp==) tmpa+=a[j+l], tmpc+=c[j+l];
else tmpb+=b[j+l], tmpc+=c[j+l];
}
v[++cnt].x=tmpb-tmpa; v[cnt].y=tmpc-tmpa; v[cnt].a=tmpa; v[cnt].ty=; v[cnt].st=i;
}
sort(v+, v++cnt, cmp); ans=-inf;
for(int i=, j=;i<=cnt;i=j)
{
int tmp1=-inf, tmp2=-inf, tmpst1=, tmpst2=;
while(v[j].x==v[i].x && v[j].y==v[i].y)
{
if(v[j].ty==) {if(tmp1<v[j].a) tmp1=v[j].a, tmpst1=v[j].st;}
else {if(tmp2<v[j].a) tmp2=v[j].a, tmpst2=v[j].st;}
j++;
}
if(tmp1==-inf || tmp2==-inf) continue;
if(tmp1+tmp2>ans) ans=tmp1+tmp2, ansst1=tmpst1, ansst2=tmpst2;
}
if(ans==-inf) return puts("Impossible"), ;
printans(ansst1, l); printans(ansst2, r-l);
}
Codeforces 585D. Lizard Era: Beginning(meet in the middle)的更多相关文章
- Codeforces 585D Lizard Era: Beginning
Lizard Era: Beginning 折半之后搜就完事了, 直接存string字符串卡空间, 随便卡卡空间吧. #include<bits/stdc++.h> #define LL ...
- [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> ...
- 【CF888E】Maximum Subsequence(meet in the middle)
[CF888E]Maximum Subsequence(meet in the middle) 题面 CF 洛谷 题解 把所有数分一下,然后\(meet\ in\ the\ middle\)做就好了. ...
- (中等) CF 585D Lizard Era: Beginning,中途相遇。
In the game Lizard Era: Beginning the protagonist will travel with three companions: Lynn, Meliana a ...
- [CSP-S模拟测试]:答题(meet in the middle)
题目传送门(内部题142) 输入格式 输入文件的第一行为两个数$n,P$. 接下来一行$n$为个正整数,表示每道题的分数. 输出格式 输出一行一个正整数,为至少需要获得的分数. 样例 样例输入: 2 ...
- [CSP-S模拟测试]:毛一琛(meet in the middle)
题目描述 历史学考后,$MYC$和$ztr$对答案,发现选择题他们没有一道选的是一样的.最后他们都考了个$C$.现在问题来了,假设他们五五开,分数恰好一样(问答题分数也恰好一样,只考虑选择题).已知考 ...
- CF888E Maximum Subsequence(meet in the middle)
给一个数列和m,在数列任选若干个数,使得他们的和对m取模后最大( \(1<=n<=35\) , \(1<=m<=10^{9}\)) 考虑把数列分成两份,两边分别暴力求出所有的可 ...
- 「笔记」折半搜索(Meet in the Middle)
思想 先搜索前一半的状态,再搜索后一半的状态,再记录两边状态相结合的答案. 暴力搜索的时间复杂度通常是 \(O(2^{n})\) 级别的.但折半搜索可以将时间复杂度降到 \(O(2 \times 2^ ...
随机推荐
- python常用模块详解2
序列化模块补充: 1.json格式的限制,json格式的key必须是字符串数据类型 2.json格式的字符串必须是"" 如果数字是key,那么dump之后会强转成字符串数据类型 i ...
- Charles连接苹果及JSON乱码情况解决
1. Charles的JSON乱码情况解决: 点击Charles界面上的help—SSL proxying—install Charles Root Certificate,将证书安装到[受信任的根 ...
- 教你thinkphp5怎么配置二级域名
有些项目要将移动端和PC端分离开来,比如访问xxx.com,展示的是PC端的页面.而访问m.xxx.com,展示的是移动端的页面.thinkphp源码需要多多学习,这里记录一下知识点,顺便分享给需要的 ...
- linux 命令自动补全包
linux 其他知识目录 rhel7如果使用最小化安装后,tab键默认是不能自动补全命令的 执行yum install bash-completion之后重启系统正常.
- Spring Bean注册解析(一)
Spring是通过IoC容器对Bean进行管理的,而Bean的初始化主要分为两个过程:Bean的注册和Bean实例化.Bean的注册主要是指Spring通过读取配置文件获取各个bean的 ...
- c++ Dynamic Memory (part 1)
1. make_shared<T>(args): return a shared_ptr dynamically allocated object of type T. Use args ...
- ffmpe安装
原文:https://www.jianshu.com/p/905df3d9e753 下载安装 下载最新源码包并解压 $ wget http://ffmpeg.org/releases/ffmpeg-3 ...
- 作业MathExamV2.0
MathExam233 211614269 林凯 211601233张康凌 一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时( ...
- ubuntu安装和查看已安装软件
说明:由于图形化界面方法(如Add/Remove... 和Synaptic Package Manageer)比较简单,所以这里主要总结在终端通过命令行方式进行的软件包安装.卸载和删除的方法. 一.U ...
- Java中的抽象类abstract
abstract定义抽象类 abstract定义抽象方法,只需要声明,不需要实现 包含抽象方法的类是抽象类 抽象类中可以包含抽象方法,也可以包含普通方法 抽象类不能直接创建,可以定义父类引用变量指向子 ...