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 ...
随机推荐
- 题解 u
传送门 这里AC解法因为手残 tag2[min(r+l, n+1)][min(c+l+1, n+1)]+=s; 写成 tag2[min(r+l, n+1)][c+l+1]+=s; 惨遭RE,以后注意查 ...
- noip模拟14
T1 离散化后线段树维护\(dp\),\(fi\)表示最小值为\(i\)时最多点亮多少个, 区间操作即可. Code #include<cstring> #include<cstdi ...
- ANSI C说明了三个用于存储空间动态分配的函数
1.1 malloc的全称是memory allocation,中文叫动态内存分配.原型:extern void *malloc(unsigned int num_bytes);说明:分配长度为num ...
- C++ leetcode接雨水
双指针算法"接雨水" 链接:https://leetcode-cn.com/problems/trapping-rain-water/ 给定 n 个非负整数表示每个宽度为 1 的柱 ...
- spring知识点(面试题)
转自(参考):https://baijiahao.baidu.com/s?id=1595722523154435312&wfr=spider&for=pc 本人收集了一些在大家在面试时 ...
- 使用dom4j工具:读取xml标签(二)
package dom4j_read; import java.io.File; import java.util.List; import org.dom4j.Document; import or ...
- Java基础和常用框架的面试题
前言 最近学校也催着找工作了,于是刷了一些面试题,学习了几篇大佬优秀的博客,总结了一些自认为重要的知识点:听不少职场前辈说,对于应届毕业生,面试时只要能说到核心重要的点,围绕这个点说一些自己的看法,面 ...
- win10 uwp 通过 Win2d 完全控制笔迹绘制逻辑
本文来告诉大家如何通过 Win2d 完全控制笔迹绘制逻辑,本文适合用来实现复杂的自定义逻辑,可以完全控制笔迹的行为.包括在书写过程中切换模式,如进行手势擦除切换为橡皮擦模式 本文提供的方法适合用来做复 ...
- Tolist案例(父子传参实现增删改)
1.Tolist案例(父子传参实现增删改) 目录结构 实现效果: App.jsx class App extends Component { // 状态在哪里, 操作状态的方法就在哪里 state = ...
- AWS EC2 实例 SSH 无法登录故障
文章链接 故障表现 在使用 jumperver 登录 AWS ec2 实例的时候发现 ssh 配合秘钥登录的时候无法登录, 具体报错如下: ssh -i /path/xx.pem user@10.0. ...