![](https://img-blog.csdnimg.cn/20201014180756916.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
贪心
文章平均质量分 80
ModestCoder_
一个modest的coder
展开
-
【题解】2020.11.2 B
高位贪心,逐位确定这个肯定是套路大方针对于每一个新来的,之前的答案肯定要满足然后用O(n2)O(n^2)O(n2)dp暴力验证时间复杂度O(qn2log2n)O(qn^2log^2n)O(qn2log2n)差不多要3.6s,很接近了,按照一般出题者的思路,接下来再优化一下首先O(n2)O(n^2)O(n2)跑不满,差不多可以少掉一半时间然后加一些inline,registerinline,registerinline,register之类的极限数据最多只要0.7sCode:#prag..原创 2020-11-03 10:19:05 · 119 阅读 · 0 评论 -
【题解】AT4520:[AGC032E] Modulo Pairing
原题传送门首先想到的当然是二分答案+贪心但是正解二分的并不是答案结论:最终的正确答案一定存在一个分界点,满足蓝线表示x+y<Mx+y<Mx+y<M红线表示x+y>=Mx+y>=Mx+y>=M给出证明情况1:任意两个数加起来都<M<M<M的情况,Ⅰ最优令从左到右分别为a<b<c<da<b<c<da<b<c<d计算出三种方案的价值Ⅰ:max(a+d,b+c)max(a+d,b+c原创 2020-11-01 11:32:11 · 216 阅读 · 0 评论 -
【题解】AT4439:[AGC028E] High Elements
原题传送门这题很难因为要求输出的字典序最小,所以0优先想到逐位确定的贪心思想:从第1位开始,先假设这一位是0,就把这个数放到xxx序列中,验证一下之后的数能否再合法,如果可以,就把这个数放到xxx序列,否则就只能放到yyy序列逐位确定的复杂度是O(n)O(n)O(n)所以要求我们在O(logn)O(logn)O(logn)时间内解决checkcheckcheck问题先定义原序列中的上升位为“原上升位”,到新序列新产生的上升位(即本来不是上升位变成了上升位)为“新上升位”结论1:原上升位到新的序原创 2020-11-01 11:00:45 · 213 阅读 · 0 评论 -
【题解】CF1154D:Walking Robot
原题传送门贪心模拟分类讨论如果蓄电池没电了,那么肯定走干电池,如果走到一条1线段,蓄电池电量可以+1如果蓄电池还有电,那么如果干电池没电,或者下一条线段为0,又或者蓄电池目前满电,走蓄电池,否则才走干电池Code:#include <bits/stdc++.h>#define maxn 200010using namespace std;int n, a, b, c;inline int read(){ int s = 0, w = 1; char c = getc原创 2020-10-20 16:30:56 · 125 阅读 · 0 评论 -
【题解】CF1157B:Long Number
原题传送门根据字典序的方便性质找到第一个可以更优的就换换到第一个会更劣的为止Code:#include <bits/stdc++.h>#define maxn 200010using namespace std;int n, a[maxn], b[maxn];inline int read(){ int s = 0, w = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') w原创 2020-10-19 08:54:54 · 217 阅读 · 0 评论 -
【题解】CF1131C:Birthday
原题传送门套路贪心先排序,成为一个有序数列a1,a2,...,ana_1,a_2,...,a_na1,a2,...,an像这样子摆放,如何证明正确性呢首先可以感性得知,如果是最优解,那么任意交换两个数的位置肯定比最优解劣那么如果一种方案可以使得无法交换两个数来更优,那么这种方案是不是最优的,我觉得是的所以可以任意交换,比如交换a4,a6a4,a6a4,a6,那么这一部分的最大值从max(a8−a6,a6−a4,a4−a2)max(a8-a6,a6-a4,a4-a2)max(a8−a6,a原创 2020-10-18 13:56:03 · 138 阅读 · 0 评论 -
【题解】CF1031C:Cram Time
原题传送门贪心,能用小的不用大的满足∑i=1ni<=a+b\sum_{i=1}^{n}i<=a+b∑i=1ni<=a+b最大的iii,一定可以用完1−n1-n1−n对于aaa,直接从大到小枚举是否可以看,剩下的给bbbCode:#include <bits/stdc++.h>#define LL long longusing namespace std;LL a, b;int flag[1000010];int main(){ scanf("%lld%原创 2020-09-01 21:11:27 · 222 阅读 · 0 评论 -
【题解】CF1085D:Minimum Diameter Tree
原题传送门贪心,只给与叶子结点相连的边赋值,平均分假设有cntcntcnt个叶子,每条叶子边的权值为scnt\frac{s}{cnt}cnts答案是2scnt2\frac{s}{cnt}2cntsCode:#include <bits/stdc++.h>#define maxn 100010using namespace std;double s, cnt;int n, d[maxn];inline int read(){ int s = 0, w = 1; cha原创 2020-08-31 21:29:21 · 188 阅读 · 0 评论 -
【题解】LuoGu1792:[国家集训队]种树
原题传送门在这道题的基础上变成了一个环Code:#include <bits/stdc++.h>#define maxn 500010#define LL long longusing namespace std;struct heap{ int num; LL val; bool operator < (const heap &x) const{return x.val > val;}};priority_queue <heap> q;原创 2020-08-20 15:38:02 · 177 阅读 · 0 评论 -
【题解】LuoGu1484:种树
原题传送门如果是O(n2)DPO(n^2)DPO(n2)DP,直接dpi,j=max(dpi−1,j,dpi−2,j−1+ai)dp_{i,j}=max(dp_{i-1,j},dp_{i-2,j-1}+a_i)dpi,j=max(dpi−1,j,dpi−2,j−1+ai)但是不行,所以可以反悔贪心所以跟这道题目差不多Code:#include <bits/stdc++.h>#define maxn 500010#define LL long longusing names原创 2020-08-19 15:31:46 · 157 阅读 · 0 评论 -
【题解】LuoGu4053:[JSOI2007]建筑抢修
原题传送门一个简单的反悔贪心按照报废时间排序对于当前的建筑,如果可以修,就修;否则在之前已经选择修的建筑中选择耗时最长的跟自己比,如果自己耗时比那个短,并且不修那个改修自己可以在承受范围内,就反悔Code:#include <bits/stdc++.h>#define maxn 150010using namespace std;struct node{ int x, y;}a[maxn];int n;priority_queue <int> q;inl原创 2020-08-14 15:40:09 · 145 阅读 · 0 评论 -
【学习笔记】反悔贪心
最近做到了两道题,有同一种思想方法,我称之为“反悔贪心”先放我的两只题解LuoGu3620: [APIO/CTSC 2007]数据备份LuoGu4597:序列sequence到底什么是反悔贪心呢,首先是贪心,然而贪心并不是最优的,我们这是需要增添一个反悔机制来进行优化这个贪心比如我放的第一题,贪心策略就是每次取最小的,但是可能不是最优的,所以引入反悔贪心,把某个值AAA取出后,加入B−A...原创 2019-12-17 20:26:59 · 2181 阅读 · 0 评论 -
【题解】CF875E:Delivery Club
原题传送门二分答案+贪心对于每个midmidmid,需要倒着做,贪心我们记当前其中一个快递员在xi+1x_{i+1}xi+1,另一个快递员在[l,r][l,r][l,r],当前那么分类讨论...原创 2019-08-12 19:07:22 · 445 阅读 · 0 评论 -
【题解】慈溪中学-8.15-T1
传送门二分,枚举答案如果验证是否能给mid个充电器都充电那当然是给更安全的充电器充电然后对于每个充电器是否能充电,总是先尽可能插插线板,然后充电Code:#include <bits/stdc++.h>#define maxn 400010#define LL long longusing namespace std;int n, m, a[maxn], b[max...原创 2019-08-16 08:05:04 · 217 阅读 · 0 评论 -
【题解】CF1110B:Tape
原题传送门You have a long stick, consisting of mm segments enumerated from 11 to mm. Each segment is 11 centimeter long. Sadly, some segments are broken and need to be repaired.You have an infinitely lon...原创 2019-02-08 17:03:26 · 541 阅读 · 0 评论 -
【题解】LuoGu3052:[USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
原题传送门 题目描述A little known fact about Bessie and friends is that they love stair climbing races. A better known fact is that cows really don’t like going down stairs. So after the cows finish racing ...原创 2018-08-23 17:12:00 · 316 阅读 · 0 评论 -
【题解】贪心二题
晚上刷了两道贪心题,难度不大,我的码力也有进步LuoGu1233木棍加工 题目描述一堆木头棍子共有n根,每根棍子的长度和宽度都是已知的。棍子可以被一台机器一个接一个地加工。机器处理一根棍子之前需要准备时间。准备时间是这样定义的:第一根棍子的准备时间为1分钟;如果刚处理完长度为L,宽度为W的棍子,那么如果下一个棍子长度为Li,宽度为Wi,并且满足L>=Li,W>=Wi,...原创 2018-08-12 20:59:38 · 592 阅读 · 0 评论 -
【题解】codeforces725D Contest Balloons
One tradition of ACM-ICPC contests is that a team gets a balloon for every solved problem. We assume that the submission time doesn’t matter and teams are sorted only by the number of balloons they ha...原创 2018-08-05 09:36:27 · 348 阅读 · 0 评论