贪心算法归纳
admin
2024-01-22 04:18:19
0

贪心算法,顾名思义是在程序设计过程中,在得到问题的解过程中,每一步采取的策略都为当前的局部最优,当这样进行下去确定最后得到的解为全局最优解,则贪心算法有效。

贪心算法一般的基本步骤如下:

1.确定每一步的局部最优解可以构成全局最优

2.从问题的初始状态开始得到问题的初始解

3.接下来每一步都选择在某种条件下当前最优的解,循环递进,缩减问题规模

4.综合得到的局部最优解,得到全局最优解

例如hdoj中的

Moving Tables

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11505    Accepted Submission(s): 3931


 

Problem Description

The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in the following figure.
 



The floor has 200 rooms each on the north side and south side along the corridor. Recently the Company made a plan to reform its system. The reform includes moving a lot of tables between rooms. Because the corridor is narrow and all the tables are big, only one table can pass through the corridor. Some plan is needed to make the moving efficient. The manager figured out the following plan: Moving a table from a room to another room can be done within 10 minutes. When moving a table from room i to room j, the part of the corridor between the front of room i and the front of room j is used. So, during each 10 minutes, several moving between two rooms not sharing the same part of the corridor will be done simultaneously. To make it clear the manager illustrated the possible cases and impossible cases of simultaneous moving.
 



For each room, at most one table will be either moved in or moved out. Now, the manager seeks out a method to minimize the time to move all the tables. Your job is to write a program to solve the manager’s problem.

Input

The input consists of T test cases. The number of test cases ) (T is given in the first line of the input. Each test case begins with a line containing an integer N , 1<=N<=200 , that represents the number of tables to move. Each of the following N lines contains two positive integers s and t, representing that a table is to move from room number s to room number t (each room number appears at most once in the N lines). From the N+3-rd line, the remaining test cases are listed in the same manner as above.

Output

The output should contain the minimum time in minutes to complete the moving, one per line.

Sample Input

 

3 4 10 20 30 40 50 60 70 80 2 1 3 2 200 3 10 100 20 80 30 50

Sample Output

 

10 20 30

Source

Asia 2001, Taejon (South Korea)

思路如下:

1.使用数组标记搬运桌子的路径的开始到结尾经过路径

2.从走廊的最开始位置出发,循环比较得出当前路径标记的最大值

3.循环结束后综合了每次循环得到的局部最优值得到搬运路径的最大的重叠,则为问题的解

代码如下:

#include
#include
using namespace std;int rooms[201]={0};int main(){int T,N,begin,end,max;scanf("%d",&T);while(T--){max=0;for(int i=0; i<201; i++)rooms[i]=0;scanf("%d",&N);for(int j=0; jend)swap(begin,end);for(int k=(begin+1)/2; k<=(end+1)/2; k++)rooms[k]++;}for(int g=0; g<201; g++)if(rooms[g]>max)max=rooms[g];printf("%d\n",max*10);}
}

相关内容

热门资讯

这场陆家嘴论坛,把中国经济金融... 来源:市场资讯 (来源:券研社) 从2008年启航,陆家嘴论坛就是观察中国经济金融走势的重要风向标...
原创 6... 周末去看房的人,情绪比以前复杂得多。售楼处里灯光还是亮,样板间还是精致,销售的话术却变了。前些年最常...
监管批复!陶曦就任成都银行行长... 2026年6月23日,根据国家金融监督管理总局消息,《成都银行股份有限公司关于陶曦高级管理人员任职资...
港股AI双雄遭空头伏击 7月超... 财联社6月23日讯(编辑 胡家荣)港股AI大模型板块的两大领头羊——智谱(02513.HK)与MIN...
山西运城:高度重视,已组成联合... “盐湖公安”微信公号、浪潮新闻 6月23日,山西省运城市公安局盐湖分局发布警情通报: 近日,网络平台...
监管批复!向云勇就任湖南吉首农... 2026年6月23日,根据国家金融监督管理总局消息,《湖南吉首农村商业银行股份有限公司关于对向云勇同...
曹操出行(2643.HK)Ro... 过去几年,大模型让AI完成了从“会聊天”到“会思考”的跨越。随着模型能力不断提升,产业关注点也开始从...
原创 金... 2026年6月23日,金价处在回调震荡阶段,水贝足金首饰批发价挂到1076元每克,但周大福、周生生那...
字节跳动CEO梁汝波:收缩业务... 在6月23日举行的火山引擎Force大会上,字节跳动CEO梁汝波表示,AI的影响力至少不亚于PC、W...
香港黄金交易平台第一梯队有哪些... 香港一直是亚洲重要的黄金交易中心。很多人在选择黄金交易平台时,面对大量的品牌,难免会感到困惑。香港黄...
WAVES 2026:今年盛夏... 盛夏赴约WAVES 2026!一群迎风的少数人,掀起中国新一代创新力量的风口。 2026年,创投圈的...
高鑫零售,试水“第三条路” 6月18日,盐城首家M会员商店轻享店在江苏盐南高新区佳源广场试营业。这是大润发母公司高鑫零售旗下全国...
28家企业排队,美妆IPO迎来... 沉寂三年后,美妆IPO重新热了起来。 2020年至2021年,是中国美妆企业上市的“黄金窗口”。贝泰...
原创 套... #格力第一大股东套现近 15.9 亿 #,格力电器正式发布减持结果公告,公司第一大股东珠海明骏(高瓴...
原创 昆... 6月23日,昆仑行机器人宣布,公司自2026年3月注册成立不足90天内,接连完成三轮融资,累计规模达...
心智观察所:从磷化铟的故事看中... 【文/观察者网 心智观察所 】 云南锗业的股价在2026年4月跑出了一波让人困惑的行情。 这家以锗为...
马云带着一群阿里合伙人,下田插... “马云带着一群阿里合伙人下田插秧,此次插秧团建的“同事们”阵容强大,吴泳铭、邵晓锋、蒋凡、吴泽明、蒋...
胖东来近半年累计销售额超139... 上证报中国证券网讯 6月22日晚,胖东来创始人于东来通过社交平台分享了集团近半年的经营情况。数据显示...
原创 帮... 达沃斯开幕+长川/卫星中报开门红:今天A股走“业绩提纯”,别蹭概念刀口舔血 老铁们,今天的早观察关键...
原创 腾... 原创首发 | 金角财经(ID: F-Jinjiao) 作者 | 田羽 “中国AMD”准备登陆A股了。...