
ACM-贪心
文章平均质量分 75
LarryNLPIR
专注NLP/IR/Machine Learning/Data Mining
展开
-
POJ 1065 木棍问题 贪心算法
<br />算法分析:先对木棍结构体数组按照l排序,消除一个变量的干扰,然后再找w连续上升的子序列。用临时变量temp,遇到更大的作更新,且标记为0,采用贪心策略去找有几个连续上升的子序列<br />#include <iostream>#include <cstdlib>using namespace std;typedef struct stick{ int l; int w;}stick;int cmp(const void *a,const void *b){原创 2010-12-09 20:41:00 · 3370 阅读 · 5 评论 -
LeetCode Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Re原创 2015-03-22 12:08:06 · 1684 阅读 · 0 评论 -
Hackerrank Connected Cell in a Grid
Problem StatementYou are given a matrix with m rows and n columns of cells, each of which contains either 1or 0. Two cells are said to be connected if they are adjacent to each other horizontally, ver原创 2015-03-16 02:59:11 · 3992 阅读 · 0 评论 -
LeetCode Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if you ar原创 2015-01-05 07:24:32 · 1687 阅读 · 0 评论 -
LeetCode - Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For原创 2015-02-03 06:29:35 · 2000 阅读 · 1 评论 -
LeetCode Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your goal is to rea原创 2015-01-19 14:40:52 · 1563 阅读 · 0 评论 -
LeetCode Candy
There are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have at least one candy.原创 2015-01-12 14:56:12 · 1649 阅读 · 0 评论 -
LeetCode Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two原创 2014-11-16 10:37:08 · 2572 阅读 · 1 评论 -
POJ 3636 俄罗斯Nested Dolls 贪心二分 动态规划
首先要区分#include 中的qsort与C++ STL algorithm中的sort,后者只有三个参数且比较函数写法较简单些 类似于POJ 1065,差异有二,其一是排序方式下降,其二是要求下降且不相等但是用常规的贪心会超时,用二分优化后不超时,注意对l递增排,对w递减排// 类似于POJ 1065,差异有二,其一是排序方式下降,其二是要求下降且不相等// 上面这种解法会超时,用二分优化原创 2010-12-09 21:35:00 · 3342 阅读 · 2 评论 -
POJ2485 求最小生成树的最大边长度
此题关键要理解输出的定义For each test case, you should output a line contains an integer, which is the length of the longest road to be built such that all the villages are connected, and this value is minimu原创 2012-02-09 23:27:06 · 3006 阅读 · 0 评论 -
POJ 1521 哈夫曼编码 贪心法
题意:给定字符串,求哈夫曼编码长和它与等长编码的比值,比较基础思路:这题考查哈弗曼编码,但其实没必要建树得出编码,只需要统计哈弗曼编码后的总码长即可参考了网友的题解,用到了优先权队列维持一个从小到大的序列第38行其实就是把越小的频数反复多加几次,越大的频率少加几次,体现了前缀码的设计思想Source CodeProblem: 1521 User:原创 2011-12-29 02:28:51 · 6054 阅读 · 1 评论 -
POJ 1018 通信设备购买方案的选择 贪心
本题很简单,但是还是WA了几次,主要原因是1、变量初始化的位置要非常小心,何时需要在下一次循环重复记录何时初始化,由于这个原因WA太可惜了2、输出保留三位小数的格式控制一定要注意主要算法思想如下先求各个设备最小带宽中的最小值和最大带宽中的最小值然后遍历最小带宽到最大带宽的区间,求最大比例,贪心搜索最优方案加速方案设置标志数组flag,只有输入中间出现过的并且位于上下原创 2011-12-24 11:32:30 · 1833 阅读 · 0 评论 -
POJ 2709 颜料盒数量 贪心法
这么多天第一次1AC,虽然题目不难,但是心里还是挺高兴算法思路:先根据纯色颜料中的最大者求出至少需要的颜料盒数,然后用剩余的颜料来配gray,配的时候要1ml 1ml的配gray颜料,通过不断重新从大到小排序,每次从前三种颜料去1ml配出1mlgray颜料,配之前判断下第3种颜料是否还有剩余,没有就加1个颜料盒,以此类推,到gray颜料配足够为止。要注意判断一定要紧跟排序之后,防止减完第三种颜原创 2011-12-26 00:02:50 · 2757 阅读 · 0 评论 -
POJ 1042 钓鱼问题 贪心枚举及动态规划
<br /> <br />题意描述:<br />john现有h个小时的空闲时间,他打算去钓鱼。john钓鱼的地方共有n个湖,所有的湖沿着一条单向路顺序排列(john每在一个湖钓完鱼后,他只能走到下一个湖继续钓), john必须从1号湖开始钓起,但是他可以在任何一个湖结束他此次钓鱼的行程。john在每个湖中每5分钟钓的鱼数(此题中以5分钟作为单位时间),随时间的增长而线性递减。而每个湖中头5分钟可以钓到的鱼数以及每个湖中相邻5分钟钓鱼数的减少量,input中均会给出。并且John从任意一个湖走到它下一个湖的时原创 2010-12-14 18:55:00 · 5926 阅读 · 2 评论 -
LeetCode Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.Example 1原创 2015-03-18 12:50:48 · 1248 阅读 · 0 评论