Stanford Algorithms: Design and Analysis, Part 1[week 6]
来源:互联网 发布:淘宝上下架规则 编辑:程序博客网 时间:2024/06/05 06:54
Problem Set-6
Programming Question-6
Question 1
The goal of this problem is to implement a variant of the 2-SUM algorithm (covered in the Week 6 lecture on hash table applications).
The file contains 1 million integers, both positive and negative (there might be some repetitions!).This is your array of integers, with the
Your task is to compute the number of target values
Write your numeric answer (an integer between 0 and 20001) in the space provided.
OPTIONAL CHALLENGE: If this problem is too easy for you, try implementing your own hash table for it. For example, you could compare performance under the chaining and open addressing approaches to resolving collisions.
#include <iostream>#include <fstream>#define MIN 10000using namespace std;int hash2[MIN + 1] = { 0 };int count = 0;void readData() {ifstream fin("2sum.txt");int temp = 0;while(fin>>temp) {if(temp < MIN) hash2[temp]++;}}bool hashMap(int n) {if(n > MIN) return false;if(hash2[n]) return true;else return false;}int main() {readData();for(int i = -10000; i <= 10000; i++) {for(int j = -10000; j <= 10000; j++) {if(hashMap(j) && hashMap(i - j)) {count++;break;}}}cout<<count<<endl;return 0;}
Question 2
The goal of this problem is to implement the "Median Maintenance" algorithm (covered in the Week 5 lecture on heap applications). The text file contains a list of the integers from 1 to 10000 in unsorted order; you should treat this as a stream of numbers, arriving one by one. Letting
In the box below you should type the sum of these 10000 medians, modulo 10000 (i.e., only the last 4 digits). That is, you should compute
OPTIONAL EXERCISE: Compare the performance achieved by heap-based and search-tree-based implementations of the algorithm.
#ifndef _HEAP_H_#define _HEAP_H_// MAX_HEAPvoid MAX_HEAPIFY(short *A,short i) { short left=2*i,right=2*i+1,larger,tmp; if ((left<=A[0])&&(A[left]>A[i])) larger = left; else larger = i; if ((right<=A[0])&&(A[right]>A[larger])) larger = right; if (larger != i) { tmp = A[larger]; A[larger] = A[i]; A[i] = tmp; MAX_HEAPIFY(A,larger); }}short EXTRACT_MAX(short *A) { short max = A[1]; A[1] = A[A[0]--]; MAX_HEAPIFY(A,1); return max;}void HEAP_INCREASE_KEY(short *A,short i,short key) { short tmp; A[i] = key; while ((i>1)&&(A[i/2]<A[i])) { tmp = A[i]; A[i] = A[i/2]; A[i/2] = tmp; i = i/2; }}void MAX_HEAP_INSERT(short *A,short key) { A[0]++; HEAP_INCREASE_KEY(A,A[0],key);}// MIN_HEAPvoid MIN_HEAPIFY(short *A,short i) { short left=2*i,right=2*i+1,smaller,tmp; if ((left<=A[0])&&(A[left]<A[i])) smaller = left; else smaller = i; if ((right<=A[0])&&(A[right]<A[smaller])) smaller = right; if (smaller != i) { tmp = A[smaller]; A[smaller] = A[i]; A[i] = tmp; MIN_HEAPIFY(A,smaller); }}short EXTRACT_MIN(short *A) { short min = A[1]; A[1] = A[A[0]--]; MIN_HEAPIFY(A,1); return min;}void HEAP_DECREASE_KEY(short *A,short i,short key) { short tmp; A[i] = key; while ((i>1)&&(A[i/2]>A[i])) { tmp = A[i]; A[i] = A[i/2]; A[i/2] = tmp; i = i/2; }}void MIN_HEAP_INSERT(short *A,short key) { A[0]++; HEAP_DECREASE_KEY(A,A[0],key);}#endif
#include<stdio.h>#include<stdlib.h>#include<ctype.h>#include "heap.h"const char INFILE[] = "Median.txt";#define MAX 10000short A1[10001],A2[10001];short RETURN_MEDIAN(short *A1,short *A2,short x) { if (x<A2[1]) MAX_HEAP_INSERT(A1,x); else MIN_HEAP_INSERT(A2,x); if (A1[0]-A2[0]>1) MIN_HEAP_INSERT(A2,EXTRACT_MAX(A1)); if (A2[0]>A1[0]) MAX_HEAP_INSERT(A1,EXTRACT_MIN(A2)); return A1[1];}void main() { int m=0; A1[0] = 1; A1[1] = 0; A2[0] = 1; A2[1] = 20000; char line[10]; FILE *fp = fopen(INFILE,"r"); while (fgets(line,10,fp)) m = (m + RETURN_MEDIAN(A1,A2,atoi(line))) % 10000; printf("%d\n",m);}
- Stanford Algorithms: Design and Analysis, Part 1[week 6]
- Stanford Algorithms: Design and Analysis, Part 2[week 6]
- Stanford Algorithms: Design and Analysis, Part 1[week 1]
- Stanford Algorithms: Design and Analysis, Part 1[week 2]
- Stanford Algorithms: Design and Analysis, Part 1[week 3]
- Stanford Algorithms: Design and Analysis, Part 1[week 4]
- Stanford Algorithms: Design and Analysis, Part 1[week 5]
- Stanford Algorithms: Design and Analysis, Part 2[week 1]
- Stanford: Algorithms: Design and Analysis, Part 1 [Week 2]
- Stanford - Algorithms: Design and Analysis, Part 1 - Week 6 Assignment: hash table and heap
- Stanford Algorithms: Design and Analysis, Part 2[week 2]
- Stanford Algorithms: Design and Analysis, Part 2[week 3]
- Stanford Algorithms: Design and Analysis, Part 2[week 4]
- Stanford Algorithms: Design and Analysis, Part 2[week 5]
- Stanford - Algorithms: Design and Analysis, Part 2 - Week 1 Assignment: Greedy and Prim
- Stanford - Algorithms: Design and Analysis, Part 1 - Week 1 Assignment: number of inversions
- Stanford - Algorithms: Design and Analysis, Part 1 - Week 2 Assignment: QuickSort
- Stanford - Algorithms: Design and Analysis, Part 1 - Week 3 Assignment: Contraction
- HDU1210---Eddy's 洗牌问题 HDU(89)
- sqlplus常用命令
- 两天没更新了
- poj 1062"错误“代码
- [Oracle] SQL*Loader 详细使用教程(3)- 控制文件
- Stanford Algorithms: Design and Analysis, Part 1[week 6]
- nodejs的总结
- -------
- SQL Server 2008阻止保存要求重新创建表的更改的解决
- 查找峰值点,求相关
- hdu 4578——Transformation
- (Ext基础篇) 表单与输入控件
- Quick Change hdu
- 在Xlib中,从XImage中取出R,G,B三分量