贪心算法的基本步骤:

1、从问题的某个初始解出发。
2、采用循环语句,当可以向求解目标前进一步时,就根据局部最优策略,得到一个部分解,缩小问题的范围或规模。
3、将所有部分解综合起来,得到问题的最终解。

HDU2037:

链接:http://acm.hdu.edu.cn/showproblem.php?pid=2037

题解:

贪心算法:在对问题求解时,总是作出在当前看来是最好的选择。也就是说,不从整体上加以考虑,它所作出的仅仅是在某种意义上的局部最优解(是否是全局最优,需要证明)。若要用贪心算法求解某问题的整体最优解,必须首先证明贪心思想在该问题的应用结果就是最优解!!

本题是贪心法的一个最简单的例子,将结束时间按从小到大排好序,然后寻找下一个开始时间大于等于上一个结束时间的,如此往复,即可解决问题

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
const int maxn=+;
typedef struct
{
int ts;
int te;
}t;
t p[maxn];
const int cmp(const t s1,const t s2)
{
return s1.te<s2.te;
}
int main()
{
int n;
while(cin>>n)
{
if(n==)
break;
for(int i=;i<n;i++)
cin>>p[i].ts>>p[i].te;
sort(p,p+n,cmp);
int cnt=;
for(int i=;i<n;i++)
{
for(int j=i+;j<n;j++)
if(p[i].te<=p[j].ts)
{
i=j;
cnt++;
continue;
}
}
cout<<cnt<<endl;
}
return ;
}

HDU1050

区间覆盖问题:

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1050

题解:先创建一个函数judge(),输入的是任意房间号,输出的是走廊的位置。比如1号房间和2号房间都对应位置1的走廊,3号房间和4号房间对应位置2的走廊,一共有200个这样的位置。我用a[201]定义这些位置,其中某位置的值就是经历这个位置的次数。如果从某一号房到另一号房,所经历的每个位置的次数都+1,最后再全体扫描下哪个位置经历次数最多,输出值便是这个位置的值*10

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
const int maxn=+;
int s[maxn];
int judge(int n)
{
if(n%==) return n/;
else return (n+)/;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n,a,b;
cin>>n;
memset(s,,sizeof(s));
for(int i=;i<n;i++)
{
cin>>a>>b;
if(a>b)
swap(a,b);
for(int j=judge(a);j<=judge(b);j++)
s[j]++;
}
int mx=;
for(int i=;i<;i++)
if(mx<s[i])
mx=s[i];
cout<<mx*<<endl;
}
return ;
}

贪心法基础题目 HDU的更多相关文章

  1. 贪心算法HURUST题目

    题目描述: Yogurt factory The cows have purchased a yogurt factory that makes world-famous Yucky Yogurt. ...

  2. 杭电OJ1789、南阳OJ236(贪心法)解题报告

    杭电OJ1789http://acm.hdu.edu.cn/showproblem.php?pid=1789 南阳OJ236http://59.69.128.203/JudgeOnline/probl ...

  3. java基础题目总结

    有些基础题目由于工作中用的比较少但却又是不可少的,这样回答起来就会反应慢,不确定,不准确,特此开了文章记录遇到的不确定或者回答比较拗口的问题. 1.servlet是单例的吗,是安全的吗,是多线程吗 s ...

  4. iOS 面试基础题目

    转载: iOS 面试基础题目 题目来自博客:面试百度的记录,有些问题我能回答一下,不能回答的或有更好的回答我放个相关链接供参考. 1面 Objective C runtime library:Obje ...

  5. Python运维开发基础06-语法基础【转】

    上节作业回顾 (讲解+温习120分钟) #!/usr/bin/env python3 # -*- coding:utf-8 -*- # author:Mr.chen # 添加商家入口和用户入口并实现物 ...

  6. Python运维开发基础05-语法基础【转】

    上节作业回顾(讲解+温习90分钟) #!/usr/bin/env python # -*- coding:utf-8 -*- # author:Mr.chen import os,time Tag = ...

  7. Python运维开发基础04-语法基础【转】

    上节作业回顾(讲解+温习90分钟) #!/usr/bin/env python3 # -*- coding:utf-8 -*- # author:Mr.chen # 仅用列表+循环实现“简单的购物车程 ...

  8. Python运维开发基础03-语法基础 【转】

    上节作业回顾(讲解+温习60分钟) #!/usr/bin/env python3 # -*- coding:utf-8 -*- # author:Mr.chen #只用变量和字符串+循环实现“用户登陆 ...

  9. Python运维开发基础02-语法基础【转】

    上节作业回顾(讲解+温习60分钟) #!/bin/bash #user login User="yunjisuan" Passwd="666666" User2 ...

随机推荐

  1. eclipse 终极操作技巧

    eclipse作为一个java开发必备软件,从用户体验来说,还是蛮一般的(按照初始设置的话),所以有必要进行一些设置上的改良,加上对一些好用的快捷键的挖掘,能让你用eclipse更加得心应手,事半功倍 ...

  2. 删除 win8.1中的网络1,网络2,宽带连接1,宽带连接2等网络记录

    新建txt文本,保存如下内容为reg文档: Windows Registry Editor Version 5.00 [-HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\W ...

  3. ubuntu server 11.10 安装 oracle 10g XE

    1.将配置的Oracle源服务器的公钥添加在本地 apt 系统的密钥库中: wget http://oss.oracle.com/el4/RPM-GPG-KEY-oraclesudo apt-key  ...

  4. DevExpress.LookUpEdit控件实现自动搜索定位功能 兼使用方法(looUpEdit可编辑)

    DevExpress.LookUpEdit 使用方法 设置可手动输入 this.LookUpEdit1.Properties.TextEditStyle = DevExpress.XtraEditor ...

  5. 【转】Apache 关于 mod_rewrite 遇到 %2F或%5C (正反斜杠)等特殊符号导致URL重写失效出现404的问题

    .htaccess 文件 <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d Rew ...

  6. java HTTP请求 DefaultHttpClient is deprecated

    最近在使用Apache的httpclient的时候,maven引用了最新版本4.3,发现Idea提示DefaultHttpClient等常用的类已经不推荐使用了,之前在使用4.2.3版本的时候,还没有 ...

  7. 转 ORACLE数据库它可以存储 中文 字节或字符

    一:因为ORACLE数据库它可以存储字节或字符,例如 CHAR(12 BYTE) CHAR(12 CHAR)的意义是不同的.一般来说默认是存储字节,你可以查看数据库参数NLS_LENGTH_SEMAN ...

  8. webView进度条

    self.progress = [[NJKWebViewProgress alloc] init]; self.webView.delegate = self.progress; __weak typ ...

  9. ibus用上搜狗拼音词库

    1.下载搜狗拼音词库 wget http://hslinuxextra.googlecode.com/files/sougou-phrases-full.7z 2.用sougou-phrases-fu ...

  10. 2304: Lights Out(枚举)

    枚举第一行所有可能的的情况 #include<iostream> #include<cstdio> #include<cstring> #include<al ...