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. 利用python的爬虫技术爬取百度贴吧的帖子

    在爬取糗事百科的段子后,我又在知乎上找了一个爬取百度贴吧帖子的实例,为了巩固提升已掌握的爬虫知识,于是我打算自己也做一个. 实现目标:1,爬取楼主所发的帖子 2,显示所爬去的楼层以及帖子题目 3,将爬 ...

  2. Linux之nc命令详解

    nc是一个强大的网络工具,可以通过yum安装 [root@LB2 ~]# which nc /usr/bin/which: no nc in (/usr/local/sbin:/usr/local/b ...

  3. JvisualVM、JMC监控远程服务器

    修改服务器上jmxremote.access与jmxremote.password,输入命令: find -name jmxremote.access进入该jmxremote.access文件所在目录 ...

  4. 腾讯云数据库团队:PostgreSQL TOAST技术理解

    作者介绍:胡彬 腾讯云高级工程师 TOAST是"The Oversized-Attribute Storage Technique"的缩写,主要用于存储一个大字段的值.要理解TOA ...

  5. css远距离链接

    远距离链接主要运用了hover伪类,但是运用了两次 <!DOCTYPE html> <html lang="en"> <head> <me ...

  6. Java基础之J2EE规范

    什么是J2EE? 在企业级应用中,都有一些通用企业需求模块,如数据库连接,邮件服务,事务处理等.既然很多企业级应用都需要这些模块,一些大公司便开发了自己的通用模块服务,即中间件.这样一来,就避免了重复 ...

  7. Centos/RHEL上查看主板型号

    老是搞忘记,专门做个记录: [root@media ~]# dmidecode | grep "Product Name" Product Name: To be filled b ...

  8. POST与GET

    面试如果被问到这个问题,相信很多人都是会心一笑,答案随口而来: 1.GET在浏览器回退时是无害的,而POST会再次提交请求. 2.GET请求会被浏览器主动cache,而POST不会,除非手动设置. 3 ...

  9. 用swap函数交换两个整数

    #include<stdio.h> //头文件 main() //主函数 { void swap(int *p,int *q); //声明 int a,b; //定义两个整数 int *p ...

  10. 利用sub lr,lr,#4:程序是如何进行返回的?

    1: ARM采用的是3级流水线 ARM的流水线结构为:   取指 -----> 译码 ------> 执行 ARM代码:                  PC           PC- ...