贪心算法归纳
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);}
}

相关内容

热门资讯

山东多城市消费上新,品质生活触... 海报新闻记者 李金珊 实习生 宋亚轩 孙晔 报道 上午十点,临沂兰山区的李女士送完孩子回家,指尖轻点...
“20cm”六连板,大牛股明日... 1月15日晚,志特新材(300986)、*ST铖昌(001270)两只停牌核查股同日宣布,即将复牌。...
原创 特... 特朗普政府试图将委内瑞拉的石油资源纳入美国巨头的囊中,但事与愿违。这场旨在促成巨额投资的白宫会议,最...
原创 谢... 谢尔盖·布林:科技巨擘的崛起与慈善之路 2026年1月12日,一个值得铭记的日子。这一天,在Alph...
马士基宣布恢复红海航线 航运业... 财联社1月16日讯(编辑 牛占林)丹麦航运巨头马士基(Maersk)当地时间周四表示,在该地区安全形...
贵金属价格上涨能否持续 回顾2025年大类资产表现,以黄金、白银、铂族金属为代表的贵金属板块价格一路狂飙,令全球投资者印象深...
手握金条无处安放?银行保管箱“... 本报(chinatimes.net.cn)记者付乐 见习记者 林佳茹 北京报道 “现在网点暂时没有空...
雷军直播再谈“反小字营销”!新... 1月15日晚,小米创办人、董事长兼CEO雷军在直播中再度聚焦“小字营销”问题,明确表达小米反对该行业...
重庆今年首只新股至信股份上市 ... 1月15日,重庆至信实业股份有限公司(以下简称“至信股份”)在上海证券交易所主板敲锣上市,成为今年A...
创历史新高!2025年我国外汇... 国家外汇管理局1月15日发布数据显示,2025年,我国外汇市场交易量达42.6万亿美元,企业外汇套期...
跨境电商运营:2026全球瑜伽... 今天分享的是:跨境电商运营:2026全球瑜伽服市场洞察报告 报告共计:41页 《2026全球瑜伽服市...
中文在线涨势“刹车”,上市以来... 来源:市场资讯 来源:国际金融报 1月15日,连续多日因“狂飙”行情被称为新“易中天”之一的中文在线...
原创 1... 写在文章前的声明:在本文之前的说明:本文中所列的投资信息,只是一个对基金资产净值进行排行的客观描述,...
湾财晚报| 商业用房首付比例降... 编辑 | 子衿 商业用房首付比例降至30% 今日,商办地产市场迎来重磅利好。 中国人民银行新闻发言人...
全年26亿!这些银行“喜提”罚... 过去一年,监管“长牙带刺、有棱有角”的态度一以贯之,驱动银行从根源上提升合规能力,强化内控管理。 企...
业绩拐点已现?金沃股份预计去年... 公司于今日晚间发布公告,2025年,公司在手订单情况良好,预计实现营业收入12.4-12.5亿元,同...
监管批复!郑荣波就任东亚银行珠... 2026年1月15日,根据国家金融监督管理总局消息,《关于核准郑荣波东亚银行(中国)有限公司珠海分行...
格力电器,近56亿元“大红包”... 1月15日晚,格力电器披露2025年中期权益分派实施公告。 根据方案,格力电器拟以公司现有总股本剔...
新技术助力提高IVF成功率,女... 打破生育瓶颈:新技术或助提高IVF成功率 想象一下,经历了数次试管婴儿(IVF)治疗的女性,心中满是...
卖“未来”换喘息:一品红主业失... 一品红(300723.SZ)再次向市场递交了一份亏损的成绩单。 1月14日发布的业绩预告显示,一品红...