题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5573

这个题……规律暂时还找不到,先贡献两发TLE的代码吧,一个dfs一个状压枚举。

 #include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; typedef long long ll;
const int maxn = ; ll n, k;
ll path[maxn][];
int pcnt;
//1 + 0 -
bool exflag;
void dfs(ll cur, ll lv, ll id) {
if(exflag) return;
if(cur == n && lv == k) {
for(ll i = ; i < pcnt; i++) {
printf("%I64d %c\n", path[i][], path[i][] == ? '+' : '-');
}
exflag = ;
return;
}
if(cur != n && lv == k) return; path[pcnt][] = id;
path[pcnt++][] = ;
dfs(cur+id ,lv+, id*);
pcnt--; path[pcnt][] = id;
path[pcnt++][] = ;
dfs(cur+id ,lv+, id*+);
pcnt--; path[pcnt][] = id;
path[pcnt++][] = ;
dfs(cur-id ,lv+, id*);
pcnt--; path[pcnt][] = id;
path[pcnt++][] = ;
dfs(cur-id ,lv+, id*+);
pcnt--;
} int main() {
// freopen("in", "r", stdin);
int T, _ = ;
scanf("%d", &T);
while(T--) {
scanf("%I64d %I64d", &n, &k);
pcnt = ; exflag = ;
printf("Case #%d:\n", _++);
dfs(, , );
}
return ;
}

DFS

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; typedef long long ll;
const int maxn = ;
ll n, k;
ll f[maxn];
ll ans[maxn];
bool sub[maxn]; void init() {
f[] = ;
for(int i = ; i < maxn; i++) {
f[i] = f[i-] * ;
}
} int main() {
// freopen("in", "r", stdin);
int T, _ = ;
init();
scanf("%d", &T);
while(T--) {
scanf("%I64d %I64d", &n, &k);
for(int i = ; i <= k; i++)
ans[i] = f[i-];
if(n % == ) ans[k]++;
ll nn = << k;
bool exflag = ;
for(ll i = ; i < nn; i++) {
if(exflag) break;
ll cur = ;
memset(sub, , sizeof(sub));
for(ll j = ; j <= k; j++) {
if(( << j) & i) {
sub[j] = ;
cur -= ans[j];
}
else cur += ans[j];
}
if(cur == n) exflag = ;
}
printf("Case #%d:\n", _++);
for(ll i = ; i <= k; i++) {
printf("%I64d ", ans[i]);
if(sub[i]) printf("-\n");
else printf("+\n");
}
}
return ;
}

ENUM

这个题想了很多天,想明白了其实还蛮简单的。

题目给了一棵满二叉树,按照层次遍历从左到右挨个编号1 2 3....问蛤蛤从根节点向下走,走到一个点可以加上当前节点编号也可以删掉当前节点编号。问走k层能否恰好续够n。

题目中给了一个条件:N≤2^K≤2^60

因为读题坑掉了没看到这个条件,浪费了很多时间在例如n=10 k=3的情况上。这种情况在我的搜索中是完全有结果的,但是实际上这个情况不会在题目中出现,因为8<10。

这样就好办了,我们考虑任何一个十进制数都可以表示为二进制,这个二进制表示了某一位上是否要加上对应的2的幂次。

(以上皆为口胡+脑补,正常题解在下面)

N<=2^k意味着我们总能找到第k+1个节点,使得N小于k+1节点的数值。既然如此,我们贪心地选取最左边的一条链。这样,最左边那个点必然是整层最小的。对于本题目而言,总有2^(k+1)-1≥n。

由于最左侧的链均为2的幂次,我们以前的知识中一定有这样一条规律:2^(k)-1=∑i(1,k-1)2^i。表达不清楚,举个例子:32-1=1+2+4+8+16。

我们假设整条长度为k链都是加的,那它的总和就是2^(k+1)-1,我们现在知道要求的n,那我们不需要的那部分的值为2^(k+1)-1-n。

假设这个值为x,那x也总是能表达为一个二进制数,我们只需要在这个链子上找到可以表示x的二进制数的位置,把它们标记为'-'即可。

 #include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; typedef long long ll;
const int maxn = ;
ll n, k;
ll f[maxn];
ll ans[maxn];
bool sub[maxn]; void init() {
f[] = ;
for(int i = ; i < maxn; i++) {
f[i] = f[i-] * ;
}
} int main() {
// freopen("in", "r", stdin);
int T, _ = ;
init();
scanf("%d", &T);
while(T--) {
scanf("%I64d %I64d", &n, &k);
memset(sub, , sizeof(sub));
for(int i = ; i <= k; i++)
ans[i] = f[i-];
ll remain = f[k] - n - ;
if(n % == ) {
ans[k]++;
remain++;
}
remain >>= ;
int cnt = ;
while(remain) {
if(remain % == ) sub[cnt] = ;
remain >>= ;
cnt++;
}
printf("Case #%d:\n", _++);
for(int i = ; i <= k; i++) {
printf("%I64d ", ans[i]);
sub[i] ? printf("-\n") : printf("+\n");
}
}
return ;
}

[HDOJ5573]Binary Tree(找规律,贪心)的更多相关文章

  1. 找规律/贪心 Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones

    题目传送门 /* 找规律/贪心:ans = n - 01匹配的总数,水 */ #include <cstdio> #include <iostream> #include &l ...

  2. [LeetCode] 366. Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

  3. [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

  4. LeetCode Minimum Depth of Binary Tree 找最小深度(返回最小深度)

    题意:找到离根结点最近的叶子结点的那一层(设同一层上的结点与根结点的距离相等),返回它所在的层数. 方法有: 1.递归深度搜索 2.层次搜索 方法一:递归(无优化) /** * Definition ...

  5. Codeforces Round #265 (Div. 2) C 暴力+ 找规律+ 贪心

    C. No to Palindromes! time limit per test 1 second memory limit per test 256 megabytes input standar ...

  6. hdu - 6277,2018CCPC湖南全国邀请赛B题,找规律,贪心找最优.

    题意: 给出N个小时,分配这些小时去写若干份论文,若用1小时写一份论文,该论文会被引用A次,新写一篇论文的话,全面的论文会被新论文引用一次. 找最大的H,H是指存在H遍论文,而且这些论文各被引用大于H ...

  7. UVALive - 6577 Binary Tree 递推+找规律

    题目链接: http://acm.hust.edu.cn/vjudge/problem/48421 Binary Tree Time Limit: 3000MS 问题描述 Binary Tree is ...

  8. Full Binary Tree(二叉树找规律)

    Description In computer science, a binary tree is a tree data structure in which each node has at mo ...

  9. [LeetCode] Find Mode in Binary Search Tree 找二分搜索数的众数

    Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...

随机推荐

  1. 【BZOJ】【2765】【JLOI2010】铁人双项比赛

    计算几何/半平面交 本来我是想去写POJ 1755的,然后想起了这道跟它很像的题,但应该是弱化版,所以就先写了这个…… 我们可以发现每个人的总用时,与k是呈一次函数关系的:$time_i=\frac{ ...

  2. 【bzoj1085】[SCOI2005]骑士精神

    1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1757  Solved: 961[Submit][Statu ...

  3. Google NACL 简介

    Back to README Getting Started This page tells you how to install Native Client and run demos, both ...

  4. 【面试题】Round A China New Grad Test 2014总结

    我也有够懒的,今天才跑来写总结,自觉面壁中… 上一篇是Practice Round,今天是Round A,五道题. 每次做完都想说,其实题不难..但在做的过程中总是会各种卡,只有自己一行一行实现了,才 ...

  5. DevExpress Form那些事儿

    1:设置子窗体依附父窗体 首先将父窗体的属性中  IsMdiContainer 设置为 True   , 就是将窗体设置为 MDI窗体.子窗体和父窗体都是继承自RibbonForm的. 代码如下 : ...

  6. ExtJs布局之Card

    <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...

  7. SIOCADDRT: No such process

    配置Ubuntu靶机遇到的问题 如果你添加/修改默认网关时遇到这个问题. 原因:你要添加的网关不在你主机所在的网段. 解决方法: 比如你要添加的网关是10.57.50.1 sudo route add ...

  8. Jmeter之Bean shell使用(二)

    上一篇Jmeter之Bean shell使用(一)简单介绍了下Jmeter中的Bean shell,本文是对上文的一个补充,主要总结下常用的几种场景和方法,相信这些基本可以涵盖大部分的需求.本节内容如 ...

  9. java中静态代理,动态代理知识的补充

    文章转载自:http://blog.csdn.net/jialinqiang/article/details/8950989 一.Java动态代理 相对于静态代理的代理类在编译时生成(.class文件 ...

  10. iOS 网络请求NSURLSession

    iOS 7 和 Mac OS X 10.9 Mavericks 中一个显著的变化就是对 Foundation URL 加载系统的彻底重构. 现在已经有人在深入苹果的网络层基础架构的地方做研究了,所以我 ...