ALDS1_9_A-CompleteBinaryTree.
Codes:
//#define LOCAL

#include <cstdio>

int parent(int i) { return i/2; }
int left(int i) { return i*2; }
int right(int i) { return i*2+1; } int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif int i, n, A[300];
scanf("%d", &n);
for(i=1; i<=n; ++i) scanf("%d", &A[i]); for(i=1; i<=n; ++i) {
printf("node %d: key = %d, ", i, A[i]);
if(parent(i) >= 1) printf("parent key = %d, ", A[i/2]);
if(left(i) <= n) printf("left key = %d, ", A[i*2]);
if(right(i) <= n) printf("right key = %d, ", A[i*2+1]);
printf("\n");
} return 0;
}
ALDS1_9_B-MaximumHeap.
Codes:
//#define LOCAL

#include <cstdio>

int i, n, A[500010];

void swap(int &a, int &b) {
int t = a;
a = b; b = t;
} void maxHeapify(int a) {
int l = a*2, r = a*2+1, maxn;
if(l<=n && A[l]>A[a]) maxn = l;
else maxn = a;
if(r<=n && A[r]>A[maxn]) maxn = r;
if(maxn != a) {
swap(A[maxn], A[a]);
maxHeapify(maxn);
}
} int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif scanf("%d", &n);
for(i=1; i<=n; ++i) scanf("%d", &A[i]); for(i=n/2; i>=1; --i) maxHeapify(i);
for(i=1; i<=n; ++i) printf(" %d", A[i]);
printf("\n"); return 0;
}
ALDS1_9_C-PriorityQueue.
Codes:
//#define LOCAL

#include <cstdio>

#define M 2000000
#define I (1<<30)
int i, n, A[M]; void swap(int &a, int &b) {
int t = a;
a = b; b = t;
} void maxHeapify(int a) {
int l = a*2, r = a*2+1, maxn;
if(l<=n && A[l]>A[a]) maxn = l;
else maxn = a;
if(r<=n && A[r]>A[maxn]) maxn = r;
if(maxn != a) {
swap(A[maxn], A[a]);
maxHeapify(maxn);
}
} int extract() {
int maxv;
if(n < 1) return -I;
maxv = A[1]; A[1] = A[n--];
maxHeapify(1);
return maxv;
} void increaseKey(int i, int key) {
if(key < A[i]) return;
A[i] = key;
while(i>1 && A[i/2]<A[i]) {
swap(A[i], A[i/2]);
i /= 2;
}
} void insert(int key) {
++n;
A[n] = -I;
increaseKey(n, key);
} int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif int key;
char com[10]; while(1) {
scanf("%s", com);
if(com[0]=='e' && com[1]=='n') break;
if(com[0] == 'i') {
scanf("%d", &key);
insert(key);
} else printf("%d\n", extract());
} return 0;
}
ALDS1_10_A-FibonacciNumber.
Codes:
//#define LOCAL

#include <cstdio>

int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif int i, n, F[50];
F[0] = 1, F[1] = 1;
scanf("%d", &n);
for(i=2; i<=n; ++i)
F[i] = F[i-1]+F[i-2];
printf("%d\n", F[n]); return 0;
}
ALDS1_10_C-LongestCommonSubsequence.
Codes:

AOJ/堆与动态规划习题集的更多相关文章

  1. BZOJ4919 大根堆(动态规划+treap+启发式合并)

    一个显然的dp是设f[i][j]为i子树内权值<=j时的答案,则f[i][j]=Σf[son][j],f[i][a[i]]++,f[i][a[i]+1~n]对其取max.这样是可以线段树合并的, ...

  2. 剑指 Offer 49. 丑数 + 小根堆 + 动态规划

    剑指 Offer 49. 丑数 Offer_49 题目详情 解法一:小根堆+哈希表/HashSet 根据丑数的定义,如果a是丑数,那么a2, a3以及a*5都是丑数 可以使用小根堆存储按照从小到大排序 ...

  3. bzoj 4585 烟火表演 - 动态规划 - 可并堆

    题目传送门 传送门I 传送门II 题目大意 给定一棵带边权有根树,修改一条边的边权的代价是修改前和修改后的值的绝对值之差.不能将一条边的边权改为负数.问使得根节点到所有叶节点的距离相等的最小代价. 当 ...

  4. Vijos 1404 遭遇战 - 动态规划 - 线段树 - 最短路 - 堆

    背景 你知道吗,SQ Class的人都很喜欢打CS.(不知道CS是什么的人不用参加这次比赛). 描述 今天,他们在打一张叫DUSTII的地图,万恶的恐怖分子要炸掉藏在A区的SQC论坛服务器!我们SQC ...

  5. 【bzoj1109】[POI2007]堆积木Klo 动态规划+树状数组

    题目描述 Mary在她的生日礼物中有一些积木.那些积木都是相同大小的立方体.每个积木上面都有一个数.Mary用他的所有积木垒了一个高塔.妈妈告诉Mary游戏的目的是建一个塔,使得最多的积木在正确的位置 ...

  6. LG5202 「USACO2019JAN」Redistricting 动态规划+堆/单调队列优化

    问题描述 LG5202 题解 \[opt[i]=xx+(cnt[i]-cnt[yy]<=0)\] 发现\(cnt[i]-cnt[yy] <= 0\)只能有两种取值 于是直接堆优化即可 \( ...

  7. AOJ/初等排序习题集

    ALDS1_1_D-MaximumProfit. Codes: //#define LOCAL #include <cstdio> #include <algorithm> u ...

  8. AOJ/数据结构习题集

    ALDS1_3_A-Stack. Description: Write a program which reads an expression in the Reverse Polish notati ...

  9. AOJ/搜索递归分治法习题集

    ALDS1_4_A-LinearSearch. Description: You are given a sequence of n integers S and a sequence of diff ...

随机推荐

  1. setDefaultCloseOperation()参数得使用说明

    System.exit(0)是退出整个程序,如果有多个窗口,全部都销毁退出.setDefaultCloseOperation()是设置用户在此窗体上发起 "close" 时默认执行 ...

  2. dos中进入其他盘中的方法

    1.在dos中进入其他盘中直接-->e: 即可,若是进入文件夹中-->cd aaa\bbb 即可. 2.返回上级目录:-->cd..

  3. unity3D插件开发——前篇

    Unity3D(以下简称Unity)是今年来非常流行的游戏开发引擎.他不仅有足够和unreal(虚幻)引擎媲美的渲染效果,足够多的平台输出,更为突出的就是Unity本身的编辑器.unity本身除了使用 ...

  4. JS——控制标记的样式

    1.定义一个div,宽度为100px,高度为100px,背景色为粉色. 定义一个事件,鼠标移入时背景色变为蓝色,宽度变为200px. 定义一个事件,鼠标移出时背景色变为红色. html文件: < ...

  5. 记录一次坑爹的VM连接主机的路程

    因为之前电脑配置过虚拟机连接主机的过程,所以没有太在意,换电脑了之后配了两天结果没有配置成功; 首先配置静态ip: 1,编辑第一个文件/etc/sysconfig/network-scripts/if ...

  6. 反射实现 Data To Model

    调用 : public ActionResult Index() { DataTable dt = new DataTable(); dt.Columns.Add("Name"); ...

  7. 微信小程序支付简单小结与梳理

    前言 公司最近在做微信小程序,被分配到做支付这一块,现在对这一块做一个简单的总结和梳理. 支付,对于购物来说,可以说是占据了十分重要的一块,毕竟能收到钱才是重点. 当然在开发之前,我们需要有下面这些东 ...

  8. Android下使用busybox的ifconfig

    busybox ifconfig eth0 10.0.16.45 netmask 255.255.254.0 broadcast 10.0.16.186busybox route add defaul ...

  9. python 自动化接口测试(6)

    迎接新的一波更新吧,这次是基于图灵机器人的一个api接口的测试. 这是api的接口:http://www.tuling123.com/openapi/api 我们试着通过浏览器直接访问看下 这是反馈的 ...

  10. jquery的冒泡事件event.stopPropagation()

    js中的冒泡事件与事件监听 冒泡事件 js中“冒泡事件”并不是能实际使用的花哨技巧,它是一种对js事件执行顺序的机制,“冒泡算法”在编程里是一个经典问题,冒泡算法里面的冒泡应该 说是交换更加准确:js ...