![](https://img-blog.csdnimg.cn/20201014180756926.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
贪心
贪心
Ypopstar
这个作者很懒,什么都没留下…
展开
-
LeetCode--561--数组拆分 I
题目描述:给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), …, (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最大。输入:[1,4,3,2]输出:4题意:题目描述题解:简单贪心排序后,两两找最小值代码:class Solution { public int arrayPairSum(int[] nums) { int sum = 0; Arrays.sort(n原创 2020-07-08 16:45:42 · 120 阅读 · 0 评论 -
LeetCode--55--跳跃游戏
题目描述:给定一个非负整数数组,你最初位于数组的第一个位置。数组中的每个元素代表你在该位置可以跳跃的最大长度。判断你是否能够到达最后一个位置。输入:[2,3,1,1,4][2,3,1,1,4]输出:truefalse题意:题目描述题解:贪心这样以来,我们依次遍历数组中的每一个位置,并实时维护 最远可以到达的位置。对于当前遍历到的位置 xx,如果它在 最远可以到达的位置 的范围内,那么我们就可以从起点通过若干次跳跃到达该位置,因此我们可以用x+nums[x] 更新 最远可以到达的位原创 2020-06-22 21:54:40 · 137 阅读 · 0 评论 -
LeetCode--1014--最佳观光组合
题目描述:给定正整数数组 A,A[i] 表示第 i 个观光景点的评分,并且两个景点 i 和 j 之间的距离为 j - i。一对景点(i < j)组成的观光组合的得分为(A[i] + A[j] + i - j):景点的评分之和减去它们两者之间的距离。返回一对观光景点能取得的最高分。输入:[8,1,5,2,6]输出:11题意:题目描述题解:边遍历边维护我们考虑从前往后枚举 jj 来统计答案,对于每个观光景点 j 而言,我们需要遍历 [0,j−1] 的观光景点 ii 来计算组成观光组原创 2020-06-17 21:52:05 · 127 阅读 · 0 评论 -
LeetCode--1402--做菜顺序
题目描述:一个厨师收集了他 n 道菜的满意程度 satisfaction ,这个厨师做出每道菜的时间都是 1 单位时间。一道菜的 「喜爱时间」系数定义为烹饪这道菜以及之前每道菜所花费的时间乘以这道菜的满意程度,也就是 time[i]*satisfaction[i] 。请你返回做完所有菜 「喜爱时间」总和的最大值为多少。你可以按 任意 顺序安排做菜的顺序,你也可以选择放弃做某些菜来获得更大的总和。输入:satisfaction = [-1,-8,0,5,-9]satisfaction = [4,原创 2020-06-17 21:44:07 · 221 阅读 · 0 评论 -
LeetCode--1323--6 和 9 组成的最大数字
题目描述:给你一个仅由数字 6 和 9 组成的正整数 num。你最多只能翻转一位数字,将 6 变成 9,或者把 9 变成 6 。请返回你可以得到的最大数字。输入:966999969999输出:996999999999题意:题目描述题解:贪心的去找,去找第一个6然后变成9代码:class Solution { public int maximum69Number (int num) { String number = String.valueOf(nu原创 2020-06-15 21:25:20 · 124 阅读 · 0 评论 -
LeetCode--LCP 06--拿硬币
题目描述:桌上有 n 堆力扣币,每堆的数量保存在数组 coins 中。我们每次可以选择任意一堆,拿走其中的一枚或者两枚,求拿完所有力扣币的最少次数。输入:[4,2,1][2,3,10]输出:48题意:题目描述题解:简单贪心只有两种操作,要么拿一个要么拿两个对于每一个数,大于等于2的时候全部按照2拿,最优代码:class Solution { public int minCount(int[] coins) { int ans = 0; i原创 2020-06-14 21:09:29 · 185 阅读 · 0 评论 -
牛客网--15156--小H和迷宫
题目描述:小H陷入了一个迷宫中,迷宫里有一个可怕的怪兽,血量有N点,小H有三瓶魔法药水,分别可以使怪兽损失a%、b%、c%的血量(之后怪兽的血量会向下取整),小H想合理地运用这三瓶药水,使怪兽失去尽可能多的血量注意:每瓶药水只能用一次输入描述:一行,四个整数 N,a,b,c输出描述:一行,一个整数w,表示最多能使得怪兽扣减多少血量输入:100 20 15 0输出:32题意:题目描述题解:简单贪心,从大的找起代码:#include<cstdio>#include&原创 2020-06-02 21:35:16 · 286 阅读 · 0 评论 -
牛客网--19784--Shopping
题目描述:你要买n件物品,其中有一些是凳子。商场正在举行促销活动,如果购物车中有至少一个凳子,那么你可以半价购买这个购物车中最贵的一个物品。你有m辆购物车,请最小化你的花费。输入描述:第一行一个整数t表示数据组数 (1 ≤ t ≤ 100)。每组数据第一行两个整数n,m (1 ≤ n,m ≤ 1000),接下来n行每行两个整数ai,bi,分别表示第i件物品的价格以及它是否是凳子 (1 ≤ ai ≤ 105, 0 ≤ bi ≤ 1)。输出描述:每组数据输出一行一个实数表示最小花费,保留一位小数原创 2020-06-02 21:33:54 · 339 阅读 · 0 评论 -
牛客网--207424--赛马
题目描述:一天小明与他同学准备赛马,他们每人有n匹马,每匹马有一个固定的战力值,战力值高的马会战胜战力值低的马并赢得比赛。每匹马只能出场比赛一次。小明偷看到了他对手每匹马的出场顺序,小明在更改自己马出场顺序后最多能赢多少场比赛。输入描述:输入t,代表有t组数据。每组数据输入正整数n,每人的马匹数量。下一行输入n个值a[i],代表小明每匹马的战力值。接下来一行输入n个值b[i],代表对手按顺序出场的每匹马的战力值。(t<=10, n<1000,1<=i<=n,a[i]<1e原创 2020-05-31 20:36:09 · 366 阅读 · 1 评论 -
HihoCoder--1051--补提交卡
题目描述:小Ho给自己定了一个宏伟的目标:连续100天每天坚持在hihoCoder上提交一个程序。100天过去了,小Ho查看自己的提交记录发现有N天因为贪玩忘记提交了。于是小Ho软磨硬泡、强忍着小Hi鄙视的眼神从小Hi那里要来M张"补提交卡"。每张"补提交卡"都可以补回一天的提交,将原本没有提交程序的一天变成有提交程序的一天。小Ho想知道通过利用这M张补提交卡,可以使自己的"最长连续提交天数"最多变成多少天。输入描述:第一行是一个整数T(1 <= T <= 10),代表测试数据的组数。每原创 2020-05-26 22:54:06 · 216 阅读 · 0 评论 -
Codeforces--626C--Block Towers
题目描述:Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks.The stud原创 2020-05-25 18:40:04 · 244 阅读 · 0 评论 -
HDOJ--5240--Exam
题目描述:As this term is going to end, DRD needs to prepare for his final exams.DRD has n exams. They are all hard, but their difficulties are different. DRD will spend at least ri hours on the i-th course before its exam starts, or he will fail it. The i-th原创 2020-05-18 19:16:03 · 190 阅读 · 0 评论 -
HDOJ--2570--迷瘴
题目描述:通过悬崖的yifenfei,又面临着幽谷的考验——幽谷周围瘴气弥漫,静的可怕,隐约可见地上堆满了骷髅。由于此处长年不见天日,导致空气中布满了毒素,一旦吸入体内,便会全身溃烂而死。幸好yifenfei早有防备,提前备好了解药材料(各种浓度的万能药水)。现在只需按照配置成不同比例的浓度。现已知yifenfei随身携带有n种浓度的万能药水,体积V都相同,浓度则分别为Pi%。并且知道,针对当时幽谷的瘴气情况,只需选择部分或者全部的万能药水,然后配置出浓度不大于 W%的药水即可解毒。现在的问题是:原创 2020-05-12 17:22:59 · 112 阅读 · 0 评论 -
HDOJ--2111--Saving HDU
题目描述:话说上回讲到海东集团面临内外交困,公司的元老也只剩下XHD夫妇二人了。显然,作为多年拼搏的商人,XHD不会坐以待毙的。一天,当他正在苦思冥想解困良策的时候,突然想到了自己的传家宝,那是公司成立的时候,父亲作为贺礼送来的一个锦囊,徐父当时交代,不到万不得已的时候,不要打开它。“现在不正是最需要的时候吗?”,一边想,XHD一边找到了这个精心保管的锦囊,打开一看,里面只有一句话“杭城北麓千人洞有宝”。二话不说,XHD拿起一个大口袋就出发了,这个千人洞他是知道的,小的时候,爸爸曾经带他来过这个隐蔽的原创 2020-05-12 17:14:20 · 148 阅读 · 0 评论 -
POJ--3253--Fence Repair
题目描述:Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1...原创 2020-04-29 18:56:17 · 141 阅读 · 0 评论 -
Codeforces--967B--Watering System
题目描述:Arkady wants to water his only flower. Unfortunately, he has a very poor watering system that was designed for n flowers and so it looks like a pipe with n holes. Arkady can only use the water t...原创 2020-04-16 23:06:15 · 150 阅读 · 0 评论 -
POJ--2431--Expedition
题目描述:A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to run over a rock and puncture the truck’s fuel tan...原创 2020-04-14 22:54:06 · 177 阅读 · 0 评论 -
牛客网--14407--甄总搬石头
题目描述:现在草地上有n堆石头,甄总想要合并这n堆石头成为1堆,但是他每次能力有限,所以只能一次合并2堆石头至1堆。现在已知第i堆石头有ai块,假设甄总要合并第i堆和第j堆石头,则需要花费ai+aj的力气。给出n堆石头每堆石头的个数,求出甄总要合并n堆成1堆石头一共需要多少力气。输入描述:第1行输入一个整数n,代表一共有n堆石头。第2行输入n个整数ai,表示第i堆有ai块石头。输出描...原创 2020-04-13 22:37:39 · 334 阅读 · 0 评论 -
HDOJ--1789--Doing Homework again
题目描述:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework af...原创 2020-04-02 15:59:51 · 120 阅读 · 0 评论 -
HDOJ--1052--Tian Ji -- The Horse Racing
题目描述:Here is a famous story in Chinese history.“That was about 2300 years ago. General Tian Ji was a high official in the country Qi. He likes to play horse racing with the king and others.”“Both o...原创 2020-04-02 15:16:49 · 176 阅读 · 0 评论 -
HDOJ--5500--Reorder the Books
题目描述:dxy has a collection of a series of books called “The Stories of SDOI”,There are n(n≤19) books in this series.Every book has a number from 1 to n.dxy puts these books in a book stack with the o...原创 2020-04-01 16:28:01 · 138 阅读 · 0 评论 -
HDOJ--3552--I can do it!
题目描述:Given n elements, which have two properties, say Property A and Property B. For convenience, we use two integers A i and B i to measure the two properties.Your task is, to partition the element...原创 2020-03-30 22:29:18 · 167 阅读 · 0 评论 -
牛客网--20865--兔子的序列
题目描述:兔子发现了一个数字序列,于是开始研究这个序列。兔子觉得一个序列应该需要有一个命名,命名应该要与这个序列有关。由于兔子十分讨厌完全平方数,所以兔子开创了一个新的命名方式:这个序列中最大的不是完全平方数的数字就是他的名字。现在兔子有一个序列,想要知道这个序列的名字是什么。输入描述:第一行一个整数 n,表示序列的长度。第二行有 n 个整数 ai,表示序列中的 n 个数分别是多少。输...原创 2020-03-28 18:58:26 · 475 阅读 · 0 评论 -
牛客--2019网易--牛牛的闹钟
题目描述:牛牛总是睡过头,所以他定了很多闹钟,只有在闹钟响的时候他才会醒过来并且决定起不起床。从他起床算起他需要X分钟到达教室,上课时间为当天的A时B分,请问他最晚可以什么时间起床输入描述:每个输入包含一个测试用例。每个测试用例的第一行包含一个正整数,表示闹钟的数量N(N<=100)。接下来的N行每行包含两个整数,表示这个闹钟响起的时间为Hi(0<=A<24)时Mi(0...原创 2020-03-27 17:32:41 · 138 阅读 · 1 评论 -
牛客--2019拼多多--两两配对差值最小
题目描述:给定一个长度为偶数的数组arr,将该数组中的数字两两配对并求和,在这些和中选出最大和最小值,请问该如何两两配对,才能让最大值和最小值的差值最小?输入描述:一共2行输入。第一行为一个整数n,2<=n<=10000, 第二行为n个数,组成目标数组,每个数大于等于2,小于等于100。输出描述:输出最小的差值。输入:42 6 4 3611 4 3 5 7 1输...原创 2020-03-23 17:40:07 · 240 阅读 · 0 评论 -
Codeforces--1106C--Lunar New Year and Number Division
题目描述:Lunar New Year is approaching, and Bob is struggling with his homework – a number division problem.There are n positive integers a1,a2,…,an on Bob’s homework paper, where n is always an even nu...原创 2020-03-22 09:10:10 · 313 阅读 · 0 评论 -
Codeforces--1176B--Merge it!
题目描述:You are given an array a consisting of n integers a1,a2,…,an.In one operation you can choose two elements of the array and replace them with the element equal to their sum (it does not matter w...原创 2020-03-21 13:12:40 · 239 阅读 · 1 评论 -
Codeforces--1271A--Suits
题目描述:A new delivery of clothing has arrived today to the clothing store. This delivery consists of a ties, b scarves, c vests and d jackets.The store does not sell single clothing items — instead, i...原创 2020-03-20 19:04:04 · 381 阅读 · 0 评论 -
Codeforces--785B--Anton and Classes
题目描述:Anton likes to play chess. Also he likes to do programming. No wonder that he decided to attend chess classes and programming classes.Anton has n variants when he will attend chess classes, i-t...原创 2020-03-19 16:35:01 · 160 阅读 · 0 评论 -
Codeforces--424B--Megacity
题目描述:The administration of the Tomsk Region firmly believes that it’s time to become a megacity (that is, get population of one million). Instead of improving the demographic situation, they decided ...原创 2020-03-19 09:54:09 · 183 阅读 · 0 评论 -
Codeforces--264A--Escape from Stones
题目描述:Squirrel Liss lived in a forest peacefully, but unexpected trouble happens. Stones fall from a mountain. Initially Squirrel Liss occupies an interval [0, 1]. Next, n stones will fall and Liss wi...原创 2020-03-18 20:13:30 · 179 阅读 · 0 评论 -
HDOJ--5969--最大的位或
题目描述:B君和G君聊天的时候想到了如下的问题。给定自然数l和r ,选取2个整数x,y满足l <= x <= y <= r ,使得x|y最大。其中|表示按位或,即C、 C++、 Java中的|运算。输入描述:包含至多10001组测试数据。第一行有一个正整数,表示数据的组数。接下来每一行表示一组数据,包含两个整数l,r。保证 0 <= l <= r &l...原创 2020-03-18 19:42:24 · 132 阅读 · 0 评论 -
Codeforces--719B--Anatoly and Cockroaches
题目描述:Anatoly lives in the university dorm as many other students do. As you know, cockroaches are also living there together with students. Cockroaches might be of two colors: black and red. There ar...原创 2020-03-17 21:07:00 · 105 阅读 · 0 评论 -
HDOJ--5112--A Curious Matt
题目描述:There is a curious man called Matt.One day, Matt’s best friend Ted is wandering on the non-negative half of the number line. Matt finds it interesting to know the maximal speed Ted may reach. I...原创 2020-03-17 10:50:55 · 118 阅读 · 0 评论 -
Codeforces--760B--Frodo and pillows
题目描述:n hobbits are planning to spend the night at Frodo’s house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone ...原创 2020-03-11 17:10:06 · 294 阅读 · 0 评论 -
POJ--1017--Packets
题目描述:A factory produces products packed in square packets of the same height h and of the sizes 11, 22, 33, 44, 55, 66. These products are always delivered to customers in the square parcels of the s...原创 2020-03-10 22:20:02 · 202 阅读 · 0 评论 -
计蒜客--T1878--丢瓶盖
题目描述:陶陶是个贪玩的孩子,他在地上丢了A个瓶盖,为了简化问题,我们可以当作这A个瓶盖丢在一条直线上,现在他想从这些瓶盖里找出B个,使得距离最近的2个距离最大,他想知道,最大可以到多少呢?输入描述:第一行,两个整数,A,B。(B<=A<=100000)第二行,A个整数,分别为这A个瓶盖坐标。输出描述:仅一个整数,为所求答案。输入:5 31 2 3 4 5输出:2...原创 2020-03-06 14:06:44 · 212 阅读 · 0 评论 -
计蒜客--T1811--奥利凡德
题目描述:蒜头君来到蘑菇森林,这里有 n 只僵尸蘑菇,每只僵尸蘑菇的闪避值为 xi,血量为 yi。只有蒜头君的命中值大于等于怪物的闪避值,才能对怪物造成伤害。蒜头君一共有 mm 点能量值,他每次攻击会消耗一点能量,然后造成一点伤害(单体攻击,某个怪物血量减少 1)。现在已知蒜头君的基础命中值为 h,身上装备增加的命中值为 b。现在蒜头君他想知道一共能杀死多少个僵尸蘑菇。输入描述:第一行...原创 2020-03-06 13:42:45 · 666 阅读 · 0 评论 -
计蒜客--T1987--田忌赛马
题目描述:赛马是一古老的游戏,早在公元前四世纪的中国,处在诸侯割据的状态,历史上称为 “战国时期”。在魏国作官的孙膑,因为受到同僚庞涓的迫害,被齐国使臣救出后,到达齐国国都。赛马是当时最受齐国贵族欢迎的娱乐项目。上至国王,下到大臣,常常以赛马取乐,并以重金赌输赢。田忌多次与国王及其他大臣赌输赢,屡赌屡输。一天他赛马又输了,回家后闷闷不乐。孙膑安慰他说:“下次有机会带我到马场看看,也许我能帮你。...原创 2020-03-06 13:26:32 · 409 阅读 · 0 评论 -
计蒜客--T1811--奥利凡德
题目描述:哈利波特在与伏地魔的战斗中毁坏了自己的魔杖,于是他决定去奥利凡德的魔杖店买个新的。他在店里看到 nn 个魔杖和 nn 个盒子,每个魔杖的长度为 X1,X2…Xn,每个盒子的长度为 Y1,Y2,…,Yn。一个长度为 X 的魔杖能放进长度为 Y 的盒子里只有满足 X≤Y。哈利想知道他能否把所有魔杖都放进盒子里,并且每个盒子只能放一根魔杖。请你帮他解决这个问题。输入描述:第一行...原创 2020-03-05 20:52:01 · 734 阅读 · 0 评论