Submission #835151

#TimeUsernameProblemLanguageResultExecution timeMemory
835151kamelfanger83Arranging Shoes (IOI19_shoes)C++14
30 / 100
96 ms15660 KiB
#include <iostream> #include <vector> #include <set> #include "shoes.h" using namespace std; struct Segtree{ vector<int> cache; Segtree (int n){ cache.resize(4 * n); } int update(int l, int r, int i, int ui, int ux){ if(ui < l || r <= ui) return cache[i]; if (l + 1 == r) return cache[i] = ux; int m = l + (r-l)/2; return cache[i] = update(l, m, i*2+1, ui, ux) + update(m, r, i*2+2, ui, ux); } int query(int l, int r, int i, int ql, int qr){ if (qr <= l || r <= ql) return 0; if (ql <= l && r <= qr) return cache[i]; int m = l + (r-l)/2; return query(l, m, i*2+1, ql, qr) + query(m, r, i*2+2, ql, qr); } }; long long count_swaps(vector<int> S){ set<pair<int, int>> lefts, rights; for (int ii = 0; ii < S.size(); ++ii) { int i = S[ii]; if (i < 0) lefts.insert({-i, ii}); else rights.insert({i, ii}); } int n = S.size(); Segtree segtree (n); long long ans = 0; for (int i = 0; i < n / 2; ++i) { auto r1 = lefts.begin(); auto r2 = rights.begin(); if (lefts.begin()->second > rights.begin()->second) { r1 = lefts.lower_bound({r2->first, 0}); } else { r2 = rights.lower_bound({r1->first, 0}); } ans += r1->second - segtree.query(0, n, 0, 0, r1->second); segtree.update(0, n, 0, r1->second, 1); ans += r2->second - segtree.query(0, n, 0, 0, r2->second); segtree.update(0, n, 0, r2->second, 1); lefts.erase(r1); rights.erase(r2); } return ans; } /* int main(){ cout << count_swaps({-2, 2, 2, -2, -2, 2}); }*/

Compilation message (stderr)

shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:30:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |     for (int ii = 0; ii < S.size(); ++ii) {
      |                      ~~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...