UVALive 6931 Can't stop playing (Regionals 2014 >> Europe - Central)
题目
一开始有一个双头队列,每次添加一个数(这是数是二的幂,所有数的和不大于\(2^13\)),由你来决定添加到队头还是队尾。如果队列里面相邻的两个数相同,设它们都是\(x\),那么这两个数会合并为\(2x\)。问所有数添加完后队列里能否只剩下一个数。
算法
搜索题,但是需要巧妙地记录状态!这种题不可多。
一个显然的是,队列里不会存在相邻的三个数\(a,b,c\),满足\(a>b,c>b\)。这样的话,队列肯定是一个倒V字。
记状态\((i,j)\)为添加完前\(i\)个数,\(j\)是倒V左半边的上升序列状态,\(j\)最大是\(2^{13}\)。通过这两个,我们可以算出倒V右半边的序列状态!
这样的话,时间复杂度就是\(O(n 2^{13})\)。
代码
官方的标程有点点小错误。UVALive上的数据也错了。但当时比赛的数据是正确的!
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#include <assert.h>
using namespace std;
template <class T>
void tension(T &a, const T &b) {
if (b < a) a = b;
}
template <class T>
void relax(T &a, const T &b) {
if (b > a) a = b;
}
typedef long long i64;
#ifndef ONLINE_JUDGE
#define ep(...) fprintf(stderr, __VA_ARGS__)
#else
#define ep(...) assert(true)
#endif
int n;
int sum[1003], A[1003];
bool vis[1003][(1 << 13) + 1];
char ans[1003];
int highBit[1 << 14];
int place(int a, int b, int x) {
if (highBit[a] > highBit[b]) {
int tmp = highBit[a];
a -= tmp;
b += tmp;
}
if (a && (a & -a) < x) return -1;
a += x;
while (highBit[a] == highBit[b]) {
int tmp = highBit[a];
a += tmp;
b -= tmp;
}
return a;
}
bool dfs(int k, int s1) {
int s2 = (k ? sum[k - 1] : 0) - s1;
if (k == n) {
if (highBit[s1] > highBit[s2]) {
int tmp = highBit[s1];
s1 -= tmp;
s2 += tmp;
}
if (highBit[s2] > highBit[s1]) {
int tmp = highBit[s2];
s2 -= tmp;
s1 += tmp;
}
if (s1 == (s1 & -s1) && s2 == 0) return true;
if (s2 == (s2 & -s2) && s1 == 0) return true;
return false;
}
if (vis[k][s1]) return false;
vis[k][s1] = true;
int x = A[k];
int tmp = place(s1, s2, x);
if (tmp != -1 && dfs(k + 1, tmp)) {
//ep("l (%d, %d) %d ->\n", s1, s2, x);
ans[k] = 'l';
return true;
}
tmp = place(s2, s1, x);
if (tmp != -1 && dfs(k + 1, sum[k] - tmp)) {
//ep("r (%d, %d) %d ->\n", s1, s2, x);
ans[k] = 'r';
return true;
}
return false;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
//freopen("all.in", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
for (int i = 1; i < 1 << 14; i ++)
if ((i & -i) == i) highBit[i] = i;
else highBit[i] = highBit[i - 1];
scanf("%*d");
for (int cases = 1; scanf("%d", &n) == 1; cases ++) {
//ep("Case %d: ", cases);
for (int i = 0; i < n; i ++) {
scanf("%d", A + i);
sum[i] = A[i];
if (i) sum[i] += sum[i - 1];
}
//memset(vis, 0, sizeof vis);
for (int i = 0; i < n; i ++)
memset(vis[i], 0, sizeof vis[i]);
if (dfs(0, 0)) {
ans[n] = '\0';
printf("%s\n", ans);
}
else printf("no\n");
}
return 0;
}
UVALive 6931 Can't stop playing (Regionals 2014 >> Europe - Central)的更多相关文章
- UVALive 6584 Escape (Regionals 2013 >> Europe - Central)
题目 给出一棵树,每个节点有一个怪物或血药,遇到怪物必须打,打完后扣掉一定的血量. 一开始英雄的血量为\(0\),当血量小于\(0\)时就挂了. 给出英雄的起点和终点,问能否成功到达终点. 算法 这题 ...
- Regionals 2014 >> Asia - Taichung 7003 - A Balance Game on Trees 树形DP + 二维费用背包
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- UVALive 5545 Glass Beads
Glass Beads Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Origin ...
- UVALive 2664 One-way traffic
One-way traffic Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Or ...
- UVALive 2957 Bring Them There
Bring Them There Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. O ...
- UVALive 3989 Ladies' Choice
Ladies' Choice Time Limit: 6000ms Memory Limit: 131072KB This problem will be judged on UVALive. Ori ...
- UVALive 5583 Dividing coins
Dividing coins Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Ori ...
- UVALive 5292 Critical Links
Critical Links Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Ori ...
- BNU 33693——Problemsetting——————【枚举+最大流】
Problemsetting Time Limit: 5000ms Memory Limit: 131072KB 64-bit integer IO format: %lld Java cl ...
随机推荐
- CSS样式表基础知识、样式表的分类及选择器
一.CSS基本概念: CSS(Cascading Style Sheet,叠层样式表),作用是美化HTML网页. /*注释区域*/ 此为注释语法 二.样式表的分类 1.内联样式表(和html联合 ...
- Oracle游标-循环查询表中数据(表名),并执行
Oralce 表中存有一个字段,该字段存储表名,要把该表中的所有表名查询出来(即表名结果集),且执行结果集from 表名结果集: declare v_ccount ); --定义一个游标变量 curs ...
- mobilize扁平化的fullPage.js类工具使用心得
可以生成一个fullPage效果的主页,但是列表页面和内容页面呢? 主页中的block,可以选择多种组建生成.甚至连form都有: 应该改造其源代码,动态化和cms系统化,添加二三级页面模板: == ...
- tf–idf算法解释及其python代码实现(下)
tf–idf算法python代码实现 这是我写的一个tf-idf的简单实现的代码,我们知道tfidf=tf*idf,所以可以分别计算tf和idf值在相乘,首先我们创建一个简单的语料库,作为例子,只有四 ...
- [代码搜索]OpenGrok搭建简易教程
面对着动辄几十GB且随时不断更新的大型代码,我们产生了以下需求:1.快速搜索代码2.代码存放于本地/服务器3.代码可跳转4.跨平台5.易于维护... 显然SourceInsight.ctags.gre ...
- poj3308Paratroopers(最小割)
题目请戳这里 题目大意:给一个n*m的矩阵,给一些点(ri,ci)表示该点在第ri行第ci列.现在要覆盖所有的点,已知覆盖第i行代价为Ri,覆盖第j列代价为Cj.总代价是累乘的,求最小总代价能覆盖所有 ...
- objective-III 窗口应用程序
objective-III 一.创建窗口应用程序 打开xcode->create->在iso目录下选择empty-null->创建 在打开的项目文件名上右击NEW FILE,在io ...
- IIS express 7.5 配置和多网站执行
iis express7.5 支持xp 以上的操作系统,能够解决xp.iis的问题. 首先先下载安装iisexpress7.5地址是id=1038">点击打开链接下载完毕点击安装就可以 ...
- Service初步了解
1.Service什么 Service它是一个应用程序组件,Android其中的四个核心组件之间 Service没有图形界面 通过经常使用来处理一些比较长耗时的操作 可以使用Service更新Cont ...
- swift优秀学习博客
http://www.00red.com/ http://www.cnblogs.com/kenshincui/ 优秀的某博客,包含大量iOS的全面的总结 https://github.com/Co ...