贪心
贪心算法刷题分类
Suprit
先选择一种经历,经历再造就一个人
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
【题解】codeforce1256 D. Binary String Minimizing⭐⭐ 【贪心】
【题解】codeforce1256 D. Binary String Minimizing⭐⭐ 【贪心】You are given a binary string of length n (i. e. a string consisting of n characters ‘0’ and ‘1’).In one move you can swap two adjacent characters...原创 2019-11-07 20:12:50 · 587 阅读 · 0 评论 -
【题解】codeforce1237 C2. Balanced Removals (Harder)⭐⭐⭐ 【贪心】
codeforce1237 C2. Balanced Removals (Harder)This is a harder version of the problem. In this version, n≤50000.There are n distinct points in three-dimensional space numbered from 1 to n. The i-th po...原创 2019-10-17 11:17:16 · 730 阅读 · 1 评论 -
Stacks of Flapjacks(翻煎饼) UVA - 120 (贪心 排序)
堆栈和队列通常被认为是数据结构的面包和黄油,可用于体系结构、解析,操作系统和离散事件模拟。堆栈在形式语言理论中也很重要。现在的问题涉及黄油和煎饼(而不是面包),同时还有一个根据唯一但完整的规则来翻煎饼的服务器。给你一栈的煎饼,请你编写一个程序用于指示这个栈如何被排序以使得最大的煎饼在最下面而最小的煎饼在最上面。煎饼的直径将被给出。栈中的所有煎饼的直径都不一样。对栈排序是通过一系列"翻...原创 2019-03-19 21:02:39 · 923 阅读 · 0 评论 -
Sunscreen POJ - 3614 (贪心 优先队列)
题解: 优先队列往往和贪心或dp结合到一起来用, 更重要的往往是算法策略, 优先队列只能用来优化局部.这道题很明显的是贪心啦, 我们先将奶牛和防晒霜升序排列(sort默认pair使用first排序), 再满足minSPF的基础上, 我们尽可能的使用最小的maxSPF, 也就是物尽其用的原则, 更大的maxSPF也许有更多的选择, 很显然对于取maxSPF我们使用优先队列即可.#include...原创 2019-02-12 11:00:04 · 865 阅读 · 4 评论 -
B - Read Time CodeForces - 343C (二分+贪心)
B - Read Time CodeForces - 343C Mad scientist Mike does not use slow hard disks. His modification of a hard drive has not one, but n different heads that can read data in parallel.When viewed from ...原创 2019-03-12 17:49:40 · 642 阅读 · 0 评论 -
最少拦截系统 HDU - 1257 (贪心)
最少拦截系统 HDU - 1257 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度.某天,雷达捕捉到敌国的导弹来袭.由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹.怎么办呢?多搞几套系统呗!你说说倒蛮容易,成本呢?成本是个大问题啊.所以俺就到这里来求救了...原创 2019-04-01 15:25:52 · 532 阅读 · 0 评论 -
Equalize Them All CodeForces - 1144D (贪心)
Equalize Them All CodeForces - 1144DYou are given an array a consisting of n integers. You can perform the following operations arbitrary number of times (possibly, zero):Choose a pair of indices (...原创 2019-04-08 09:38:20 · 776 阅读 · 0 评论 -
B. Alyona and a Narrow Fridge (贪心)
B. Alyona and a Narrow Fridge Alyona has recently bought a miniature fridge that can be represented as a matrix with h rows and 2 columns. Initially there is only one shelf at the bottom of the fridg...原创 2019-04-08 10:19:16 · 766 阅读 · 0 评论 -
B. Yet Another Array Partitioning Task (构造 思维 好题)
B. Yet Another Array Partitioning TaskAn array b is called to be a subarray of a if it forms a continuous subsequence of a, that is, if it is equal to al, al+1, …, ar for some l,r.Suppose m is some...原创 2019-04-12 21:35:07 · 948 阅读 · 0 评论 -
【题解】B. Ugly Pairs(贪心 思维)⭐⭐
B. Ugly PairsYou are given a string, consisting of lowercase Latin letters.A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For exa...原创 2019-05-03 10:01:58 · 1117 阅读 · 0 评论 -
【题解】codeforce1187 C. Vasya And Array⭐⭐⭐ 【构造】
codeforce1187 C. Vasya And ArrayVasya has an array a1,a2,…,an.You don’t know this array, but he told you m facts about this array. The i-th fact is a triple of numbers ti, li and ri (0≤ti≤1,1≤li<...原创 2019-07-01 20:25:56 · 672 阅读 · 1 评论 -
【题解】codeforce1197 B. Pillars⭐⭐ 【贪心 构造】
codeforce1197 B. PillarsThere are n pillars aligned in a row and numbered from 1 to n.Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai.You can move ...原创 2019-07-23 11:08:25 · 494 阅读 · 0 评论 -
【题解】codeforces1197 C. Array Splitting⭐⭐⭐【思维 贪心】
codeforces1197 C. Array SplittingYou are given a sorted array a1,a2,…,an (for each index i>1 condition ai≥ai−1 holds) and an integer k.You are asked to divide this array into k non-empty consecut...原创 2019-07-23 11:44:14 · 609 阅读 · 0 评论 -
【题解】codeforce1206 B. Make Product Equal One⭐⭐【贪心】
codeforce1206 B. Make Product Equal One⭐⭐You are given n numbers a1,a2,…,an. With a cost of one coin you can perform the following operation:Choose one of these numbers and add or subtract 1 from it...原创 2019-08-19 20:06:33 · 715 阅读 · 0 评论 -
【题解】HDU6669 Game⭐⭐⭐ 【区间贪心】
HDU6669 Game度度熊在玩一个好玩的游戏。游戏的主人公站在一根数轴上,他可以在数轴上任意移动,对于每次移动,他可以选择往左或往右走一格或两格。现在他要依次完成 n 个任务,对于任务 i,只要他处于区间 [ai,bi] 上,就算完成了任务。度度熊想知道,为了完成所有的任务,最少需要移动多少次?度度熊可以任意选择初始位置。Input第一行一个整数 T (1≤T≤10) 表示数据组...原创 2019-08-20 09:43:11 · 577 阅读 · 1 评论 -
【题解】codeforce1204 B. Mislove Has Lost an Array⭐⭐⭐ 【贪心】
codeforce1204 B. Mislove Has Lost an ArrayMislove had an array a1, a2, ⋯, an of n positive integers, but he has lost it. He only remembers the following facts about it:The number of different number...原创 2019-08-22 09:41:52 · 666 阅读 · 0 评论 -
【题解】codeforces1203 B. Equal Rectangles⭐⭐ 【贪心】
codeforces1203 B. Equal Rectangles⭐⭐You are given 4n sticks, the length of the i-th stick is ai.You have to create n rectangles, each rectangle will consist of exactly 4 sticks from the given set. T...原创 2019-08-18 14:57:09 · 609 阅读 · 0 评论 -
【题解】codeforce 1208B. Uniqueness⭐⭐ 【贪心 枚举 好题】
codeforce 1208B. UniquenessYou are given an array a1,a2,…,an. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.In other words, at most one time you c...原创 2019-08-30 21:24:31 · 1011 阅读 · 0 评论 -
【题解】codeforce 1217 B. Zmei Gorynich⭐⭐ 【贪心】
codeforce 1217 B. Zmei GorynichYou are fighting with Zmei Gorynich — a ferocious monster from Slavic myths, a huge dragon-like reptile with multiple heads!Initially Zmei Gorynich has x heads. You ca...原创 2019-09-09 20:15:14 · 784 阅读 · 0 评论 -
【题解】计蒜客 Honk's pool⭐⭐⭐ 【思维】
计蒜客 Honk’s poolAs we all know, Honk has nn pools, numbered as 11 ~ nn . There is a_ia i liters water in the ii-th pool. Every day, Honk will perform the following operations in sequence.Find the po...原创 2019-09-18 14:56:46 · 461 阅读 · 0 评论 -
【题解】P1031 均分纸牌⭐⭐ 【贪心】
P1031 均分纸牌有NN堆纸牌,编号分别为 1,2,…,N1,2,…,N。每堆上有若干张,但纸牌总数必为NN的倍数。可以在任一堆上取若干张纸牌,然后移动。移牌规则为:在编号为11堆上取的纸牌,只能移到编号为22的堆上;在编号为NN的堆上取的纸牌,只能移到编号为N-1N−1的堆上;其他堆上取的纸牌,可以移到相邻左边或右边的堆上。现在要求找出一种移动方法,用最少的移动次数使每堆上纸牌数都一样多...原创 2019-09-19 15:37:27 · 760 阅读 · 1 评论 -
【题解】Drying POJ - 3104 ⭐⭐⭐ 【二分】
Drying POJ - 3104 最让HSQ学长头疼的就是洗衣服了。洗完之后,每件衣服都有一定单位水分,在不使用烘干器的情况下,每件衣服每分钟自然流失1个单位水分,但如果使用了烘干机则每分钟流失K个单位水分。令人遗憾是HSQ所在的宿舍楼只有1台烘干机,而每台烘干机同时只能烘干1件衣服,请问要想烘干N件衣服最少需要多长时间?Input第一行输入N,表示有N件衣服,第二行输入N件衣服的水分ai...原创 2019-09-24 15:34:01 · 1142 阅读 · 0 评论 -
【题解】codeforce1230 D. Marcin and Training Camp⭐⭐⭐ 【位运算 贪心】
D. Marcin and Training CampMarcin is a coach in his university. There are n students who want to attend a training camp. Marcin is a smart coach, so he wants to send only the students that can work c...原创 2019-09-25 17:50:47 · 936 阅读 · 0 评论 -
【题解】codeforce1228 D. Complete Tripartite⭐⭐⭐ 【构造】
codeforce1228 D. Complete TripartiteYou have a simple undirected graph consisting of n vertices and m edges. The graph doesn’t contain self-loops, there is at most one edge between a pair of vertices...原创 2019-10-07 15:05:28 · 610 阅读 · 0 评论 -
母牛的故事 HDU - 2018 (递推 | 思维水题)
https://vjudge.net/problem/HDU-2018按理说这应该就是一道水题, 可最近实在对递推的理解有些生疏了, 考虑了很久一直在WA, 其实就是一个变种般的斐波那契数列分析 根据题意,先列出前几年的牛头数,试着找找规律: 在列出这个序列的过程中,应当能找出规律。 以n=6为例,fn=9头牛可以分解为6+3,其中6是上一年(第5年)的牛,3是新生的...原创 2018-10-25 14:47:42 · 1273 阅读 · 0 评论 -
Poj百练 4110:圣诞老人的礼物-Santa Clau’s Gifts (分类:贪心)
#include <iostream>#include<cstdio>#include<algorithm>using namespace std;const int maxn = 100+5;int n, w;struct Box{ int v, w; double den;};bool operator < (co...原创 2018-08-04 22:02:52 · 1058 阅读 · 0 评论 -
Poj百练 3468:电池的寿命 (分类:贪心)
对于贪心问题,不要太关注过程或者方法,而应该关注结果的数据。一般来讲,贪心问题至少要讨论两种情况。 这道题点关注点就不在什么时候换电池,如何换电池,而在于电池到低能用多久。讨论可分为两种情况:1.最大的电池寿命大于其他电池寿命之和 时间=其他电池寿命之和2.最大的电池寿命小于等于其他电池寿命之和时间=总和/2 想明白就很简单啦!//电池的寿命 Open...原创 2018-08-04 23:32:50 · 2050 阅读 · 0 评论 -
Tian Ji -- The Horse Racing POJ - 2287(贪心)
https://vjudge.net/problem/POJ-2287田忌赛马的故事大家都听过吧,仔细分析下本题思路大致相同1.田忌最快的马大于齐王最快的马,直接比之2.田忌最快的马小于齐王最快的马,拿田忌最慢的马比之3.田忌最快的马和齐王最快的马速度相等,注意此时要分类讨论 1.田忌最慢的马大于齐王最慢的马,比之 ...原创 2018-09-03 10:35:43 · 399 阅读 · 0 评论 -
001:特殊密码锁(贪心)
http://cxsjsxmooc.openjudge.cn/2018t2winterw1/001/这道题不难,可以枚举来写,每个锁两种状态一共30个锁2^30的复杂度可能不会超,在此提供另外一种贪心的思路。对于第一个锁,我们枚举(好吧算不上枚举)开或不开两种情况。如果第一个锁不为目标状态,那么只有第二个锁才能关掉它。。。依次类推,如果第i个锁不为目标状态,那么只有第i+1个锁才能关掉它...原创 2018-09-05 19:54:22 · 1051 阅读 · 1 评论 -
Radar Installation POJ - 1328 (区间贪心)
https://vjudge.net/problem/POJ-1328一道比较基础的贪心题目, 比较巧妙的是题中用到了区间贪心的算法.起初看题的时候一直有一个误区, 总想通过分析雷达关于岛的位置来确定范围, 后来发现其实加上一点儿逆向思维, 通过考虑岛来确定雷达的范围其实会更简单具体来说, 考虑一下每个小岛被覆盖时雷达的区间, 最后就把各个小岛转化为了一个个区间, 我们的任务就是求最少...原创 2018-10-11 20:14:33 · 1257 阅读 · 2 评论 -
金银岛 OpenJ_Bailian - 2795 (贪心)
https://vjudge.net/problem/OpenJ_Bailian-2795贪心水题. 不解释//金银岛 贪心#include<cstdio>#include<algorithm>#include<cstring>using namespace std;const int maxn = 10010;int w, s, k;s...原创 2018-10-11 21:28:32 · 872 阅读 · 0 评论 -
拼点游戏 OpenJ_Bailian - 4005 (贪心)
https://vjudge.net/problem/OpenJ_Bailian-4005这道题目和田忌赛马问题极为类似, 本质思想都是: 如果我最好的比不过你, 就拿我的最差的比你最好的, 再拿我最好的比你其次好的 (1)如果B的大点数牌能赢A的大点数牌,则进行比较,否则执行(2); (2)如果B的小点数牌能赢A的小点数牌,则进行比较,否则执行(3);...原创 2018-10-18 15:37:13 · 1479 阅读 · 0 评论 -
完美字符串 51Nod - 1182 (贪心水题)
https://vjudge.net/problem/51Nod-1182#include<cstdio>#include<iostream>#include <string>#include <cstring>#include<algorithm>using namespace std;string s;str...原创 2018-10-18 18:03:14 · 545 阅读 · 0 评论 -
独木舟 51Nod - 1432 (贪心)
https://vjudge.net/problem/51Nod-1432很明显的贪心算法, 如果能带最小的走则带走, 否则自己走即可开始那道题的时候有一个误区, 总想着能够要带能带走的最大的走, 这个其实无所谓, 因为一条船只能坐两个人, 只要坐上两个人就算是值得了, 不考虑空间问题. 而如果我这样写就又会变成n^2复杂度//独木舟 贪心#include<cstdio&...原创 2018-10-18 22:18:22 · 497 阅读 · 0 评论 -
最小新整数 OpenJ_Bailian - 4137 (贪心)
https://vjudge.net/problem/OpenJ_Bailian-4137一道基础的贪心问题, 开始为算法还纠结了好一会, 其实想明白就好了1.每次都删去位数更大且大于大于右边的数2.如果依然k>0, 删去最大的数3.如果依然k>0, 依次删(也就是所有数都相等的情况)//Bookshelf#include<cstdio>#in...原创 2018-10-16 21:01:29 · 634 阅读 · 1 评论 -
Best Cow Line POJ - 3617 (贪心)
https://vjudge.net/problem/POJ-3617很简单的贪心水题,(2) 如果相等, 再向后/前, 判断(1)(1)每次都取两边最小的注意仔细读取, 题中还有一个要求每输够80次多输出一个换行, 所以说英文真的很重要#include <iostream>#include <cstdio>#include <strin...原创 2018-10-22 18:22:11 · 784 阅读 · 2 评论 -
Too Rich HDU - 5527 (贪心+思维)
这道题看起来比较像经典的货币问题,贪心可解。其实仔细观察的会发现20-50, 200-500之间不存在倍数关系,所以没有办法直观的用贪心来解。假如在某个例子中 只剩下了一张50 三张20 此时差为60,按照贪心算法的话 你会直接拿50 还剩的10你就拿不出来了我们这时去凑出来倍数就好了,比如放两个50就等于放一个100,也就符合了倍数关系。注意分类讨论。//too rich...原创 2018-08-30 19:14:45 · 563 阅读 · 0 评论 -
nyoj 1036-非洲小孩 (区间贪心)
http://nyoj.top/problem/1036这是一道区间贪心问题中的区间选点问题, 即给定部分区间, 选得最少的点, 使得每个区间至少有一个点(含重叠)应该来说有两种算法一是左端点排序(<), 考虑右端点(<), 然后求交集的右边界, 没有交集就ans+1//非洲的小孩 贪心#include<cstdio>#include<ios...原创 2018-10-25 20:42:26 · 515 阅读 · 0 评论 -
Saruman's Army POJ - 3069 (区间贪心)
https://vjudge.net/problem/POJ-3069简单总结一下关于区间贪心问题区间贪心一、线段覆盖n个开区间(ai,bi),选择尽量多个区间,使得这些区间两两不相交右端点排序(<)兼顾左端点(>),再从左到右遇到不相交的就选 sort(he+1,he+n+1,cmp); int tot=0,now=-1000; for(int i=...原创 2018-10-23 14:51:23 · 991 阅读 · 1 评论 -
Fence Repair POJ - 3253 (单调队列 贪心)
https://vjudge.net/problem/POJ-3253贪心复杂度n^2, 单调队列nlogn每次取出最小的两个点, 和再入队即可 #include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#include <str...原创 2018-10-23 19:25:52 · 870 阅读 · 0 评论
分享