《数据结构与算法分析——c语言描述》 练习6.34 a b c 答案
毫无算法可言。不知道怎样用堆。。。最近事情真多,原来我想要的只是安安静静地写代码。
用了堆了。就是把盒子的容量放入堆中。
a
#include<queue> #include<iostream> using namespace std; #define MAXN 100 int w[MAXN]; int c; int box[MAXN]; int main() { memset(box, 0, sizeof(box)); scanf("%d", &c); int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &w[i]); int boxCnt = 0; for (int i = 0; i < n; i++) { int j = 0; while ((c - box[j]) < w[i]) j++; if (j > boxCnt) boxCnt = j; box[j] += w[i]; } printf("%d", boxCnt+1); }
b 堆版的,通过大堆来
leftheap.h
#ifndef _LeftHeap_H #define _LeftHeap_H typedef int ElementType; struct TreeNode; typedef struct TreeNode *PriorityQueue; PriorityQueue initialize(void); ElementType findMax(PriorityQueue h); int isEmpty(PriorityQueue h); PriorityQueue merge(PriorityQueue h1, PriorityQueue h2); #define insert(X,H) (H=insert1((X),H)) PriorityQueue insert1(ElementType X, PriorityQueue h); PriorityQueue deleteMax1(PriorityQueue h); #define deleteMax(H) (H=deleteMax1(H)) #endif // !_BinHeap_H
leftheap.c
#include"leftheap.h" #include"fatal.h" struct TreeNode { ElementType element; PriorityQueue left; PriorityQueue right; int np1; }; PriorityQueue initialize(void) { return NULL; } ElementType findMax(PriorityQueue h) { if (isEmpty(h)) Error("EMPTY HEAP"); return h->element; } int isEmpty(PriorityQueue h) { return h == NULL; } static PriorityQueue merge1(PriorityQueue h1, PriorityQueue h2) { if (h1->left == NULL) { h1->left = h2; return h1; } else { h1->right = merge(h1->right, h2); if (h1->right->np1 > h1->left->np1) { PriorityQueue temp; temp = h1->right; h1->right = h1->left; h1->left = temp; } h1->np1 = h1->right->np1 + 1; return h1; } } PriorityQueue merge(PriorityQueue h1, PriorityQueue h2) { if (h1 == NULL) return h2; else if (h2 == NULL) return h1; else { if (h1->element > h2->element) return merge1(h1, h2); else return merge1(h2, h1); } } PriorityQueue insert1(ElementType X, PriorityQueue h) { PriorityQueue newNode = malloc(sizeof(struct TreeNode)); newNode->element = X; newNode->left = NULL; newNode->right = NULL; newNode->np1 = 0; return merge(newNode, h); } PriorityQueue deleteMax1(PriorityQueue h) { PriorityQueue temp1 = h->left; PriorityQueue temp2 = h->right; free(h); return merge(temp1, temp2); }
main.c
#include<stdio.h> #include"leftheap.h" #define MAXN 100 int w[MAXN]; int c; int main() { scanf("%d", &c); int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &w[i]); PriorityQueue h = initialize(); int boxCnt = 0; for (int i = 0; i < n; i++) { int flag = 0; if (!isEmpty(h)) { int max = findMax(h);//最大盒子的容纳量 if (max >= w[i]) { deleteMax(h); insert(max - w[i], h); flag = 1; } } if (!flag) { insert(c-w[i], h); boxCnt++; } } printf("%d", boxCnt ); }
b 数组版的
#include<queue> #include<iostream> using namespace std; #define MAXN 100 int w[MAXN]; int c; int box[MAXN]; int main() { memset(box, 0, sizeof(box)); scanf("%d", &c); int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &w[i]); int boxCnt = 0; for (int i = 0; i < n; i++) { int j = 0; while (c - box[j] < w[i]) j++; int maxPos = j; int max = c - box[j]; for (int i = j+1; i <= boxCnt; i++) { if (c - box[i] > max) { maxPos = i; max = c - box[i]; } } if (j > boxCnt) boxCnt = j; box[j] += w[i]; } printf("%d", boxCnt+1); }
c 堆版的
用的堆是小堆。
main.cpp
#include<stdio.h> #include"leftheap.h" #define MAXN 100 int w[MAXN]; int c; int main() { scanf("%d", &c); int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &w[i]); PriorityQueue h = initialize(); PriorityQueue h_temp = initialize(); int boxCnt = 0; for (int i = 0; i < n; i++) { int flag = 0; if (!isEmpty(h)) { int min = findMin(h);//最大盒子的容纳量 deleteMin(h); while (!isEmpty(h)&& min < w[i]) { insert(min, h_temp); min = findMin(h); deleteMin(h); } if (min >= w[i]) { insert(min - w[i], h); flag = 1; } h=merge(h, h_temp); h_temp = NULL; } if (!flag) { insert(c-w[i], h); boxCnt++; } } printf("%d", boxCnt ); }
c 数组版的
#include<queue> #include<iostream> using namespace std; #define MAXN 100 int w[MAXN]; int c; int box[MAXN]; int main() { memset(box, 0, sizeof(box)); scanf("%d", &c); int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &w[i]); int boxCnt = 0; for (int i = 0; i < n; i++) { int j = 0; while (c - box[j] < w[i]) j++; int minPos = j; int min = c - box[j]; for (int i = j+1; i <= boxCnt; i++) { if (c - box[i]>=w[i] && c - box[i] < min) { minPos = i; min = c - box[i]; } } if (j > boxCnt) boxCnt = j; box[j] += w[i]; } printf("%d", boxCnt+1); }