![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
贪心
谛听-
线上幽灵
展开
-
背包问题
#include "stdio.h"#include "stdlib.h"#include "string.h"#define MAX 20 typedef struct Item{ double w; //物品重量 double v; //物品价值 double vDw; //物品单位重量的价值 int index; //物品下标}Item;It原创 2015-10-14 17:06:31 · 327 阅读 · 0 评论 -
任务时间表问题
for(i=0; i<n; i++) { fin >> task[i].d >> task[i].w; cout << task[i].d << " " << task[i].w << endl; }原创 2015-11-25 11:25:34 · 1445 阅读 · 1 评论 -
区间相交问题
#include <iostream>#include <fstream>#include <algorithm>using namespace std;/*与活动安排问题类似,每次选取右端点坐标最小的闭区间,保留该闭区间,并将与其相交的闭区间删除*/原创 2015-11-25 10:24:41 · 472 阅读 · 0 评论 -
多元Huffman编码问题
#include <iostream>#include <fstream>#include <algorithm>#include <queue>using namespace std;/*每次选最小的k个元素进行合并。与2元Huffman算法类似*/原创 2015-11-25 09:46:35 · 5862 阅读 · 1 评论 -
多元Huffman编码变形
#include <iostream>#include <fstream>#include <algorithm>#include <queue>using namespace std;/*首先找到向量y,使y = max{ y<=m| y(2)*1 + y(3)*2 + ... + y(n)*(n-1) = n-1 }。如果找不到,则问题无解,否则用向量y作贪心计算贪心策略:每次选最原创 2015-11-25 14:40:44 · 764 阅读 · 0 评论 -
最优合并问题
#include "iostream"#include "fstream"//#include "vector"#include "queue"using namespace std;int a[50];//n为要合并的序列个数//返回最少合并次数int mergeMin(int n){ priority_queue< int, vector<int>, greater<int原创 2015-11-22 20:41:52 · 898 阅读 · 0 评论 -
单源最短路径--Dijkstra
#include "stdio.h"#define MAX 10#define INF 999typedef struct Mgraph{ int n; //图的顶点个数 int edge[MAX][MAX]; //图中边的权值}Mgraph;int dist[MAX]; //dist[vi]表示当前其找到的v0到每个终点vi的最短路径的长度int path[MAX]原创 2015-10-15 16:45:19 · 938 阅读 · 0 评论 -
非单位时间任务安排问题
#include <iostream>#include <fstream>#include <algorithm>using namespace std;/*首先将任务按其截止时间非减序排序p(i, d)表示对任务1,2..i,截止时间为d的最小误时惩罚p(i, d) = min{ p(i-1, d)+wi, p(i-1, min{d, di}-ti) }p(1, d) = 0 t原创 2015-11-24 21:14:30 · 4529 阅读 · 12 评论 -
最小生成树--prime
#include "stdio.h"#define MAX 10#define INF 999typedef struct Mgraph{ int n; //图的顶点个数 int edge[MAX][MAX]; //图中边的权值}Mgraph;int lowest[MAX]; //lowest[vi]表示当前生成树这一整体到其余顶点的权值int cloest[MAX]原创 2015-10-15 19:07:51 · 396 阅读 · 0 评论 -
最小生成树--kruskal
#include "stdio.h"#include "stdlib.h"#define MAX 10#define INF 999typedef struct Edge{ int u; //边一端的顶点 int v; //边另一端的顶点 int w; //边的权值}Edge;int root[MAX]; //定义并查集数组Edge edge[MAX];原创 2015-10-15 20:07:22 · 352 阅读 · 0 评论 -
哈弗曼编码
#include "iostream"#include "queue"#include "vector"#include "string"using namespace std;struct huffmanTree{ char data; //表示的字符 int weight; //字符出现的频率,权值 int parent; //父亲的下标 int原创 2015-10-15 14:12:23 · 395 阅读 · 0 评论 -
leetcode---Queue Reconstruction by Height
Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is the number of people in front of this p原创 2017-04-28 22:24:09 · 297 阅读 · 0 评论 -
leetcode---gas-station---贪心
There are N gas stations along a circular route, where the amount of gas at station i isgas[i]. You have a car with an unlimited gas tank and it costscost[i]of gas to travel from station i to its next原创 2017-09-25 19:27:35 · 272 阅读 · 0 评论 -
leetcode---maximum-subarray---贪心
Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array[−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray[4,−1,2,1]has the原创 2017-10-14 19:18:50 · 312 阅读 · 0 评论 -
leetcode---jump-game---贪心
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you原创 2017-10-14 20:01:16 · 262 阅读 · 1 评论 -
剑指offer---最小的K个数
输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。class Solution {public: vector<int> GetLeastNumbers_Solution(vector<int> input, int k) { vector<int>...原创 2018-03-06 20:11:36 · 142 阅读 · 0 评论 -
leetcode---jump-game-ii---贪心
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is ...原创 2018-03-02 20:30:33 · 157 阅读 · 0 评论 -
硬币找钱问题1
#include "iostream"#include "fstream"using namespace std;/*最大面值优先*/int coins[6];int greedy(double money){ cout << "找钱时用到的钱币有:"; int m[] = {200, 100, 50, 20, 10, 5}; //硬币面值,单位为分 money原创 2015-11-23 14:53:56 · 782 阅读 · 0 评论 -
磁带利用率问题
#include <iostream>#include <fstream>#include <algorithm>using namespace std;/*最短程序优先。求得最多可以存储的程序个数count后,再求最大利用率,问题转换为装载问题,但count已知。 */const int MAX = 50;int f[MAX];int r = 0; //剩余程序长度和int l原创 2015-11-24 15:25:59 · 853 阅读 · 1 评论 -
最优装载
#include "stdio.h"#include "stdlib.h"#include "string.h"#define MAX 20 typedef struct Item{ double w; //集装箱重量 int index; //集装箱下标}Item;Item item[MAX]; //集装箱double x[MAX]; //x[i]表示是否装入原创 2015-10-14 20:35:12 · 383 阅读 · 0 评论 -
多机调度问题
#include "stdio.h"#include "stdlib.h"#include "queue"#define MAX 10using namespace std;typedef struct Job{ int id; int time; //执行时间}Job;typedef struct Machine{ int id; int avai原创 2015-10-16 08:57:52 · 1025 阅读 · 0 评论 -
程序存储问题
#include "iostream"#include "fstream"#include "queue"using namespace std;/*最短程序优先 */double a[50];//返回最多能存储的程序个数double greedy(int n, int len){ sort(a, a+n); //按程序长度排序 int i = 0; int原创 2015-11-23 09:39:31 · 634 阅读 · 0 评论 -
最优服务次序问题
#include "iostream"#include "fstream"#include "queue"using namespace std;/*最短服务时间优先 */double t[50];//返回最小平均等待时间double greedy(int n){ sort(t, t+n); //按服务时间排序 int i; double sum = 0;原创 2015-11-23 09:49:17 · 619 阅读 · 0 评论 -
d森林问题
#include "iostream"#include "fstream"#include "queue"using namespace std;/*最短服务时间优先*/const int MAX = 50;int parent[MAX]; //存储父结点int leaf[MAX]; //存储叶子结点int deg[MAX]; //存储顶点的出度int parlen[MAX]原创 2015-11-23 12:10:05 · 5426 阅读 · 5 评论 -
区间覆盖问题
#include "iostream"#include "fstream"#include "queue"using namespace std;/*每次覆盖尽可能多的点*/int x[50];int greedy(int n, int k){ sort(x, x+n); //将各点坐标排序 int i = 1; int dist = 0; int d;原创 2015-11-23 14:01:52 · 501 阅读 · 0 评论 -
磁带最优存储问题
#include "iostream"#include "fstream"#include "queue"using namespace std;/*最短平均读取时间优先 */double p[50];int l[50];int y[50]; //存放每个程序的平均读取时间//返回n个程序的最小平均读取时间double greedy(int n){ int i;原创 2015-11-22 21:35:21 · 1378 阅读 · 0 评论 -
多处最优服务次序问题
#include "iostream"#include "fstream"#include "queue"using namespace std;/*最短服务时间优先*/double t[50];//返回最小平均等待时间double greedy(int n, int s){ sort(t, t+n); //按服务时间排序 double *sum = new doub原创 2015-11-23 10:09:03 · 1507 阅读 · 1 评论 -
汽车加油问题
#include "iostream"#include "fstream"#include "queue"using namespace std;/*最远加油站优先*/int s[50];int greedy(int n, int k){ int i; int sum = 0; int count = 0; for(i=0; i<=k; i++) {原创 2015-11-23 12:33:07 · 906 阅读 · 0 评论 -
会场安排问题
#include "iostream"#include "fstream"#include "algorithm"using namespace std;/*首先对n个活动的2n个端点排序,然后从左到右扫描,遇到开始时刻s[i],将活动i安排在一个空闲的会场中。遇到一个结束时刻f[i]就将活动i占用的会场释放。*/struct Point{ int time; //时间原创 2015-11-22 19:52:36 · 393 阅读 · 0 评论 -
磁盘文件最优存储问题
#include "iostream"#include "fstream"#include "queue"using namespace std;/*将n个文件按概率排序,排序后有p1<=p2<=p3...<=pnf(n-1)占中心磁道,f(n-2)和f(n-3)分居f(n-1)两侧,f(n-4)和f(n-5)分居最外两侧 */double p[50];//返回n个文件的最小期望检索时间原创 2015-11-22 22:19:45 · 1397 阅读 · 1 评论 -
删数问题
#include <iostream>#include <fstream>#include <string>using namespace std;//最近下降点优先//返回a删除k个数字后的最小数string delek(string a, int k){ int i; int len = a.length(); int count = 0; while原创 2015-11-23 20:53:04 · 395 阅读 · 0 评论 -
数列极差问题
#include <iostream>#include <fstream>#include <queue>using namespace std;//类似Huffmanint max(int *a, int n){ priority_queue<int> q; int i; for(i=0; i<n; i++) q.push(a[i]); i原创 2015-11-23 21:11:30 · 911 阅读 · 0 评论 -
套汇问题
#include <iostream>#include <fstream>#include <string>using namespace std;const int MAX = 10;string name[MAX];double r[MAX][MAX]; //r[i][j]表示货币i与货币j的兑换率double max(double a, double b){ return原创 2015-11-24 11:26:56 · 1615 阅读 · 0 评论 -
硬币找钱问题2
//参考 http://www.cppblog.com/cuijiaxing/archive/2012/04/15/171448.aspx #include <iostream> #include <fstream> using namespace std; ifstream ifs; int coins[6];//各面值硬币数 int count = 0;//需要的最少硬币数原创 2015-11-23 20:32:02 · 1104 阅读 · 2 评论 -
嵌套箱问题
//参考:http://wenku.baidu.com/link?url=pfD9FxgHfr7uyJcphmGhGDbgNjbEbBT51sfC8Y2sXckQE2j54I9xMGLGSro_c4fZ62xKjbifLH16DBmoQeXeBz_nW3jJC-gJxhL-rgdYy97#include <iostream>#include <fstream>#include <queue>u原创 2015-11-24 10:44:14 · 885 阅读 · 0 评论 -
leetcode---minimum-window-substring---贪心
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For example, S =”ADOBECODEBANC” T =”ABC” Minimum window is”BANC”. N...原创 2018-03-03 21:04:50 · 153 阅读 · 0 评论