I have a list of pairs [x; Y] where x is unique and y can be duplicate (integer).
Here's a problem:
A pair of [x; Y], the new pair [k; M], such as:
- K> X
- M & gt; = Y
- k - x has been reduced
Now, I have solved this problem with this argument; I sort the pair by x, and then start the naïve oo (n ^ 2) algorithm on it. It seems to work fine, except that it is very slow
Can I do better?
I am trying to solve the real problem, here it is: and my current code:#include & lt; Stdio.h & gt; # Include & lt; Utility & gt; #include & lt; Queue & gt; # Include & lt; Vector & gt; # Include & lt; Algorithm & gt; # Include & lt; Map & gt; using namespace std; Structure employee {int id; Int pay; Inti height; Int parent_index; Int sub_nordinate; Int cur; Boole important; The Bull Operator & lt; (Constant & amp; nbsp; A) Const {if (height == e.height) Return salary & gt; E.salary; Return (height> E. high); }}; // Problem explains that there are no two employees / one salary. Struct salary_predicate {Inline Bull Operator () (Constant Employee and Straight 1, Constant Employee and Straight 2) {Return (Straight 1 ciliary> Straight 2.salary); }}; Const int MAX_EMPLOYEES = 30000; Const int MAX_QUERIES = 200; Employee employee [MAX_EMPLOYEES]; Integer query [MAX_QUERIES]; Int main () {int test_cases; Scanf ("% d", and test_cases); While (test_cases--) {int employee, query query; Scanf ("% d% d", and employee count, and query content); Int i = 0; Int j = 0; While (i & lt; employee count) {employees [i] Parent_index = -1; Employee [i] .sub_ordinates = 0; Employee [i] .cur = i; Employees [ii] Important = false; Scan F ("% d% d% d", and employee [i] .id, and employee [i]. Solarium, and employee [ii] .high); I ++; } Map & lt; Int, int & gt; Mapper; While (j & lt; query count) {scanf ("% d", and question [j]); Mapper.insert (pair & lt; int, int & gt; (query [j], -1); J ++; } // Now step 1; Based on the structure / basis of the sort staff !! Sort (employee, employee + employee count, pay_activator) For (int k = 0; k & lt; employee count; k ++) {employees [k] .cur = k; If (mapper.find (employee [k] .id)! = Mapper.end ()) {mapper [employees [k] .id] = k; Employees [of] Important = true; }} Int found = 0; For (int l = employee count - 1; l & gt; = 0; l--) {int gef = l - 1; // View information about the previous employee, // he can give us some important information! // With his help, we know that we can leave some nonsense :) If (L + 1 & employee; employee and employee [L + 1]. Parent_index! = -1) {// If the previous staff is at least Our current employee // So we can leave some people, because we know that the answer is not in comparison to that // smalle :) (employees [L + 1] .Little; = employees [L]. Height) gef = employees [L + 1]. Parent_index - 1; } // Find the boss! (Int b = gef; b & gt; = 0; b--) {if (employee [B]. HEIGHT = = employees [L]. HEIGHT) {employees [L]. Parent_index = b; Employees [B] .sub_ordinates + = employees [L] .sub_ordinates + 1; break; }} // ensures that we take action on all essential things, we can basically stop our work. If found (employee [L] important) ++; If (found == mapper.size ()) break; } // Time to print it For (int b = 0; b & lt; queryCount; b ++) {int id = queries [b]; Int index = mapper [id]; Int parent_index = employee [index]. Parent_index; Int parent = parent_indack & lt; 0? 0: employees [parent_index] .id; Printf ("% d% d \ r \ n", guardian, employee [index] .sub_ordinates); }} Return 0; }
Pay = x, and height = y.
I will start by destroying all records where
m < Y or
k & lt; = X . Then find the smallest
k value except the item. Both of these should be linear, so your overall complexity should also be linear.
struct p {int k, m; }; P find_item (p xy, std :: vector & lt; p & gt; and value) {auto end = std :: partition (values.begin), values.end (), [xy] (p const & v ) {Return Xy.k & lt; Vk || Xy.m & gt; = Vm;}); Return * std :: min_element (values.begin (), end, [] (p const & amp; a, p const & amp; amp; amp; ak & lt; bk;} );}
No comments:
Post a Comment