Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <utility>
- void PrintArr(const std::vector<int>& arr, bool isBreakLine = true)
- {
- for (const auto& i : arr)
- {
- std::cout << i << " ";
- }
- if (isBreakLine)
- std::cout << "\n";
- }
- void Swap(int* num1, int* num2)
- {
- int tmp = *num1;
- *num1 = *num2;
- *num2 = tmp;
- }
- /* Merge Overlapping Interval
- [6,10], [7,9]
- */
- bool CheckOverlapping(const std::vector<int>& arr1, const std::vector<int>& arr2)
- {
- bool ret = false;
- if (arr1[0] <= arr2[0])
- {
- if (arr2[0] <= arr1[1])
- ret = true;
- }
- else
- {
- if (arr1[0] <= arr2[1])
- ret = true;
- }
- return ret;
- }
- std::vector<int> MergeInterval(const std::vector<int>& arr1, const std::vector<int>& arr2)
- {
- std::vector<int> mergeInterval(2, 0);
- mergeInterval[0] = (arr1[0] <= arr2[0]) ? arr1[0] : arr2[0];
- mergeInterval[1] = (arr1[1] >= arr2[1]) ? arr1[1] : arr2[1];
- return mergeInterval;
- }
- std::vector<std::vector<int>> PairInterval(const std::vector<std::vector<int>>& arr1, const std::vector<std::vector<int>>& arr2)
- {
- std::vector<std::vector<int>> ret;
- if (arr1.size() == 1 && arr2.size() == 1)
- {
- if (arr1[0][0] > arr2[0][0])
- {
- ret.emplace_back(arr2[0]);
- ret.emplace_back(arr1[0]);
- }
- else
- {
- ret.emplace_back(arr1[0]);
- ret.emplace_back(arr2[0]);
- }
- return ret;
- }
- int i = 0, j = 0;
- while (i < arr1.size() && j < arr2.size())
- {
- if (arr1[i][0] < arr2[j][0])
- {
- ret.emplace_back(arr1[i]);
- i++;
- }
- else
- {
- ret.emplace_back(arr2[j]);
- j++;
- }
- }
- for (int m = i; m < arr1.size(); m++)
- {
- ret.emplace_back(arr1[m]);
- }
- for (int n = j; n < arr2.size(); n++)
- {
- ret.emplace_back(arr2[n]);
- }
- return ret;
- }
- std::vector<std::vector<int>> SortInterval(std::vector<std::vector<int>>& intervals)
- {
- if (intervals.size() == 1)
- return intervals;
- int mid = intervals.size() / 2;
- std::vector<std::vector<int>> leftIntervals(intervals.begin(), intervals.begin() + mid);
- std::vector<std::vector<int>> rightIntervals(intervals.begin() + mid, intervals.end());
- auto leftRet = SortInterval(leftIntervals);
- auto rightRet = SortInterval(rightIntervals);
- auto pairRet = PairInterval(leftRet, rightRet);
- return pairRet;
- }
- /*
- Idea: Tìm phần tử nhỏ nhất sau mỗi lần lặp
- */
- void SelectionSort(std::vector<int>& unsorted_arr)
- {
- for (int i = 0; i < unsorted_arr.size(); i++)
- {
- int curMin = unsorted_arr[i];
- int newIdxMin = -1;
- for (int j = i + 1; j < unsorted_arr.size(); j++)
- {
- if (unsorted_arr[j] < curMin)
- {
- newIdxMin = j;
- curMin = unsorted_arr[j];
- }
- }
- if (newIdxMin != -1)
- Swap(&unsorted_arr[i], &unsorted_arr[newIdxMin]);
- }
- }
- int main()
- {
- std::vector<std::vector<int>> originInterval = {{1,3},{2,6},{8,10},{15,18}};
- std::vector<std::vector<int>> originInterval1 = {{7, 9}, {6,10}, {4, 5}, {1, 3}, {2, 4}};
- auto sortedIntervals = SortInterval(originInterval);
- for(const auto& interval : sortedIntervals)
- {
- std::cout << "interval:{" << interval[0] << ", " << interval[1] << "} ";
- }
- std::cout << std::endl;
- int idxNonOverlapInterval = 0;
- for (int i = 1; i < sortedIntervals.size(); i++)
- {
- if (CheckOverlapping(sortedIntervals[idxNonOverlapInterval], sortedIntervals[i]))
- {
- sortedIntervals[idxNonOverlapInterval] = MergeInterval(sortedIntervals[idxNonOverlapInterval], sortedIntervals[i]);
- }
- else
- {
- idxNonOverlapInterval++;
- sortedIntervals[idxNonOverlapInterval] = sortedIntervals[i];
- }
- }
- for (int j = 0; j <= idxNonOverlapInterval; j++)
- {
- std::cout << "ret: {" << sortedIntervals[j][0] << ", " << sortedIntervals[j][1] << "} ";
- }
- std::cout << std::endl;
- //---------------------------------------------------------------------------------------
- std::vector<int> unsorted_arr = {11, 4, 27, 34, 43, 17, 13, 17, 31, 12};
- SelectionSort(unsorted_arr);
- PrintArr(unsorted_arr);
- }
Advertisement
Comments
-
- https://katfile.com/47dnpxn71ghh/defloration_of_rebecca.mp4.html
- https://katfile.com/smcxklrxq2bu/t33n_hottie_hippie.mp4.html
- https://katfile.com/2m534lsws01y/1yo-_renee_roulette.mp4.html
- https://katfile.com/1cufobt46nkl/teen_fucked_and_spanked.mp4.html
- https://katfile.com/9h0ejv5fyw6d/skinny_teen_squirted_on_a_huge_cock.mp4.html
- https://katfile.com/2c17vu10m7f0/Xvideos_loving_sex_with_teen.mp4.html
- https://katfile.com/21h5dbudizpq/little_puusy.mp4.html
- https://katfile.com/311arifdnur0/stepdaughter_is_fucked.mp4.html
- https://katfile.com/io0wfnl7mdk6/eos__arcel.mp4.html
- https://katfile.com/khn7888qjnn3/exxxtra_t.mp4.html
- https://katfile.com/vzfutxpkkmgk/you_doing_i_am_t.mp4.html
- https://katfile.com/23eazz88040p/girl_masturbation_pussy.mp4.html
- https://katfile.com/m7tkkbbgfy9e/exciting_teenie.mp4.html
- https://katfile.com/9qs94as6iix4/774442[pt.mp4.html
- https://katfile.com/bw4nby0d4hfl/analj.mp4.html
- https://katfile.com/7us4r0bsconi/cam_5801.mp4.html
- https://katfile.com/36s00ol8nv1q/hot_play_webcam_teen_masturbation_in_front.mp4.html
- https://katfile.com/110ld42x6d7w/morning_teen_masturb.mp4.html
- https://katfile.com/mjk1qxqefaii/orrga01.MP4.html
- https://katfile.com/gon211e3ldm9/orrga02.MOV.html
- https://katfile.com/41fuahxsgv9j/orrga3.mp4.html
- https://katfile.com/si951brc7wai/teen_cocksucker.mp4.html
- https://katfile.com/i6ejhnfmzkmy/VID_201_018.mp4.html
- https://katfile.com/fi4x6r4tgkqm/darling_tomoyo_isumi_fucks_a_gu.mp4.html
- https://katfile.com/ti4wk9bbnwlr/defloration_t.mp4.html
- https://katfile.com/io0wfnl7mdk6/eos__arcel.mp4.html
- https://katfile.com/m7tkkbbgfy9e/exciting_teenie.mp4.html
- https://katfile.com/khn7888qjnn3/exxxtra_t.mp4.html
- https://katfile.com/23eazz88040p/girl_masturbation_pussy.mp4.html
- https://katfile.com/110ld42x6d7w/morning_teen_masturb.mp4.html
- https://katfile.com/dy8t4x1qh63k/my_best_friend_was_a_virgin.mp4.html
- https://katfile.com/311arifdnur0/stepdaughter_is_fucked.mp4.html
- https://katfile.com/si951brc7wai/teen_cocksucker.mp4.html
- https://katfile.com/air1cunyil6q/Xxxx_.t.mp4.html
- https://katfile.com/vzfutxpkkmgk/you_doing_i_am_t.mp4.html
Add Comment
Please, Sign In to add comment
Advertisement