贪心算法习题
第4章贪心算法习题(免费阅读)

算法实现题4-5 程序存储问题
数据输入:
第一行是2 个正整数,分别表示文件个数n和磁带的长 度L。接下来的1 行中,有n个正整数,表示程序存放在磁 带上的长度。
结果输出: 最多可以存储的程序数。
输入示例
6 50
2 3 13 8 80 20 输出示例
5
i 012345
x 2 3 13 8 80 20 7
3
算法实现题4-5 程序存储问题
问题描述: 设有n 个程序{1,2,…, n }要存放在长度为L的磁带上。
程序i存放在磁带上的长度是 li,1 ≤ i ≤ n。 程序存储问题要求确定这n 个程序在磁带上的一个
存储方案,使得能够在磁带上存储尽可能多的程序。 编程任务:
对于给定的n个程序存放在磁带上的长度,编程计 算磁带上最多可以存储的程序数。
532.00
10
算法实现题4-6 最优服务次序问题
double greedy( vector<int> x) {
int i,n=x.size(); sort(x.begin(),x.end()); for(i=1;i<n;++i)
x[i] += x[i-1]; double t=0; for(i=0;i<n;++i) t+=x[i]; t /= n;
算法实现题4-5 程序存储问题
int greedy( vector<int> x, int m){
int i=0, sum=0, n=x.size();
sort(x.begin(),x.end());
while(i
if(sum <= m) i++;
第4章-贪心算法-习题

算法实现题4-15 套汇问题
while(1){
cin >> n;
if(n == 0) break;
//输入结束
for(i=0; i < n; ++i) cin >> name[i];
数据输入
第1行有2个正整数n和k,表示汽车加满油后可行驶nkm, 且旅途有k个加油站。接下来的一行中,有k+1个整数,表示 第k个加油站与第k-1个加油站之间的距离。第0个加油站表 示出发地,汽车已加满油。第k+1个加油站表示目的地。
结果输出
计算出的最少加油次数。如果无法到达目的地,则输 出”No Solution”。
3
算法实现题4-5 程序存储问题
问题描述: 设有n 个程序{1,2,…, n }要存放在长度为L的磁带上。
程序i存放在磁带上的长度是 li,1 ≤ i ≤ n。 程序存储问题要求确定这n 个程序在磁带上的一个
存储方案,使得能够在磁带上存储尽可能多的程序。 编程任务:
对于给定的n个程序存放在磁带上的长度,编程计 算磁带上最多可以存储的程序数。
定义:
vector<int> x; 读取数据:
int n; scanf(“%d”, &n); int temp; for (int i=0; i<n; i++){
scanf(“%d”, &temp); x.push_back(temp); }
return t;
}
i 01234567 8 9
x 1 12 33 55 56 99 99 234 812 1000
输入示例 77
输出示例 4
1 2 3 4 5 1 6 6 加油站数
贪心算法练习题

贪心算法1.喷水装置(一)描述现有一块草坪,长为20米,宽为2米,要在横中心线上放置半径为Ri的喷水装置,每个喷水装置的效果都会让以它为中心的半径为实数Ri(0<Ri<15)的圆被湿润,这有充足的喷水装置i(1<i<600)个,并且一定能把草坪全部湿润,你要做的是:选择尽量少的喷水装置,把整个草坪的全部湿润。
输入第一行m表示有m组测试数据每一组测试数据的第一行有一个整数数n,n表示共有n个喷水装置,随后的一行,有n个实数ri,ri表示该喷水装置能覆盖的圆的半径。
输出输出所用装置的个数样例输入252 3.2 4 4.5 6101 2 3 1 2 1.2 3 1.1 1 2样例输出25根据日常生活知道,选择半径越大的装置,所用的数目越少。
因此,可以先对半径排序,然后选择半径大的。
另外,当装置刚好喷到矩形的顶点时,数目最少。
此时只要装置的有效喷水距离的和不小于20时,输出此时的装置数目即可。
2.喷水装置(二)时间限制:3000 ms | 内存限制:65535 KB难度:4描述有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的喷水装置,每个喷水装置i喷水的效果是让以它为中心半径为Ri的圆都被润湿。
请在给出的喷水装置中选择尽量少的喷水装置,把整个草坪全部润湿。
输入对于每一组输入,输出最多能够安排的活动数量。
每组的输出占一行样例输入221 1010 1131 1010 1111 20样例输出12提示注意:如果上一个活动在T时间结束,下一个活动最早应该在T+1时间开始。
解题思路:这是一个贪心法中选择不相交区间的问题。
先对活动结束时间从小到大排序,排序的同时活动的起始时间也要跟着变化。
而且,结束时间最小的活动一定会安排,不然这段时间就白白浪费了。
后一个活动的起始时间如果比前一个活动的结束时间大,即两个活动没有相交时间,就把这个活动也安排上。
贪心算法_精品文档

贪心算法第八章贪心算法一、选择题1用贪心法设计算法的关键是(D)。
A.将问题分解为多个子问题来分别处理B.选好贪心准则C.获取各阶段间的递推关系式D.满足最优性原理2考虑背包问题:n=6,M=10,P(1:6)=(15,59,21,30,60,5),W(1:6)=(1,5,2,3,6,1)。
该问题的最大效益值为(B)。
若把它看着是0、1背包问题,则最大效益值为(C)。
A.101B.110C.115D.120 8。
3#includeintmain(){freopen(\intmoney,1,2,3,4,5,p,d,n,q,h,temp;canf(\temp=money;p=money、1;if(p>0)money-=p1;d=money、2;if(d>0)money-=d2;n=money、3;if(n>0)money-=n3;q=money、4;if(q>0)money-=q4;h=money、5;if(temp==(p1+d2+n3+q)){printf(\printf(\printf(\printf(\printf(\}eleprintf(\return0;}8。
4uingSytem;uingSytem。
Collection。
Generic;uingSytem。
Linq;uingSytem。
Tet;uingSytem。
Threading。
Tak;namepace动态规划解决硬币问题{claProgram{publicclaCoinChange{publictaticvoidmakeChange(int[]value,intvalueKind,intmoney,i nt[]coinUed){coinUed[0]=0;for(intcent=1;cent<=money;cent++){intminCoin=cent;for(intkind=0;kind<valueKind;kind++){}}}}}8。
第六章-贪心算法

//每堆牌的张数减去平均数
i:=1;j:=n;
while (a[i]=0) and (i<n) do inc(i);
//过滤左边的0
while (a[j]=0) and (j>1) do dec(j);
//过滤右边的0
while (i<j) do
begin
inc(a[i+1],a[i]); a[i]:=0; inc(step); inc(i); while (a[i]=0) and (i<j) do inc(i);
现在要求找出一种移动方法,用最少的移动次数使每堆上纸牌数都一样多。 例如 N=4,4 堆纸牌数分别为: ① 9 ② 8 ③ 17 ④ 6
移动3次可达到目的: 从 ③ 取4张牌放到④(9 8 13 10)->从③取3张牌放到 ②(9 11 10 10)> 从②取1张牌放到①(10 10 10 10)。 【输入格式】 N(N 堆纸牌,1 <= N <= 100) A1 A2 … An (N 堆纸牌,每堆纸牌初始数,l<= Ai <=10000) 【输出格式】 所有堆均达到相等时的最少移动次数。 【样例输入】Playcard.in
输出n;
//删去串首可能产生的无用零
【例6】拦截导弹问题(NOIP1999) 某国为了防御敌国的导弹袭击,开发出一种导弹拦截系统,但是这种拦
截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每 一发炮弹都不能高于前一发的高度。某天,雷达捕捉到敌国的导弹来袭,由 于该系统还在试用阶段。所以一套系统有可能不能拦截所有的导弹。
因此,贪心不能简单进行,而需要全面的考虑,最后得到证明。
【例3】排队打水问题
c语言贪心算法题目

c语言贪心算法题目
问题描述:
给定一个非负整数数组nums,你的任务是通过在数组中选择一些数,使得这些数的和最大,并且所选的数不能相邻。
请编写一个C程序,实现该任务。
函数原型:
int maxSum(int* nums, int numsSize);
输入参数:
- nums: 非负整数数组nums
- numsSize:数组nums的大小
输出:
返回所选数的最大和
示例:
输入:[1,2,3,1]
输出:4
解释:选择索引为0和2的元素,即nums[0]=1和nums[2]=3,它们的和为4,满足选取数的和最大且不相邻的要求。
提示:
1. 贪心算法是一种优化问题的解决方法,它每次都选择当前最优的解,希望通过局部最优解的选择来达到全局最优解。
2. 对于本题,可以使用动态规划的思想来解决。
通过定义一个dp数组,dp[i]表示选择索引为0到i之间的元素,所选数的最大和。
则对于元素nums[i],有两种选择:选或不选。
- 如果选择nums[i],则dp[i] = nums[i] + dp[i-2]
- 如果不选择nums[i],则dp[i] = dp[i-1]
- 因此,对于每个i,我们选择使得dp[i]最大的方案,直到求得dp[numsSize-1],即所选数的最大和。
3. 初始化dp数组时,dp[0]=nums[0],dp[1]=max(nums[0], nums[1]),从第三个元素开始遍历数组即可。
贪心算法几个经典例子c语言

贪心算法几个经典例子c语言1. 零钱兑换问题题目描述:给定一些面额不同的硬币和一个总金额,编写一个函数来计算可以凑成总金额所需的最少的硬币个数。
如果没有任何一种硬币组合能够凑出总金额,返回 -1。
贪心策略:每次选择面额最大的硬币,直到凑出总金额或者无法再选择硬币为止。
C语言代码:int coinChange(int* coins, int coinsSize, int amount){int count = 0;for(int i = coinsSize - 1; i >= 0; i--){while(amount >= coins[i]){amount -= coins[i];count++;}}return amount == 0 ? count : -1;}2. 活动选择问题题目描述:有 n 个活动,每个活动都有一个开始时间和结束时间,选择一些活动使得它们不冲突,且能够参加的活动数最多。
贪心策略:每次选择结束时间最早的活动,直到所有活动都被选择或者无法再选择为止。
C语言代码:typedef struct{int start;int end;}Activity;int cmp(const void* a, const void* b){return ((Activity*)a)->end - ((Activity*)b)->end;}int maxActivities(Activity* activities, int n){qsort(activities, n, sizeof(Activity), cmp);int count = 1;int end = activities[0].end;for(int i = 1; i < n; i++){if(activities[i].start >= end){count++;end = activities[i].end;}}return count;}3. 跳跃游戏题目描述:给定一个非负整数数组,你最初位于数组的第一个位置。
贪心算法题库

贪心算法是一种在每一步选择中都采取当前情况下的局部最优选择,并希望导致结果是全局最优解的算法。
下面是一些贪心算法的题目和解答:1. 旅行商问题(Travelling Salesman Problem):问题描述:给定一个城市列表和一个距离列表,要求找出一条路径,使得路径上的所有城市都经过,且总距离最短。
贪心算法解法:首先对城市按照距离进行排序,然后从最近的两个城市开始,每次都选择距离当前位置最近的两个城市,直到遍历完所有城市。
由于贪心算法每次选择的都是当前情况下的最优解,因此最终得到的路径总距离是最短的。
2. 背包问题(Knapsack Problem):问题描述:给定一组物品,每个物品都有自己的重量和价值,要求在不超过背包总重量的情况下,如何选择物品使得背包中物品的总价值最大。
贪心算法解法:按照物品的重量对物品进行排序,然后每次选择重量最小的物品,直到背包已满或无物品可选。
由于贪心算法每次选择的都是当前情况下的最优解,因此最终得到的方案总是可以找到一个大于等于当前最优解的方案。
3. 网格找零问题(Currency Change Problem):问题描述:给定一组面值不同的硬币,要求用最少的组合方式从一定金额中找零。
贪心算法解法:首先对硬币面值进行排序,然后每次使用当前面值最小的硬币进行组合,直到金额为零或无硬币可选。
贪心算法在此问题中的思路是每次选择最小的硬币进行使用,这样可以保证找零的最小数量。
以上题目和解答只是贪心算法的一部分应用,实际上贪心算法在许多其他领域也有广泛的应用,例如网页布局优化、任务调度、网络流等等。
贪心算法的优势在于其简单易懂、易于实现,但也有其局限性,例如无法处理一些存在冲突的情况或最优解不唯一的问题。
因此在实际应用中需要根据具体问题选择合适的算法。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
喷水装置(一)时间限制:3000 ms | 内存限制:65535 KB难度:3描述现有一块草坪,长为20米,宽为2米,要在横中心线上放置半径为Ri的喷水装置,每个喷水装置的效果都会让以它为中心的半径为实数Ri(0<Ri<15)的圆被湿润,这有充足的喷水装置i(1<i<600)个,并且一定能把草坪全部湿润,你要做的是:选择尽量少的喷水装置,把整个草坪的全部湿润。
输入第一行m表示有m组测试数据每一组测试数据的第一行有一个整数数n,n表示共有n个喷水装置,随后的一行,有n个实数ri,ri表示该喷水装置能覆盖的圆的半径。
输出输出所用装置的个数输入第一行输入一个正整数N表示共有n次测试数据。
每一组测试数据的第一行有三个整数n,w,h,n表示共有n个喷水装置,w表示草坪的横向长度,h表示草坪的纵向长度。
随后的n行,都有两个整数xi和ri,xi表示第i个喷水装置的的横坐标(最左边为0),ri表示该喷水装置能覆盖的圆的半径。
输出每组测试数据输出一个正整数,表示共需要多少个喷水装置,每个输出单独占一行。
如果不存在一种能够把整个草坪湿润的方案,请输出0。
输入第一行是一个整型数m(m<100)表示共有m组测试数据。
每组测试数据的第一行是一个整数n(1<n<10000)表示该测试数据共有n个活动。
随后的n行,每行有两个正整数Bi,Ei(0<=Bi,Ei<10000),分别表示第i个活动的起始与结束时间(Bi<=Ei)输出对于每一组输入,输出最多能够安排的活动数量。
每组的输出占一行样例输入样例输出12Gone Fishing时间限制:3000 ms | 内存限制:65535 KB难度:5描述John is going on a fishing trip. He has h hours available (1 <= h <= 16), and there are n lakes in the area (2 <= n <= 25) all reachable along a single, one-way road.John starts at lake 1, but he can finish at any lake he wants. He can only travelfrom one lake to the next one, but he does not have to stop at any lake unless he wishes to. For each i = 1,...,n - 1, the number of 5-minute intervals it takes totravel from lake i to lake i + 1 is denoted ti (0 <ti<=192). For example, t3 = 4means that it takes 20 minutes to travel from lake 3 to lake 4. To help plan hisfishing trip, John has gathered some information about the lakes. For each lake i, the number of fish expected to be caught in the initial 5 minutes, denoted fi( fi >=0 ), is known. Each 5 minutes of fishing decreases the number of fish expected tobe caught in the next 5-minute interval by a constant rate of di (di >= 0). If thenumber of fish expected to be caught in an interval is less than or equal to di ,there will be no more fish left in the lake in the next interval. To simplify theplanning, John assumes that no one else will be fishing at the lakes to affect thenumber of fish he expects to catch.Write a program to help John plan his fishing trip to maximize the number of fish expected to be caught. The number of minutes spent at each lake must be amultiple of 5.输入You will be given a number of cases in the input. Each case starts with a linecontaining n. This is followed by a line containing h. Next, there is a line of nintegers specifying fi (1 <= i <=n), then a line of n integers di (1 <=i <=n), andfinally, a line of n - 1 integers ti (1 <=i <=n - 1). Input is terminated by a case in which n = 0.输出For each test case, print the number of minutes spent at each lake, separated by commas, for the plan achieving the maximum number of fish expected to becaught (you should print the entire plan on one line even if it exceeds 80characters). This is followed by a line containing the number of fish expected.If multiple plans exist, choose the one that spends as long as possible at lake 1,even if no fish are expected to be caught in some intervals. If there is still a tie,choose the one that spends as long as possible at lake 2, and so on. Insert a blank line between cases.过河问题时间限制:1000 ms | 内存限制:65535 KB难度:5描述在漆黑的夜里,N位旅行者来到了一座狭窄而且没有护栏的桥边。
如果不借助手电筒的话,大家是无论如何也不敢过桥去的。
不幸的是,N个人一共只带了一只手电筒,而桥窄得只够让两个人同时过。
如果各自单独过桥的话,N人所需要的时间已知;而如果两人同时过桥,所需要的时间就是走得比较慢的那个人单独行动时所需的时间。
问题是,如何设计一个方案,让这N人尽快过桥。
输入第一行是一个整数T(1<=T<=20)表示测试数据的组数每组测试数据的第一行是一个整数N(1<=N<=1000)表示共有N个人要过河每组测试数据的第二行是N个整数Si,表示此人过河所需要花时间。
(0<Si<=100)输出输出所有人都过河需要用的最少时间C小加有一些木棒,它们的长度和质量都已经知道,需要一个机器处理这些木棒,机器开启的时候需要耗费一个单位的时间,如果第i+1个木棒的重量和长度都大于等于第i个处理的木棒,那么将不会耗费时间,否则需要消耗一个单位的时间。
因为急着去约会,C小加想在最短的时间内把木棒处理完,你能告诉他应该怎样做吗?输入第一行是一个整数T(1<T<1500),表示输入数据一共有T组。
每组测试数据的第一行是一个整数N(1<=N<=5000),表示有N个木棒。
接下来的一行分别输入N个木棒的L,W(0 < L ,W <= 10000),用一个空格隔开,分别表示木棒的长度和质量。
输出处理这些木棒的最短时间。
Farmer John needs to travel to town to pick up K (1 <= K <= 100)pounds of feed.Driving D miles with K pounds of feed in his truck costs D*K cents.The county feed lot has N (1 <= N<= 100) stores (conveniently numbered 1..N) that sell feed. Each store is located on a segment of the X axis whose length is E (1 <= E <= 350). Store i is at location X_i (0 <X_i< E) on the number line and can sell John as much as F_i (1 <= F_i<= 100) pounds of feed at a cost of C_i (1 <= C_i<= 1,000,000) cents per pound.Amazingly, a given point on the X axis might have more than one store.Farmer John starts at location 0 on this number line and can drive only in thepositive direction, ultimately arriving at location E, with at least K pounds of feed.He can stop at any of the feed stores along the way and buy any amount of feed up to the the store's limit.What is the minimum amount Farmer John has to pay to buy and transport the K pounds of feed? Farmer Johnknows there is a solution.Consider a sample where Farmer John needs twopounds of feed from three stores (locations: 1, 3, and 4) on a number line whoserange is 0..5:0 1 2 3 4 5---------------------------------1 1 1 Available pounds of feed1 2 2 Cents per poundIt is best for John to buy one pound of feed from both the second and third stores.He must pay two cents to buy each pound of feed for a total cost of 4.When John travels from 3 to 4 he is moving 1 unit of length and he has 1 pound of feed so he must pay1*1 = 1 cents.When John travels from 4 to 5 heis moving one unit and he has 2 pounds of feed so he must pay 1*2 = 2 cents. The total cost is 4+1+2 = 7 cents.输入The first line of input contains a number c giving the number of cases that follow There are multi test cases ending with EOF.Each case starts with a line containing three space-separated integers: K, E, and N Then N lines follow :every line contains three space-separated integers: Xi Fi Ci 输出For each case,Output A single integer that is the minimum cost for FJ to buy and transport the feed但是,John的C (2 <= C <= N)头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争斗。