Maximum children happiness [duplicate] - c++

This question already has answers here:
How to code the maximum set packing algorithm?
(2 answers)
Closed 8 years ago.
Given N different candies and M children. Each of the children demands for K[i] different candies and will only be happy iff he get all those different candies which he demanded.
Now I want to maximize the number children that get happy. How should I distribute the candies?
Example: Let's have N=4 candies and M=3 children:
1st child requires 2 (K[1]) candies which are: 1 and 2
2nd child requires 2 (K[2]) candies which are: 2 and 3
3rd child requires 2 (K[3]) candies which are: 3 and 4
The answer here is 2 as I can at best only make the 1st and 3rd child happy.
My attempt:
Give candies to children in the order of the amounts that they require to be happy (i.e. K[i]). In other words, you should only give candy to a child if you have made happy all the children that demand less, and every time you give candy to one of them you have to give them the whole amount that they require.
Is this solution correct?

Yes, you are wrong. Consider:
Wants candies 1, 2, 3, and 4.
Wants candies 1, 5, 6, 7, 8, and 9.
Wants candies 2, 10, 11, 12, 13, 14, and 15.
Wants candies 3, 16, 17, 18, 19, 20, and 21.
Wants candies 4, 22, 23, 24, 25, 26, and 27.
Your algorithm will only make child 1 happy. But you can make 2, 3, 4, and 5 happy.

As this a problem in on-going programming contest i cannot give you much of an answer but will push you in right direction. The problem you are solving is np complete as it can be reduced to maximum independent set and hence it can only be solved in general case using brute force which is trying out all combinations. You can reduce computations by checking if new set added is not intersecting with the other in this way you can skip a lot of invalid combinations.

Related

Clojure adding up keys in map [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Hey guys I'm playing around with a personal project I'm making as I'm new to clojure, its measuring football players performance.
This is the a partial example of the data I am using
:Performance 2019, :Day 1, :CR7 43, :Messi -2,
:Performance 2019, :Day 2, :CR7 12, :Messi 6,
:Performance 2019, :Day 3, :CR7 -11, :Messi 4,
:Performance 2019, :Day 4, :CR7 4, :Messi 32,
:Performance 2019, :Day 5, :CR7 21, :Messi -48,
The years in the data that I'm using go back to around 2010 and the days go upto 31, It would have been to much to put here so I included a sample. The numbers next to the players CR7 and Messi are personal performance indicators that I have given them. There's less players for now but over time I will add more.
Ive stored the data in a zipmap like so
(zipmap [:Performance :Day :CR7 :Messi
My question is how would you add up all the ratings for each player from the 31 days and work out their highest performing year, bare in mind the years go back to 2010-2020
You can make the problem more tractable by re-shaping the data to avoid mixing program-logic keys (:Performance and :Day) with data ("CR7" and "Messi" for now).
For example, you could state the database as a list of assertions:
[{:performance 2019 :day 3 :player "CR7" :rating -11}
{:performance 2019 :day 3 :player "Messi" :rating 4}
...]
Conveniently, you could source that data from a CSV file, making it easy to add facts about more players without revising the column structure of the data.
Computationally, you face two challenges. 1) For each player, for each year, sum the ratings. 2) For each player, harvest the year of the highest rating.
The result of challenge No.1 is probably a map of player to (map of year to sum of ratings). Like this:
{"CR7" {2018 44, 2019 73, 2020 81}
"Messi" {2018 32, 2019 11, 2020 6}}
To get there, you need to transform a list (of assertions) to a single thing (a map of player to years' ratings). A Clojure standard-library function that transforms a list to a single thing is reduce. With each input record (one year, one day, one player, one rating) you update the reduction by adding the rating to one counter. The standard-library function update-in will be just the ticket.
For challenge No.2, you want the year with the highest rating. The standard-library function max-key could be helpful there.

Modified candy distribution task

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.
Children with a higher rating get more candies than anybody with lower.
What is the minimum candies you must give?
Example:
Input:
[2, 1, 5]
Output:
6
Complexity: O(n)
There are already many solutions for this task when we notice only neighboring children (ex: https://leetcode.com/problems/candy/solution/) but what to do with this case?

Probability of obtaining 2 hearts and 2 aces

I am really really struggling with this question and so far have not found an answer nor could figure it out correctly. I'll really appreciate if anybody can help answering the question with explanations. Thanks in advance!
You are to pick 4 cards randomly from a deck of cards that contains four suits and the following denominations: Ace, 2, 3, 4, 5, 6, 7, 8, 9, and 10.
There are no face-cards in this deck. "What is the probability of obtaining two Aces AND 2 hearts"?

Data structure to multi-sort feature vector by attributes

I need to sort a vector with tuples
[
(a_11, ..., a_1n),
... ,
(a_m1, ..., a_mn)
]
based on a list of attributes and their comparison operators < or >.
For example: sort first by a_2 with the > operator and by a_57 with the < operator.
Question: I am looking for a data structure to do this efficiently under the assumption that sorting happens much more often than updates to the vector.
My current idea is to store the sorting order for each attribute by adding pointers similar to a linked list for each attribute:
For example, this vector:
0: (1, 7, 4)
1: (2, 5, 6)
2: (3, 4, 5)
Would get the data structure
0: (1 next:1 prev:-, 7 next:- prev:1, 4 next:2 prev:-)
1: (2 next:2 prev:1, 5 next:0 prev:2, 6 next:- prev:2)
2: (3 next:- prev:2, 4 next:1 prev:-, 5 next:1 prev:0)
Edit:
At any given time I need only one sorting order. After I get a user request for a different sorting order I need to recompute as quickly as possible.
The incremental idea is very good, but I need to make an estimate on how much time I need and this is way more easy if I have an idea how it should be done.
Once i am finished I need random access to groups of 100 elements, i.e. the first 100, the second 100, or elements 5100-5199.
I would use boost::MultiIndex for this. – drescherjm

Two plus Two Poker hand evaluator: How are the cards mapped to the integers?

In an effort to create the fastest possible monte carlo texas hold'em hand analyzer with C++, I am currently looking into the subject of hand evaluation.
As many of you may know, there are quite a number of hand evaluators, open source, out there. After giving it some thought, I settled on the "Two Plus Two hand evaluator" (so named since it was first introduced on the two plus two forum).
This is one of the fastest known evaluators out there, and uses array lookups to quickly find the value of a hand.
Now, for the function, you need to pass in an array with the cards you are interested in. Example:
int Cards[] = { 3, 5, 10, 17, 23, 24, 32 };
int hv = HandValue(Cards);
With values between 1 and 52. Now, my question is: What cards do these integers correspond to? Is a 3 an ace of spades? A three of hearts? I have scoured google, the two + two forum, various pages where hand evaluators are presented, the source file for the build-up of the array. All in vain. So I am hoping that someone here can point me in the right direction of where I can find this information, or give it to me outright.
The source where the evaluators are taken from is this excellent article: http://www.codingthewheel.com/archives/poker-hand-evaluator-roundup#cactus_kev
Which explains all the evaluators individually.
I didn't verify this, but it appears to be:
"2c": 1,
"2d": 2,
"2h": 3,
"2s": 4,
"3c": 5,
"3d": 6,
...
"kh": 47,
"ks": 48,
"ac": 49,
"ad": 50,
"ah": 51,
"as": 52
ref: https://github.com/chenosaurus/poker-evaluator/blob/master/lib/PokerEvaluator.js