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 ...
随机推荐
- STM32—DMA存储器到外设
DMA目录 DMA简介 DMA框图 DMA传输数据分析 1.传输的方向 2.传输的数量 3.传输的模式 代码部分 DMA初始化结构体 USART配置函数 DMA配置函数 主函数 DMA简介 DMA(D ...
- Java中使用split方法根据英文问号?切割字符串时报错
因为正则表达式的原因,我们无法在java中直接使用String.split("?"),需要先转义其正确写法为: public static void splitStr() { St ...
- SpringBoot JPA查询映射到自定义实体类
和 SegmentFault上的文章(https://segmentfault.com/a/1190000021869465)一样, 都是俺账号 场景 举一个简单的栗子: 比如有一个User实体类 @ ...
- noip 模拟 7
我花了我多久的rp啊-- 考试经过 这次是三道题,依旧先看一遍,然后从头开始做 T1一看,这好像是KMP?等等,我好像忘了啊你个废,没事哈希也能做,On似乎可以呀,一波操作,我是不是要A题了? 转到T ...
- 题解 [AHOI2017/HNOI2017]大佬
传送门 注意到题面里n很小,有\(n\leq100\) 考虑联系n的实际意义 n是你在大佬手中能活的天数 题面颇富深意 好了不闹了 n很小,对于\(40\%\)的数据,爆搜即可 考场上靠这个骗了40p ...
- exportfs命令 – 管理NFS服务器共享的文件系统
exportfs命令需要参考配置文件"/etc/exportfs".也可以直接在命令行中指定要共享的NFS文件系统. 语法格式: export [参数] [目录] 常用参数: -a ...
- 简单介绍无限轮播图,js源代码
无限轮播图js源代码,今天介绍一下用js简单的编写无限轮播图 <!DOCTYPE html> <html> <head> <meta charse ...
- asp.net core 中的路由
- JMeter结果树响应数据中文乱码
打开apache-jmeter-2.11\bin\jmeter.properties文件,搜索"encoding"关键字,找到如下配置: # The encoding to be ...
- 刷题-力扣-213. 打家劫舍 II
213. 打家劫舍 II 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/house-robber-ii/ 著作权归领扣网络所有.商业 ...