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 ...
随机推荐
- Eclipse集成Tomcat教程
(初学者都会问一个问题,就是Eclipse好用还是Myeclipse好用.好吧,这个问题我昨晚才刚刚问完,哈哈,因为我一开始学Java都是直接下了一个MyeClipse来用的,没想过太多.其实也是,两 ...
- mongoose简单使用样例
新建文件 app.js 内容如下: var mongoose = require('mongoose') , Schema = mongoose.Schema; mongoose.connect('m ...
- iOS性能之其他
本篇文章是个引用,因为这些技术我都只是研究过,但是并没有在项目中使用,也没有深入研究,所以只能当做一个笔记了 网络请求 现在大多数的网络请求都是使用的json格式(相信没有APP再使用XML格式了吧) ...
- jQuery中jsonp函数实现
由于浏览器中的同源策略,不同的域名,不同的协议,甚至不同的端口都无法请求数据.因此出现了浏览器跨域请求数据问题. Jsonp是解决跨域问题的一个非常流行的方法. JSONP(JSON with Pad ...
- C语言指针基础
新手在C语言的学习过程中遇到的最头疼的知识点应该就是指针了,指针在C语言中有非常大的用处.下面我就带着问题来写下我对于指针的一些理解. 指针是什么? 指针本身是一个变量,它存储的是数据在内存中的地址 ...
- 老李推荐:第6章3节《MonkeyRunner源码剖析》Monkey原理分析-事件源-事件源概览-命令翻译类
老李推荐:第6章3节<MonkeyRunner源码剖析>Monkey原理分析-事件源-事件源概览-命令翻译类 每个来自网络的字串命令都需要进行解析执行,只是有些是在解析的过程中直接执行 ...
- Android内存优化之OOM
内容大多都是和OOM有关的实践总结概要.理解错误或是偏差的地方,还请多包涵指正,谢谢!本人Q:1524447071 (一)Android的内存管理机制 Google在Android的官网上有这样一篇文 ...
- 转接口IC NCS8807:LVDS转MINI LVDS芯片
LVDS 4K TCON w/ Scaler1 General Description NCS8807 is an LVDS 4K TCON with advanced scaling func ...
- UIDatePicker的使用
UIDatePicker的介绍 UIDatePicker这个类的对象让用户可以在多个车轮上选择日期和时间.iPhone手机上的‘时钟’应用程序中的时间与闹铃中便使用了该控件.使用这个控件时,如果你能配 ...
- 第1课 - 学习Lua的意义
学习Lua的意义 1.Lua简介 (1) 1993年.巴西 (2) 小巧精致的脚本语言,大小只有200K (3) 用标准C语言写成,能够在所有的平台上编译运行 (4) 发明的目 ...