CodeChef submission 70955 (C++ 4.0.0-8) plaintext list. Status: AC, problem INSOMA3, contest . By triplem (Stephen Merriman), 2009-08-16 05:04:49.
#include <iostream> using namespace std; int nums[100000]; int ans = 0; int tmp[100000]; void solve(int low, int high) { if (low==high) return; int mid = (low+high)/2; solve(low,mid); solve(mid+1,high); // assume [low,mid] is sorted and [mid+1,high] is sorted int lowpos = low, highpos = mid+1; int tmppos = 0; while (lowpos<=mid || highpos<=high) { if (lowpos>mid || highpos<=high && nums[highpos]<nums[lowpos]) { // take it from the high end tmp[tmppos++]=nums[highpos++]; ans += (mid-lowpos+1); } else { tmp[tmppos++]=nums[lowpos++]; } } tmppos = 0; for (int i=low; i<=high; i++) nums[i] = tmp[tmppos++]; } int main() { solve(0,N-1); }
Comments

