贪心算法的基本步骤:

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. UITabBarController相关之tabBar文字不显示

    1.在用tabBarController管理控制器的时候,出现了下面的问题 对应的代码: JingHuaController *jinghuaVC = [[JingHuaController allo ...

  2. 自动打开notepad 并写入数据

    import java.awt.AWTException; import java.awt.Robot; import java.awt.event.KeyEvent; import java.io. ...

  3. c语言scanf详解

    函数名: scanf 功 能: 执行格式化输入 用 法: int scanf(char *format[,argument,...]);scanf()函数是通用终端格式化输入函数,它从标准输入设备(键 ...

  4. c# 去除文本的html标签

    public static string ContentReplace(string input) { input = Regex.Replace(input, @"<(.[^> ...

  5. java 数组流

    Example10_10.java import java.io.*; public class Example10_10 { public static void main(String args[ ...

  6. C#中:函数访问级别对函数形参访问级别的约束

    Inconsistent accessibility: parameter type 'Program.CommandLineInfo' is less accessible than method ...

  7. Talking about Health

    George: Doctor, what is the best way to stay healthy? Dictor: Having a good diet is probably the mos ...

  8. PAT乙1002

    有毒,真的有毒,难题对于简单题影响太大了,想的东西太多,总会在考虑,会不会时间超限,数据量有多大,三个循环就太慢了.... 总之我是真的不愿意看到这样的程序能过的,总觉得有更好的,却总是找不更好的额. ...

  9. hdu_3886_Final Kichiku “Lanlanshu”(数位DP)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3886 题意:这题的题意有点晦涩难懂,大概意思就是给你一个区间,让你找一些满足递增递减条件的数,举个列: ...

  10. android从asset文件夹读取文件

    1)将一个txt文本(msg.txt)复制到开发目录的asset文件夹下. 2)用getAssets().open()可以得到一个输入流.注意getAssets方法必须用在Activity下边.如果不 ...