greedy

Longest K Sequential Increasing Subsequences

最后都变了- 提交于 2020-01-12 05:26:09
问题 Why I created a duplicate thread I created this thread after reading Longest increasing subsequence with K exceptions allowed. I realised that the person who was asking the question hadn't really understood the problem, because he was referring to a link which solves the "Longest Increasing sub-array with one change allowed" problem. So the answers he got were actually irrelevant to LIS problem. Description of the problem Suppose that an array A is given with length N . Find the longest

Segments poked (covered) with points - any tricky test cases?

我的梦境 提交于 2020-01-07 04:59:10
问题 In this question I would like to ask you to hack/break my code because I can't think of any test cases it doesn't pass, however, it does not pass the grader. So here's the problem description: a set of n segments [l, r] on the line, l <= r. We need to find the minimum number of points such that each segments has at least one point and print the set of such points. If there are multiple versions of the set, print any. As you see, here we have points 'poking' segments, not segments covering

Maximum Value taken by thief

血红的双手。 提交于 2020-01-03 05:13:30
问题 Consider we have a sacks of gold and thief wants to get the maximum gold. Thief can take the gold to get maximum by, 1) Taking the Gold from contiguous sacks. 2) Thief should take the same amount of gold from all sacks. N Sacks 1 <= N <= 1000 M quantity of Gold 0 <= M <= 100 Sample Input1: 3 0 5 4 4 4 Output: 16 Explanation: 4 is the minimum amount he can take from the sacks 3 to 6 to get the maximum value of 16. Sample Input2: 2 4 3 2 1 Output: 8 Explanation: 2 is the minimum amount he can

Greedy Algorithm Implementation

自闭症网瘾萝莉.ら 提交于 2020-01-01 09:49:12
问题 You know who knows who among n people that you would like to have come to a party. Assume "knows" is symmetric: If I know you, you know me. You make further requirements that you want each person to have at least 5 new people to meet at the party, and also, so nobody feels too isolated, each person should already know at least 5 people at the party. Your original list may not satisfy these extra two conditions, so you may need to eliminate some people from the invitation list (or maybe you

How can I fix my regex to not match too much with a greedy quantifier? [duplicate]

假装没事ソ 提交于 2019-12-30 03:29:11
问题 This question already has answers here : My regex is matching too much. How do I make it stop? (5 answers) Closed 10 months ago . I have the following line: "14:48 say;0ed673079715c343281355c2a1fde843;2;laka;hello ;)" I parse this by using a simple regexp: if($line =~ /(\d+:\d+)\ssay;(.*);(.*);(.*);(.*)/) { my($ts, $hash, $pid, $handle, $quote) = ($1, $2, $3, $4, $5); } But the ; at the end messes things up and I don't know why. Shouldn't the greedy operator handle "everything"? 回答1: The

Minimizing weighted sum

烈酒焚心 提交于 2019-12-29 03:33:18
问题 I came across this problem quite recently. Suppose there are n points on x-axis, x[0],x[1] .. x[n-1]. Let the weight associated with each of these points be w[0],w[1] .. w[n-1]. Starting from any point between 0 to n-1, the objective is to cover all the points such that the sum of w[i]*d[i] is minimized where d[i] is the distance covered to reach the ith point from the starting point. Example: Suppose the points are: 1 5 10 20 40 Suppose the weights are: 1 2 10 50 13 If I choose to start at

Find minimum possible height of the highest tower

妖精的绣舞 提交于 2019-12-24 23:15:56
问题 I have towers with height H = h1,h2,h3... and i have a cutting machine that only cuts each tower with a specific length A = a1,a2,a3.. and i have m cuts find the minimum possible height of the highest tower. for ex- H = 1, 4, 9, 16, 25 and A = 1,2,3,4,5 m = 3 ( only 3 cuts ) the minimum possible height is 15 as after cuts the array looks like H = 1,4,9,12,15 ( after applying 0, 0 , 0, 1, 2 cuts respectively on towers) What I tried : I recognised it as greedy problem (correct me if i am wrong)

Given an array of positive numbers, find the maximum subsequence sum <= given sum, such that no two elements are adjacent to each other

橙三吉。 提交于 2019-12-24 22:27:04
问题 This is my code It passes for few cases but not for all, for eg. number of elements n = 6 elements a[n] = {5,5,10,100,10,5} and sum = 25 the output is 15, but output should be 25 (5 + 10 + 10) click here to see its working in IDE #include<bits/stdc++.h> #define ll long long int using namespace std; int main(){ int n; ll sum; vector<ll> v; cin>>n; ll a[n]; for(int i=0;i<n;i++){ cin>>a[i]; } cin>>sum; ll ans = 0; ll inc = a[0]; ll exc = 0; ll prev = 0; for(int i=1;i<n;i++){ prev = inc; inc =

Find the smallest subset of overlapping intervals

陌路散爱 提交于 2019-12-24 17:02:39
问题 Consider a question to find a minimum dominating set of an interval graph. In the context of interval scheduling, it is converted to the question below: There are multiple intervals which may or may overlap with each other. Find a minimum subset of the intervals, so that for every interval that is not included in the subset, it will find at least 1 interval in the subset that will overlap with it. There is an agreed greedy solution available from various sources on the Internet, such as one

Where does this greedy scheduling algorithm become sub-optimal?

眉间皱痕 提交于 2019-12-24 05:16:11
问题 This question is inspired by another question on process allocation. This is a twist on, and enhancement of, the problem discussed there. We have n processes, and we need to allocate them to the fewest possible processors. Each process has a scheduled start and finish time, which we will define in terms of time units , indexed from 1; a process will run for some contiguous sequence of time units. A processor can then be scheduled to run any number of non-overlapping processes. The obvious