Submission #18637

#TimeUsernameProblemLanguageResultExecution timeMemory
18637choyi0521즐거운 채소 기르기 (JOI14_growing)C++14
100 / 100
111 ms4600 KiB
#include<stdio.h> #include<algorithm> using namespace std; const int MAX_N = 300000; int n, bit[MAX_N + 1]; pair<int, int> p[MAX_N]; void update(int h) { for (; h <= n; h += h&-h) bit[h]++; } int query(int h) { int s = 0; for (; h; h -= h&-h) s += bit[h]; return s; } int main() { scanf("%d", &n); for (int i = 0; i <n; i++) { scanf("%d", &p[i].first); p[i].second = i + 1; } sort(p, p + n); long long res = 0; for (int i = n - 1, j = n - 1; i >= 0; i--) { res += min(query(p[i].second - 1), query(n) - query(p[i].second)); if (!i || p[i].first == p[i - 1].first) continue; for (; j >= i; j--) update(p[j].second); } printf("%lld", res); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...