We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Lily's Homework
- Discussions
Lily's Homework
Lily's Homework
Sort by
recency
|
24 Discussions
|
Please Login in order to post a comment
include
using namespace std;
string ltrim(const string &); string rtrim(const string &); vector split(const string &);
int countSwapsToTarget(const vector& arr, const vector& target) { int n = arr.size(); unordered_map pos; pos.reserve(n * 2); for (int i = 0; i < n; ++i) pos[target[i]] = i; vector p(n); for (int i = 0; i < n; ++i) p[i] = pos[arr[i]]; vector vis(n, 0); int cycles = 0; for (int i = 0; i < n; ++i) { if (vis[i]) continue; ++cycles; int u = i; while (!vis[u]) { vis[u] = 1; u = p[u]; } } return n - cycles; }
int lilysHomework(vector arr) { vector asc = arr; sort(asc.begin(), asc.end()); vector desc = asc; reverse(desc.begin(), desc.end()); int swapsAsc = countSwapsToTarget(arr, asc); int swapsDesc = countSwapsToTarget(arr, desc); return min(swapsAsc, swapsDesc); }
int main() { ofstream fout(getenv("OUTPUT_PATH")); string n_temp; getline(cin, n_temp); int n = stoi(ltrim(rtrim(n_temp))); string arr_temp_temp; getline(cin, arr_temp_temp); vector arr_temp = split(rtrim(arr_temp_temp)); vector arr(n); for (int i = 0; i < n; i++) { int arr_item = stoi(arr_temp[i]); arr[i] = arr_item; } int result = lilysHomework(arr); fout << result << "\n"; fout.close(); return 0; }
string ltrim(const string &str) { string s(str); s.erase( s.begin(), find_if(s.begin(), s.end(), not1(ptr_fun(isspace))) ); return s; }
string rtrim(const string &str) { string s(str); s.erase( find_if(s.rbegin(), s.rend(), not1(ptr_fun(isspace))).base(), s.end() ); return s; }
vector split(const string &str) { vector tokens; string::size_type start = 0; string::size_type end = 0; while ((end = str.find(" ", start)) != string::npos) { tokens.push_back(str.substr(start, end - start)); start = end + 1; } tokens.push_back(str.substr(start)); return tokens; }
Solution in Python 3
Reviving Teammates Once a shrine has been placed, gamers will need to technique the shrine and location the glowing coronary heart in their deceased teammate on pinnacle of it. As soon as this is carried out, gamers will need to start a lengthy ritual of prayer at the shrine. Players must only start this ritual when they are quite secure to achieve this, because the ritual to revive fallen teammates takes quite a long term to complete, leaving the participant and their crew at risk of ambush with the aid of monsters or different gamers.
Once gamers finish channeling the ritual, the heart could be ate up, reviving the teammate who the coronary heart belongs to. Players can then supply their teammate their gear back that was looted formerly, as the revived group member will haven't any gadget in any respect whilst they come again, and they could preserve to try to acquire greater spoils in the course of the dungeon.
Pool compliance refers to adhering to local regulations and safety standards concerning the construction, maintenance, and operation of swimming pools.