[Codeforces 585D] Lizard Era: Beginning (折半枚举)
来源:互联网 发布:神经网络如何预测数据 编辑:程序博客网 时间:2024/04/29 17:38
Codeforces - 585D
有三个开始为 0的数,有
N 次选择,每次改变其中两个
问最后使得三个数相等且最大的选择方案
其中N≤25
一看
直接状压是不行的,所以折半状压
先用三进制压前一半,每一位表示不选的那个人
然后把后两个人对第一个人价值的差当作 key
存一下第一个人的最大价值,以及此时的状态
然后枚举后一半,就可以
时间复杂度为
如果将最后的前一半和后一半的状态搞在一起的话
要注意
#pragma comment(linker, "/STACK:102400000,102400000")#include <cstdio>#include <iostream>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>#include <cctype>#include <map>#include <set>#include <queue>#include <bitset>#include <string>using namespace std;typedef pair<int,int> Pii;typedef long long LL;typedef unsigned long long ULL;typedef double DBL;typedef long double LDBL;#define MST(a,b) memset(a,b,sizeof(a))#define CLR(a) MST(a,0)#define SQR(a) ((a)*(a))#define PCUT puts("\n----------")const char name[]="LMW";const int INF=0x3f3f3f3f;int N, W[3][30], pow3[15];map<Pii,Pii> stor;int main(){ #ifdef LOCAL freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout); #endif pow3[0]=1; for(int i=1; i<15; i++) pow3[i]=3*pow3[i-1]; while(~scanf("%d", &N)) { stor.clear(); int hl=N>>1, le=N-hl, ans=-INF; LL mask=0; for(int i=0; i<N; i++) for(int j=0; j<3; j++) scanf("%d", &W[j][i]); if(N==1) { int cnt=0; char res[3]={0}; for(int i=0; i<3 && cnt<2; i++) if(!W[i][0]) res[cnt++] = name[i]; if(cnt<2) puts("Impossible"); else puts(res); continue; } for(int m=0,res[3],tem; m<pow3[hl]; m++) { tem = m; CLR(res); for(int i=0; i<hl; i++) { for(int j=0; j<3; j++) if(j!=tem%3) res[j] += W[j][i]; tem /= 3; } res[1] -= res[0]; res[2] -= res[0]; auto it = stor.find(Pii(res[1],res[2])); if(it == stor.end()) stor.insert( make_pair(Pii(res[1],res[2]), Pii(res[0],m)) ); else if(it->second.first < res[0]) it->second = Pii(res[0], m); } for(int m=0,res[3],tem; m<pow3[le]; m++) { tem = m; CLR(res); for(int i=hl; i<N; i++) { for(int j=0; j<3; j++) if(j!=tem%3) res[j] += W[j][i]; tem /= 3; } res[1] -= res[0]; res[2] -= res[0]; auto it = stor.find(Pii(-res[1], -res[2])); if(it != stor.end() && it->second.first+res[0] > ans) { ans = it->second.first+res[0]; mask = (LL)m*pow3[hl] + it->second.second; } } if(ans==-INF) puts("Impossible"); else { char res[3]={0}; for(int i=0; i<N; i++,mask/=3,puts(res)) for(int j=0,cnt=0; j<3 && cnt<2; j++) if(j!=mask%3) res[cnt++] = name[j]; } } return 0;}
0 0
- [Codeforces 585D] Lizard Era: Beginning (折半枚举)
- Codeforces 585D Lizard Era: Beginning
- CodeForces 585D Lizard Era: Beginning
- codeforces585D. Lizard Era: Beginning
- HDU 5936 && 2016CCPC杭州 D: Difference(折半枚举)
- CodeForces 478 E.Wavy numbers(折半枚举+map)
- CodeForces 478 E.Wavy numbers(折半枚举+map)
- codeforces 888E Maximum Subsequence (折半枚举 双向搜索)
- poj2785(折半枚举)
- poj2549(折半枚举)
- POJ2785 (折半枚举)
- POJ3977(折半枚举)
- poj3977Subset (折半枚举)
- codeforces 158D Ice Sculptures (枚举)
- codeforces 479D Long Jumps(枚举)
- codeforces 496D Tennis Game(枚举)
- CodeForces 633 D.Fibonacci-ish(枚举)
- Educational Codeforces Round 32 E 折半枚举
- 封装图片轮播器
- sqlserver使用CHARINDEX搜索
- 【持久化框架】SpringMVC+Spring4+Mybatis3集成
- Laravel入门
- ios中蓝牙开发
- [Codeforces 585D] Lizard Era: Beginning (折半枚举)
- 网络爬虫Java还是Python还是c++?
- 微信关注即可使用Wi-Fi是怎样实现的?
- windows10 安装MongoDB
- 2015 北京区域赛现场赛 部分题解
- 错误:'XXX/XXX' does not contain bitcode.
- java.lang.NumberFormatException null
- 利用git上传项目到github里面去的基本操作
- 网络接口学习方法总结