greedy algorithm, insertion sort, quick sort
always makes the choice that seems to be the best at that moment.
Example #1:
@function: scheduling
// You are given an array A of integers, where each element indicates the time
// thing takes for completion. You want to calculate the maximum number of things
// that you can do in the limited time that you have.
//
// main.cpp
// greedy #include <iostream> using std::cout;
using std::cin;
using std::string; #define SIZEOF_ARRAY(a) (sizeof(a)/sizeof(a[0])) template<typename T>
void insertion_sort(T *a, size_t n) {
T tmp;
size_t j, p;
for (p = 1; p < n; p++) {
tmp = a[p];
for (j = p; 0 < j && tmp < a[j-1]; j--) {
a[j] = a[j-1];
}
a[j] = tmp;
}
} template<typename T>
void swap(T *a, T *b) {
T tmp = *a;
*a = *b;
*b = tmp;
} // Return median of "left", "center", and "right"
// Order these and hide the pivot
template<typename T>
T median3(T *a, size_t left, size_t right) {
size_t center = (left+right)/2;
if (a[left] > a[center])
swap(&a[left], &a[center]);
if (a[left] > a[right])
swap(&a[left], &a[right]);
if (a[center] > a[right])
swap(&a[center], &a[right]); /* Invariant: a[left]<=a[center]<=a[right] */
swap(&a[center], &a[right-1]); /* HIde pivot */
return a[right-1]; /* return pivot */
} #define cutoff (3)
template<typename T>
void quicksort(T *a, size_t left, size_t right) {
size_t i, j;
T pivot;
if (left + cutoff <= right) {
pivot = median3(a, left, right);
i = left;
j = right-1;
for (;;) {
while (a[++i] < pivot) {}
while (a[--j] > pivot) {}
if (i < j)
swap(&a[i], &a[j]);
else
break;
}
swap(&a[i], &a[right-1]); /* restore pivot */ quicksort(a, left, i-1);
quicksort(a, i+1, right);
} else {
insertion_sort(a+left, right-left+1);
}
} template<typename T>
void sort(T *a, size_t n) {
quicksort(a, 0, n-1);
} template<typename T>
void print_array(T *a, size_t n) {
size_t i;
cout << "[";
for (i = 0; i < n-1; i++) {
cout << a[i] << ",";
}
cout << a[i] << "]\n";
} int scheduling(int *a, size_t n, int t) {
int numberOfThings = 0, currentTime = 0; sort<int>(a, n);
print_array<int>(a, n); for (int i = 0; i < n; i++) {
currentTime += a[i];
if (currentTime > t) {
break;
}
numberOfThings++;
}
return numberOfThings;
} int main(int argc, const char * argv[]) { int a[] = {15, 17, 15, 18, 10};
int t = 40, n = SIZEOF_ARRAY(a); cout << "the Scheduling problem output: "<< scheduling(a, n, t) << "\n"; return 0;
}
output:
p.p1 { margin: 0; font: 11px Menlo }
span.s1 { font-variant-ligatures: no-common-ligatures }
the Scheduling problem output: [10,15,15,17,18]
3
Program ended with exit code: 0
https://www.hackerearth.com/zh/practice/algorithms/dynamic-programming/bit-masking/tutorial/
https://www.geeksforgeeks.org/greedy-algorithm-to-find-minimum-number-of-coins/
greedy algorithm, insertion sort, quick sort的更多相关文章
- C/C++ Quick Sort Algorithm
本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50255069 快速排序算法,由C.A. ...
- 1101. Quick Sort (25)
There is a classical process named partition in the famous quick sort algorithm. In this process we ...
- PAT (Advanced Level) 1098. Insertion or Heap Sort (25)
简单题.判断一下是插排还是堆排. #include<cstdio> #include<cstring> #include<cmath> #include<ve ...
- PAT1101:Quick Sort
1101. Quick Sort (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Peng There is a ...
- PAT A1098 Insertion or Heap Sort (25 分)——堆排序和插入排序,未完待续。。
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- A1101. Quick Sort
There is a classical process named partition in the famous quick sort algorithm. In this process we ...
- 1098 Insertion or Heap Sort
1098 Insertion or Heap Sort (25 分) According to Wikipedia: Insertion sort iterates, consuming one in ...
- 1101 Quick Sort
There is a classical process named partition in the famous quick sort algorithm. In this process we ...
- PAT甲1101 Quick Sort
1101 Quick Sort (25 分) There is a classical process named partition in the famous quick sort algorit ...
随机推荐
- Linux修改Ip简单知识了解
1. 在终端输入:vim /etc/sysconfig/network-scripts/ifcfg-etho(etho是指的安装centos的产生的网卡) 2.按i开始编辑,填写ip地址.子网掩码.网 ...
- luoguP2601 对称的正方形
题目描述 给出一个数字矩形,求这个矩形中有多少个子正方形满足上下对称.左右对称. 思路 我们可以用3个哈希数组 \(a\ b\ c\) 分别表示矩形从左上往右下看,从左下往右上看,从右上往左下看的样子 ...
- ESP32CAM 人脸识别追踪
引言 总体实现的流程:ESP32cam作为客户端,pc作为服务端通过mqtt协议建立通信,将采集的图像在电脑端显示人脸识别的方法使用的是opencv,并通过mqtt传输指令给esp32cam控制舵机云 ...
- ATM取款机优化需求的用例设计
案例设计需求 有一个ATM取款系统,现对于取款功能进行了如何需求变更:碑只能取面额是100元(如取500,输出5张100元),现在功能修改为,可以取面额是10元.50元和100元的,其余功能不变,用户 ...
- 什么?都1202年了还不懂k8s和容器的关系?!这份k8s指南快拿走不谢!
都1202年了,还是有许多人搞不清容器与k8s之间的关系.在开始本篇正文之前,我们先来捋一捋这对"CP"的关系. 你可能已经很熟悉虚拟机了,最常见的莫过于我们拿到macOS却需要用 ...
- uwp 中的动画
xml --------------------------------------- <Page x:Class="MyApp.MainPage" xmlns=" ...
- JSP编码问题
JSP的开头内容: 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 ...
- 关于Ubuntu18.04上Python版本管理
时间: 2019-11-11 整理: pangyuaner 标题:树梅派上多版本python及pip安装使用管理指南 地址:https://blog.csdn.net/zbgjhy88/article ...
- MySQL锁(表锁,行锁,共享锁,排它锁,间隙锁)使用详解
锁,在现实生活中是为我们想要隐藏于外界所使用的一种工具.在计算机中,是协调多个进程或县城并发访问某一资源的一种机制.在数据库当中,除了传统的计算资源(CPU.RAM.I/O等等)的争用之外,数据也是一 ...
- Android常见面试题(一)
ANDROID(一) Activity 1.什么是Activity? 请描述一下生命周期 Activity: 一个Activity是一个应用程序组件,提供一个屏幕,用户可以用来交互为了完成某项任务,例 ...