Created
September 4, 2014 16:08
-
-
Save socantre/57185beb0935b763ca31 to your computer and use it in GitHub Desktop.
construct worst case for arbitrary? sort algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <vector> | |
| #include <algorithm> | |
| void make_killer(int size, std::vector<int>& v) { | |
| int candidate = 0; | |
| int num_solid = 0; | |
| int gas = size - 1; | |
| std::vector<int> tmp(size); | |
| v.resize(size); | |
| for (int i = 0; i < size; ++i) { | |
| tmp[i] = i; | |
| v[i] = gas; | |
| } | |
| std::sort(tmp.begin(), tmp.end(), [&](int x, int y) { | |
| if (v[x] == gas && v[y] == gas) { | |
| if (x == candidate) v[x] = num_solid++; | |
| else v[y] = num_solid++; | |
| } | |
| if (v[x] == gas) candidate = x; | |
| else if (v[y] == gas) candidate = y; | |
| return v[x] < v[y]; | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment