贪心(2)选取问题

目录

一,选取问题

二,集合的选取问题

CSU 1012 Prestige

CSU 1043 克格莫

力扣 948. 令牌放置

​力扣 826. 安排工作以达到最大收益

力扣 2708. 一个小组的最大实力值

力扣 1561. 你可以获得的最大硬币数目

三,数组的选取问题

力扣 55. 跳跃游戏

力扣 45. 跳跃游戏 II

力扣 122. 买卖股票的最佳时机 II

力扣 376. 摆动序列

四,双端队列的选取问题

OpenJ_Bailian 3377 Best Cow Line, Gold

五,循环数组的选取问题

力扣 134. 加油站


一,选取问题

根据对象空间的结构,把贪心选取问题分为集合、数组、双端、循环数组等选取问题

二,集合的选取问题

CSU 1012 Prestige

题目:

Description

Are you watching closely?

Every great magic trick consists of three acts. The first act is called The Pledge, the magician shows you something ordinary, but of course, it probably isn't. The second act is called The Turn. The magician makes his ordinary something do something extraordinary. Now, if you're looking for the secret,you won't find it. That's why there's a third act, called The Prestige. This is the part with the twists and turns, where lives hang in the balance, and you see something shocking you've never seen before.

Li Lei and Han Meimei are magicians and they want to act the prestige. So they need to divide the props for the magic show. They have decided upon the following procedure: they choose props one by one, in turn, until all the props are chosen.

Li Lei and Han Meimei have different strategies in deciding what to choose. When faced with a choice, Li Lei always selects the prop that is most valuable to him. In case of a tie, he is very considerate and picks the one that is least valuable to Han Meimei. (Since Li Lei and Han Meimei are good friends, they know exactly how much value the other places on each prop.)

Han Meimei’s strategy, however, consists of maximizing her own final value(The total value of all the props she picked). She is also very considerate, so if multiple choices lead to the same optimal result, she prefers Li Lei to have as much final value as possible.

You are given the result that who chooses first. After Li Lei and Han Meimei have finished dividing all the props between themselves, what is the total value of the props each of them ends up with?

Input

On the first line a positive integer: the number of test cases, at most 100. After that per test case:

  • One line with an integer n(1≤n≤1,000): the number of props.

  • One line with a letter, either “L” for Li Lei or “H” for Han Meimei: the person that chooses first.

  • n lines with two integers li and hi (0≤li, hi≤1000) each: The values that Li Lei and Han Meimei assign to the i-th prop, respectively.

Output

For each test case, output one line with two integers: the value Li Lei gets and the value Han Meimei gets. Both values must be according to their own valuations.

Sample Input

3
4
L
100 80
70 80
50 80
30 50
4
L
10 1
1 10
6 6
4 4
7
H
4 1
3 1
2 1
1 1
1 2
1 3
1 4

Sample Output

170 130
14 16
9 10

题意:

有n个物品,L和H两人轮流来取

每个物品对L的价值为v1,对H的价值为v2

输入谁先取,和每个物品的价值v1v2,输出最后的结果

L的策略是每次都取v1最大的物品,如果有多个,就取其中v2最小的那个

H的策略是尽量使得最后所得总价值最大,如果有多个选择方案,就选择让L取到价值最大的那个方案

思路:

贪心,先把所有物品按照L的策略进行排序

然后H每次都取v2最大的物品,但是要满足限制,就是前面若干个物品中H最多只能取一半,因为L是一直按照这个顺序往后取的

只要满足这个限制,H就能得到最优解

代码:

#include<iostream>
#include<algorithm>
using namespace std;
 
int n;
char c;
bool visit[1001], ans[1001];
 
struct node
{
	int v1, v2;
}nod[1001];
 
bool cmp(node a, node b)
{
	if (a.v1 == b.v1)return a.v2 < b.v2;
	return a.v1 > b.v1;
}
 
int getmax()
{
	int max = 0, key = 0;
	for (int i = 1; i <= n; i++)
		if (!visit[i] && max <= nod[i].v2)max = nod[i].v2, key = i;
	return key;
}
 
bool ok(int k)
{
	int s = 0;
	for (int i = 1; i <= n; i++)
	{
		if (i == k || ans[i])s++;
		if (s > (i + (c == 'H'))/2)return false;
	}
	return true;
}
 
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		cin >> n >> c;
		for (int i = 1; i <= n; i++)
		{
			cin >> nod[i].v1 >> nod[i].v2;
			visit[i] = false, ans[i] = false;
		}
		sort(nod + 1, nod + n + 1, cmp);
		int num = (n + (c == 'H')) / 2;
		visit[1] = (c == 'L');
		while (num)
		{
			int k = getmax();
			if (ok(k))ans[k] = true, visit[k] = true, num--;
			else for (int i = 1; i <= k; i++)visit[k] = true;
		}
		int s1 = 0, s2 = 0;
		for (int i = 1; i <= n; i++)
			if (ans[i])s2 += nod[i].v2; else s1 += nod[i].v1;
		cout << s1 << " " << s2 << endl;
	}
	return 0;
}

CSU 1043 克格莫

Description

最近流行一款网游英雄联盟,类似dota的5V5对战游戏,其中一个远程攻击英雄深渊巨口·克格莫,非常强力,团战的时候站着喷,就可能拯救世界……

我们现在计算一个简单的数据,假设克格莫倒霉的遇到了对面五个人偷袭,克格莫在挂掉之前可以对敌人进行若干次攻击,友军即将包抄过来,所以克格莫只想造成尽可能高的伤害,并不追求杀掉哪一个。那么,它最高能制造多高的输出?

Input

每组数据第一行为正整数n(0 < n < 20),表示克格莫挂掉之前可以进行的攻击次数,接下来5行,代表对敌方五个人能造成的伤害。每行两个数,第一个数是敌人的生命值,第二个数是一次攻击能产生的伤害。如果一次伤害之后敌人的生命值小于0,本次伤害值依然按能够制造的伤害值计算,而下次不能再攻击这个挂掉的敌人。敌人生命值不超过5500,攻击能产生的伤害不超过550。

Output

每组数据输出一行,克格莫可以打出的最高伤害。

Sample Input

10
1000 300
5000 200
4000 500
3000 100
2000 550

Sample Output

5200

代码:

#include<iostream>
#include<algorithm>
using namespace std;
 
struct node
{
	int sm, sh;
};
 
bool cmp(node a, node b)
{
	return a.sh > b.sh;
}
 
int main()
{
	int n;
	node nod[5];
	while (cin>>n)
	{
		for (int i = 0; i < 5; i++)cin >> nod[i].sm >> nod[i].sh;
		sort(nod, nod + 5, cmp);
		int ans = 0, t;
		for (int i = 0; n; i++)
		{
			t = (nod[i].sm - 1) / nod[i].sh + 1;
			if (t > n)t = n;
			n -= t, ans += t * nod[i].sh;
		}
		cout << ans << endl;
	}
	return 0;
}

力扣 948. 令牌放置

题目:

你的初始能量为 P,初始分数为 0,只有一包令牌。

令牌的值为 token[i],每个令牌最多只能使用一次,可能的两种使用方法如下:

如果你至少有 token[i] 点能量,可以将令牌置为正面朝上,失去 token[i] 点能量,并得到 1 分。
如果我们至少有 1 分,可以将令牌置为反面朝上,获得 token[i] 点能量,并失去 1 分。
在使用任意数量的令牌后,返回我们可以得到的最大分数。

示例 1:

输入:tokens = [100], P = 50
输出:0
示例 2:

输入:tokens = [100,200], P = 150
输出:1
示例 3:

输入:tokens = [100,200,300,400], P = 200
输出:2
 

提示:

tokens.length <= 1000
0 <= tokens[i] < 10000
0 <= P < 10000

思路:

排序,贪心。注意tokens为空的情况

代码:

class Solution {
public:
    int bagOfTokensScore(vector<int>& tokens, int P) 
    {
        if(tokens.empty())return 0;
        int ans=0;
        sort(tokens.begin(),tokens.end());
        auto it1=tokens.begin();
        auto it2=tokens.end()-1;
        while(it1<=it2){
            if(*it1<=P){
                P-=*it1;
                ans++,it1++;
            }
            else if(ans){
                P-=*it1-*it2;
                it1++,it2--;
            }
            else{
                break;
            }
        }
        return ans;
    }
};

​力扣 826. 安排工作以达到最大收益

你有 n 个工作和 m 个工人。给定三个数组: difficultyprofit 和 worker ,其中:

  • difficulty[i] 表示第 i 个工作的难度,profit[i] 表示第 i 个工作的收益。
  • worker[i] 是第 i 个工人的能力,即该工人只能完成难度小于等于 worker[i] 的工作。

每个工人 最多 只能安排 一个 工作,但是一个工作可以 完成多次 。

  • 举个例子,如果 3 个工人都尝试完成一份报酬为 $1 的同样工作,那么总收益为 $3 。如果一个工人不能完成任何工作,他的收益为 $0 。

返回 在把工人分配到工作岗位后,我们所能获得的最大利润 

示例 1:

输入: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7]
输出: 100 
解释: 工人被分配的工作难度是 [4,4,6,6] ,分别获得 [20,20,30,30] 的收益。

示例 2:

输入: difficulty = [85,47,57], profit = [24,66,99], worker = [40,25,25]
输出: 0

提示:

  • n == difficulty.length
  • n == profit.length
  • m == worker.length
  • 1 <= n, m <= 104
  • 1 <= difficulty[i], profit[i], worker[i] <= 105
class Solution {
public:
    int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {
		SortExtend(difficulty, profit);
		vector<int> difficulty2, profit2;
		difficulty2.push_back(difficulty[0]);
		profit2.push_back(profit[0]);
		for (int i = 1; i < difficulty.size(); i++) {
			if (profit[i] > profit2[profit2.size() - 1]) {
				difficulty2.push_back(difficulty[i]);
				profit2.push_back(profit[i]);
			}
		}
		int ans = 0;
		for (auto x : worker) {
			int id = upper_bound(difficulty2.begin(), difficulty2.end(), x) - difficulty2.begin();
			ans += id?profit2[id - 1]:0;
		}
		return ans;
    }
};

力扣 2708. 一个小组的最大实力值

给你一个下标从 0 开始的整数数组 nums ,它表示一个班级中所有学生在一次考试中的成绩。老师想选出一部分同学组成一个 非空 小组,且这个小组的 实力值 最大,如果这个小组里的学生下标为 i0i1i2, ... , ik ,那么这个小组的实力值定义为 nums[i0] * nums[i1] * nums[i2] * ... * nums[ik​] 。

请你返回老师创建的小组能得到的最大实力值为多少。

示例 1:

输入:nums = [3,-1,-5,2,5,-9]
输出:1350
解释:一种构成最大实力值小组的方案是选择下标为 [0,2,3,4,5] 的学生。实力值为 3 * (-5) * 2 * 5 * (-9) = 1350 ,这是可以得到的最大实力值。

示例 2:

输入:nums = [-4,-5,-4]
输出:20
解释:选择下标为 [0, 1] 的学生。得到的实力值为 20 。我们没法得到更大的实力值。

提示:

  • 1 <= nums.length <= 13
  • -9 <= nums[i] <= 9
class Solution {
public:
	long long maxStrength(vector<int>& nums) {
		if (nums.size() == 1)return nums[0];
		int s = 0, m = -12345, num0 = 0;
		for (auto x : nums) {
			if (x < 0)s++, m = max(m, x);
			if (x == 0)num0++;
		}
		if (num0 == nums.size())return 0;
		if (num0 == nums.size()-1 && m!=-12345)return 0;
		s %= 2;
		long long ans = 1;
		for (auto x : nums) {
			if (x == m && s)s--, x = 1;
			if (x)ans *= x;
		}
		return ans;
	}
};

力扣 1561. 你可以获得的最大硬币数目

有 3n 堆数目不一的硬币,你和你的朋友们打算按以下方式分硬币:

  • 每一轮中,你将会选出 任意 3 堆硬币(不一定连续)。
  • Alice 将会取走硬币数量最多的那一堆。
  • 你将会取走硬币数量第二多的那一堆。
  • Bob 将会取走最后一堆。
  • 重复这个过程,直到没有更多硬币。

给你一个整数数组 piles ,其中 piles[i] 是第 i 堆中硬币的数目。

返回你可以获得的最大硬币数目。

示例 1:

输入:piles = [2,4,1,2,7,8]
输出:9
解释:选出 (2, 7, 8) ,Alice 取走 8 枚硬币的那堆,你取走 7 枚硬币的那堆,Bob 取走最后一堆。
选出 (1, 2, 4) , Alice 取走 4 枚硬币的那堆,你取走 2 枚硬币的那堆,Bob 取走最后一堆。
你可以获得的最大硬币数目:7 + 2 = 9.
考虑另外一种情况,如果选出的是 (1, 2, 8) 和 (2, 4, 7) ,你就只能得到 2 + 4 = 6 枚硬币,这不是最优解。

示例 2:

输入:piles = [2,4,5]
输出:4

示例 3:

输入:piles = [9,8,7,6,5,1,2,3,4]
输出:18

提示:

  • 3 <= piles.length <= 10^5
  • piles.length % 3 == 0
  • 1 <= piles[i] <= 10^4
class Solution {
public:
    int maxCoins(vector<int>& piles) {
        sort(piles.begin(),piles.end());
        int ans = 0;
        for(int i = piles.size()-2;i>=int(piles.size()/3);i-=2)ans+=piles[i];
        return ans;
    }
};

三,数组的选取问题

力扣 55. 跳跃游戏

题目:

给定一个非负整数数组,你最初位于数组的第一个位置。

数组中的每个元素代表你在该位置可以跳跃的最大长度。

判断你是否能够到达最后一个位置。

示例 1:

输入: [2,3,1,1,4]
输出: true
解释: 我们可以先跳 1 步,从位置 0 到达 位置 1, 然后再从位置 1 跳 3 步到达最后一个位置。
示例 2:

输入: [3,2,1,0,4]
输出: false
解释: 无论怎样,你总会到达索引为 3 的位置。但该位置的最大跳跃长度是 0 , 所以你永远不可能到达最后一个位置。

代码:

class Solution {
public:
    bool canJump(vector<int>& nums) {
        int i=0,k=0;
        while(i<nums.size() && i<=k)k = max(k,nums[i] + i++);
        return i==nums.size();
    }
};

力扣 45. 跳跃游戏 II

题目:

给定一个非负整数数组,你最初位于数组的第一个位置。

数组中的每个元素代表你在该位置可以跳跃的最大长度。

你的目标是使用最少的跳跃次数到达数组的最后一个位置。

示例:

输入: [2,3,1,1,4]
输出: 2
解释: 跳到最后一个位置的最小跳跃数是 2。
     从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置。
说明:

假设你总是可以到达数组的最后一个位置。

代码:

class Solution {
public:
int jump(vector<int>& nums,int left,int right) {
        if(right>=nums.size()-1)return 0;
        int k=0;
        for(int i=left;i<=right;i++)k=max(k,i+nums[i]);
        return jump(nums,right+1,k)+1;
    }
    int jump(vector<int>& nums) {        
        return jump(nums,0,0);
    }
};

力扣 122. 买卖股票的最佳时机 II

题目:

给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。

设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。

注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

示例 1:

输入: [7,1,5,3,6,4]
输出: 7
解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
     随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。
示例 2:

输入: [1,2,3,4,5]
输出: 4
解释: 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
     注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。
     因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。
示例 3:

输入: [7,6,4,3,1]
输出: 0
解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。

代码:

class Solution {
public:
	int maxProfit(vector<int>& prices) {
		int ans = 0;
		for (int i = 1; i < prices.size(); i++)
		{
			ans = max(ans + prices[i] - prices[i - 1], ans);
		}
		return ans;
	}
};

力扣 376. 摆动序列

题目:

如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为摆动序列。第一个差(如果存在的话)可能是正数或负数。少于两个元素的序列也是摆动序列。

例如, [1,7,4,9,2,5] 是一个摆动序列,因为差值 (6,-3,5,-7,3) 是正负交替出现的。相反, [1,4,7,2,5] 和 [1,7,4,5,5] 不是摆动序列,第一个序列是因为它的前两个差值都是正数,第二个序列是因为它的最后一个差值为零。

给定一个整数序列,返回作为摆动序列的最长子序列的长度。 通过从原始序列中删除一些(也可以不删除)元素来获得子序列,剩下的元素保持其原始顺序。

示例 1:

输入: [1,7,4,9,2,5]
输出: 6 
解释: 整个序列均为摆动序列。
示例 2:

输入: [1,17,5,10,13,15,10,5,16,8]
输出: 7
解释: 这个序列包含几个长度为 7 摆动序列,其中一个可为[1,17,10,13,10,16,8]。
示例 3:

输入: [1,2,3,4,5,6,7,8,9]
输出: 2
进阶:
你能否用 O(n) 时间复杂度完成此题?

思路:

只需要看有多少折点即可。

代码:

class Solution {
public:
	int wiggleMaxLength(vector<int>& nums) {
		for (auto it = nums.begin()+1; it < nums.end();)
		{
			if (*it == *(it - 1))nums.erase(it);
			else it++;
		}
		if (nums.size() < 2)return nums.size();
		int ans = 2;
		for (int i = 2; i < nums.size(); i++)
		{
			if ((nums[i-1] - nums[i - 2])*(nums[i] - nums[i - 1]) < 0)ans++;
		}
		return ans;
	}
};

四,双端队列的选取问题

OpenJ_Bailian 3377 Best Cow Line, Gold

题目:

FJ is about to take his N (1 <= N <= 30,000) cows to the annual "Farmer of the Year" competition. In this contest every farmer arranges his cows in a line and herds them past the judges. 

The contest organizers adopted a new registration scheme this year: simply register the initial letter of every cow in the order they will appear (e.g., If FJ takes Bessie, Sylvia, and Dora in that order, he just registers BSD). After the registration phase ends, every group is judged in increasing lexicographic order (i.e., alphabetical order) according to the string of the initials of the cows' names. 

FJ is very busy this year and has to hurry back to his farm, so he wants to be judged as early as possible. He decides to rearrange his cows, who have already lined up, before registering them. 

FJ marks a location for a new line of the competing cows. He then proceeds to marshal the cows from the old line to the new one by repeatedly sending either the first or last cow in the (remainder of the) original line to the end of the new line. When he's finished, FJ takes his cows for registration in this new order. 

Given the initial order of his cows, determine the least lexicographic string of initials he can make this way.Input* Line 1: A single integer: N 

* Lines 2..N+1: Line i+1 contains a single initial ('A'..'Z') of the cow in the ith position in the original lineOutputThe least lexicographic string he can make. Every line (except perhaps the last one) contains the initials of 80 cows ('A'..'Z') in the newline.Sample Input

6
A
C
D
B
C
B

Sample Output

ABCBCD

题意很简单,给出数列,每次取出两端的一个字符,求能得到的最小字典序字符串。

贪心即可。

代码:

#include<iostream>
using namespace std;
 
int main()
{
	int n;
	cin >> n;
	char s[30001];
	for (int i = 1; i <= n; i++)cin >> s[i];
	int left = 1, right = n, l, r;
	n = 0;
	while (left <= right)
	{
		l = left, r = right;
		while (s[l] == s[r] && l < r - 1)l++, r--;
		if (s[l] < s[r])cout << s[left++];
		else cout << s[right--];
		n++;
		if (n % 80 == 0)cout << endl;
	}
	return 0;
}

五,循环数组的选取问题

力扣 134. 加油站

题目:

在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升。

你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发,开始时油箱为空。

如果你可以绕环路行驶一周,则返回出发时加油站的编号,否则返回 -1。

说明: 

如果题目有解,该答案即为唯一答案。
输入数组均为非空数组,且长度相同。
输入数组中的元素均为非负数。
示例 1:

输入: 
gas  = [1,2,3,4,5]
cost = [3,4,5,1,2]

输出: 3

解释:
从 3 号加油站(索引为 3 处)出发,可获得 4 升汽油。此时油箱有 = 0 + 4 = 4 升汽油
开往 4 号加油站,此时油箱有 4 - 1 + 5 = 8 升汽油
开往 0 号加油站,此时油箱有 8 - 2 + 1 = 7 升汽油
开往 1 号加油站,此时油箱有 7 - 3 + 2 = 6 升汽油
开往 2 号加油站,此时油箱有 6 - 4 + 3 = 5 升汽油
开往 3 号加油站,你需要消耗 5 升汽油,正好足够你返回到 3 号加油站。
因此,3 可为起始索引。
示例 2:

输入: 
gas  = [2,3,4]
cost = [3,4,3]

输出: -1

解释:
你不能从 0 号或 1 号加油站出发,因为没有足够的汽油可以让你行驶到下一个加油站。
我们从 2 号加油站出发,可以获得 4 升汽油。 此时油箱有 = 0 + 4 = 4 升汽油
开往 0 号加油站,此时油箱有 4 - 3 + 2 = 3 升汽油
开往 1 号加油站,此时油箱有 3 - 3 + 3 = 3 升汽油
你无法返回 2 号加油站,因为返程需要消耗 4 升汽油,但是你的油箱只有 3 升汽油。
因此,无论怎样,你都不可能绕环路行驶一周。

代码:

class Solution {
public:
	int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
		int dif = 0, sumDif = 0, ans = 0;
		for (int i = 0; i < gas.size(); i++)
		{
			dif += gas[i] - cost[i], sumDif += gas[i] - cost[i];
			if (dif < 0)dif = 0, ans = i + 1;
		}
		return (sumDif < 0) ? -1 : ans;
	}
};

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
贪心算法是一种常见的近似算法,用于在图论、排队问题、选择问题等多种实际问题中求解最优解。而背包问题是一个经典的优化问题,是贪心算法常用的应用之一。 以下是一个贪心算法背包问题的C语言代码: #include <stdio.h> int main() { int n, i, j, w[100], v[100], c, ans = 0; float t; //比率 printf("请输入背包数量和可容纳重量:\n"); scanf("%d %d", &n, &c); printf("请输入每个背包的重量和价值:\n"); for (i = 1; i <= n; i++) { scanf("%d %d", &w[i], &v[i]); } //计算每个背包的比率,按比率排序 for (i = 1; i <= n; i++) { for (j = i + 1; j <= n; j++) { if ((float) v[i] / w[i] < (float) v[j] / w[j]) { t = (float) v[i] / w[i]; v[i] = v[j]; w[i] = w[j]; v[j] = (int) (v[i] / t); w[j] = (int) (w[i] / t); } } } //不断选取比率最高的背包 for (i = 1; i <= n && c > 0; i++) { if (w[i] <= c) { ans += v[i]; c -= w[i]; } else { ans += (int) (v[i] * ((float) c / w[i])); c = 0; } } printf("该背包能够获取的最大价值为:%d", ans); return 0; } 输入数据后,程序首先计算每个背包的比率,按比率从高到低排序。接下来,程序从比率最高的背包开始,一直按比率选取背包,直到无法再选取为止。在选取背包时,程序优先选取可以直接放进背包的背包,如果一个背包无法完全放进背包,程序则按比率选取部分放进背包。最后,程序输出可获取的最大价值。 以上是一个简单的贪心算法背包问题C语言代码。通过这个例子,我们可以看到贪心算法的两个重要特点:1. 每一步只考虑当前最优解。2. 最终结果必须为最优解。但是,贪心算法也有其局限性,它不一定能够找到全局最优解,而只能得到一个近似最优解。因此,在实际应用中,我们需要结合问题特点,选择合适的算法,避免贪心算法带来的误差。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值