贪心法进阶

贪心算法

Huffman编码

模拟退火

贪心算法是指,在对问题求解时,总是以当前情况为基础作最优选择,而不考虑各种可能的整体情况,它所做出的仅仅是在某种意义上的局部最优解,省去了为找最优解要穷尽所有可能而必须耗费的大量时间,类似数学归纳法,无后效性,在运行过程中没有回溯过程,每一步都是当前的最佳选择。
难点是如何贪心和证明贪心的正确性,即如何用一个小规模的解构造更大规模的解,比赛过程中需要胆大心细地归纳、分析。

  1. 贪心的缺点:
    1) 可能得不到最优解
    2) 可能算不出答案
  2. 用贪心法求解需要满足以下特征:
    1) 最优子结构性质(当一个问题的最优解包含其子问题的最优解时,称此问题具有最优子结构性质)
    2) 贪心选择性质
  3. 贪心常见问题
    1) 活动安排问题(区间调度问题)
    2) 区间覆盖问题
    3) 最优装载问题
    4) 多机调度问题
    就活动安排问题来讲:
    【今年暑假不AC】 题目大意:节目总数n,接着输入每个节目的开始时间和结束时间,输出能看的尽量多的节目数目。
  4. 选择贪心策略
  5. 罗列算法步骤
  6. 判断是否能得到全局最优解
  7. 实现代码
【核心代码】
struct  s{
   
	int b,e;
}z[100];
bool cmp(s a, s b){
   
	return a.e<b.e;
}
(输入省略)
sort(z,z+n,cmp)
int time=0,count=0;
for(int i=0;i<n;i++){
   
	if(z[i].b>=time){
   
		time=z[i].e;[添加链接描述](https://www.icourse163.org/course/PKU-1001894005?tid=1463180448)
		count++;
	}
}
cout<<count<<endl;

知乎贪心算法:
https://zhuanlan.zhihu.com/p/340804779
中国大学MOOC十一周贪心算法:
https://www.icourse163.org/course/PKU-1001894005?tid=1463180448

贪心相关题目:

A - Intervals POJ - 1089
Description

There is given the series of n closed intervals [ai; bi], where i=1,2,…,n. The sum of those intervals may be represented as a sum of closed pairwise non−intersecting intervals. The task is to find such representation with the minimal number of intervals. The intervals of this representation should be written in the output file in acceding order. We say that the intervals [a; b] and [c; d] are in ascending order if, and only if a <= b < c <= d.
Task
Write a program which:
reads from the std input the description of the series of intervals,
computes pairwise non−intersecting intervals satisfying the conditions given above,
writes the computed intervals in ascending order into std output
Input

In the first line of input there is one integer n, 3 <= n <= 50000. This is the number of intervals. In the (i+1)−st line, 1 <= i <= n, there is a description of the interval [ai; bi] in the form of two integers ai and bi separated by a single space, which are respectively the beginning and the end of the interval,1 <= ai <= bi <= 1000000.
Output

The output should contain descriptions of all computed pairwise non−intersecting intervals. In each line should be written a description of one interval. It should be composed of two integers, separated by a single space, the beginning and the end of the interval respectively. The intervals should be written into the output in ascending order.
Sample Input

5
5 6
1 4
10 10
6 9
8 10
Sample Output

1 4
5 10

//题解一:
#include <iostream>
#include <algorithm>
using namespace std;
struct xz {
   
	int x;
	int z;
} a[100005],b[100005];
bool cmp(xz a,xz b) {
   
	return a.x<b.x;
}
int main() {
   
	int n;
	cin>>n;
	for(int i=0; i<n; i++) {
   
		cin>>a[i].x>>a[i].z;
	}
	sort(a,a+n,cmp);
	int k=0;
	b[k].x=a[0].x;
	b[k].z=a[0].z;
	for(int i=1; i<n; i++) {
   
		if(a[i].x<=b[k].z) {
   
			if(b[k].z<a[i].z)
				b[k].z=a[i].z;
		} else {
   
			k++;
			b[k].x=a[i].x;
			b[k].z=a[i].z;
		}
	}
	for(int i=0; i<=k; i++)
		cout<<b[i].x<<" "<<b[i].z<<endl;
	return 0;
}

//题解二
#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
   
	int b;
	int e;
}a[50005];	
bool cmp(node x,node y)
{
   
	return x.b<y.b;
}
int main()
{
   
	int n;
	cin>>n;
	for(int i=0;i<n;i++)
	{
   
		cin>>a[i].b>>a[i].e;
	}
	sort(a,a+n,cmp)
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值