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^ ...
随机推荐
- 车架号识别,VIN码识别 助力汽车后市场
又有一家汽配圈新贵引入了小译家的 车架号识别(VIN码识别)技术 那就是明觉科技 是一个服务于汽车后市场 集数据服务.行业数据挖掘 及“互联网+”为一体的汽配信息协作平台 旗下拥有一款全车零配件信息智 ...
- eclipse查看源代码问题
最近分析源代码时,eclipse总是出错,显示org.eclipse.core.runtime.CoreException,解决方法:在builderpath点击 add external jars, ...
- openstack系列文章(三)
学习openstack的系列文章-glance glance 基本概念 glance 架构 openstack CLI Troubleshooting 1. glance 基本概念 在 opensta ...
- Matplotlib用法
一 环境安装 Make sure you have installed numpy. 先安装np pip install matplotlib (Python2.X) pip3 install mat ...
- 遗传算法框架GAFT优化小记
前言 前段时间一直在用自己写的遗传算法框架测试算法在优化力场参数的效果,但是跑起来效率很慢,因为适应度函数需要调用多次力场程序计算能量,但是还是比我预想中的慢我也没有及时对程序进行profiling和 ...
- nginx gzip压缩配置
gzip(GNU-ZIP)是一种压缩技术.经过gzip压缩后页面大小可以变为原来的30%甚至更小,这样,用户浏览页面的时候速度会块得多.gzip 的压缩页面需要浏览器和服务器双方都支持,实际上就是服务 ...
- [C++] Solve "No source available for main()" error when debugging on Eclipse
In Mac, the issue image: 1. A existing cmake project on disk 2. import this project into Eclipse. 3 ...
- 你应该知道的PHP库
Libchart – 这也是一个简单的统计图库. JpGraph – 一个面向对象的图片创建类. Open Flash Chart – 这是一个基于Flash的统计图. RSS 解析 解释RSS并是一 ...
- static块的本质
在网上看到了下面的一段代码: public class Test { static { _i = 20; } public static int _i = 10; public static void ...
- Numpy and Pandas
安装 视频链接:https://morvanzhou.github.io/tutorials/data-manipulation/np-pd/ pip install numpy pip instal ...