AOJ/堆与动态规划习题集
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/堆与动态规划习题集的更多相关文章
- 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.这样是可以线段树合并的, ...
- 剑指 Offer 49. 丑数 + 小根堆 + 动态规划
剑指 Offer 49. 丑数 Offer_49 题目详情 解法一:小根堆+哈希表/HashSet 根据丑数的定义,如果a是丑数,那么a2, a3以及a*5都是丑数 可以使用小根堆存储按照从小到大排序 ...
- bzoj 4585 烟火表演 - 动态规划 - 可并堆
题目传送门 传送门I 传送门II 题目大意 给定一棵带边权有根树,修改一条边的边权的代价是修改前和修改后的值的绝对值之差.不能将一条边的边权改为负数.问使得根节点到所有叶节点的距离相等的最小代价. 当 ...
- Vijos 1404 遭遇战 - 动态规划 - 线段树 - 最短路 - 堆
背景 你知道吗,SQ Class的人都很喜欢打CS.(不知道CS是什么的人不用参加这次比赛). 描述 今天,他们在打一张叫DUSTII的地图,万恶的恐怖分子要炸掉藏在A区的SQC论坛服务器!我们SQC ...
- 【bzoj1109】[POI2007]堆积木Klo 动态规划+树状数组
题目描述 Mary在她的生日礼物中有一些积木.那些积木都是相同大小的立方体.每个积木上面都有一个数.Mary用他的所有积木垒了一个高塔.妈妈告诉Mary游戏的目的是建一个塔,使得最多的积木在正确的位置 ...
- LG5202 「USACO2019JAN」Redistricting 动态规划+堆/单调队列优化
问题描述 LG5202 题解 \[opt[i]=xx+(cnt[i]-cnt[yy]<=0)\] 发现\(cnt[i]-cnt[yy] <= 0\)只能有两种取值 于是直接堆优化即可 \( ...
- AOJ/初等排序习题集
ALDS1_1_D-MaximumProfit. Codes: //#define LOCAL #include <cstdio> #include <algorithm> u ...
- AOJ/数据结构习题集
ALDS1_3_A-Stack. Description: Write a program which reads an expression in the Reverse Polish notati ...
- AOJ/搜索递归分治法习题集
ALDS1_4_A-LinearSearch. Description: You are given a sequence of n integers S and a sequence of diff ...
随机推荐
- wireshark过滤规则
WireShark过滤语法 1.过 滤IP,如来源IP或者目标IP等于某个IP 例子:ip.src eq 192.168.1.107 or ip.dst eq 192.168.1.107或者ip.ad ...
- python3.6 简单爬虫
# coding='UTF-8' from bs4 import BeautifulSoup # 引入beautifulsoup 解析html事半功倍 import re import urllib ...
- windows和linux删除文件方法
Windows下bat文件内容如下:复制代码 代码如下: @echo offforfiles -p "D:\servers\apache2.2\logs" -s -m *.log ...
- 10大支持移动“触摸操作”的JavaScript框架
摘要:移动开发行业的发展速度让人目不暇接,也在此大势之下,推出移动网站App成为开发者必经之路,如何让触屏设备 更易使用?如何让网站对触摸手势做出反应并使触摸更友好?所有这一切,皆因JavaScrip ...
- vuejs子组件向父组件传递数据
子组件通过$emit方法向父组件发送数据,子组件在父组件的模板中,通过自定义事件接收到数据,并通过自定义函数操作数据 <!DOCTYPE html> <html lang=" ...
- [LeetCode] Zuma Game 题解
题目 题目 Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B ...
- Hibernate原理、配置及单表操作
一.Hibernate的配置文档 其中:hbm2ddl.auto中的create表示每次修改数据的时候都会删除原有的表,生成新的表结构,原有的数据不再存在:update表示在原有数据的基础上进行更新, ...
- 老李分享:jvm结构简介 1
老李分享:jvm结构简介 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:9088214 ...
- 关于WdatePicker.js的结束时间大于开始时间
简单笔记 : WdatePicker.js 要使结束时间大于开始时间只要在线束时间的 minDate:'#F{$dp.$D(\'stimeParam\')}' 即可:不多说 详细代码如下: <t ...
- SQL SERVER:CASE判断空,错误一例
-----错误判断------------------------------------------------------------------------------------ SELEC ...