Submission #1022682

#TimeUsernameProblemLanguageResultExecution timeMemory
1022682vjudge1Arranging Shoes (IOI19_shoes)C++17
50 / 100
1090 ms13948 KiB
    #include <bits/stdc++.h>
    using namespace std;
     
    #define rep(i, a, b) for(int i = a; i < (b); ++i)
    #define all(x) begin(x), end(x)
    #define sz(x) (int)(x).size()
    #define trav(a, x) for (auto &a : x)
    #define pb push_back
    #define el "\n";
    #define dbg(x) cerr << "\e[92m" << #x << " = " << x << "\e[97m" << el;
    //#define dbg(x)  
    typedef long long ll;
    typedef pair<int, int> pii;
    typedef vector<int> vi;
    typedef vector<ll> vll;
     
    /* dies on 
    4
    1 2 1 -1 -2 -1 3 -3
    */
     
    ll count_swaps(vector<int> S) {
    	assert(sz(S) % 2 == 0);
    	ll n = sz(S) / 2; // number of pairs
    	vi a = S;
     
    	vector<vll> pos(2 * n + 1);
    	rep(i, 0, 2*n) {
    		dbg(i);
    		pos[a[i] + n].pb(i);
    		dbg(a[i]);
    	}
    	ll ans = 0;
    	ll ind = 0;
    	while(ind < 2 * n) {
    		ll cur = a[ind];
    		auto nxt = upper_bound(all(pos[-cur + n]), ind);
    		auto dbgarray = [&](vi &arr, string s) -> void {
    			cerr << s << " is: ";
    			rep(i, 0, sz(arr)) {
    				cerr << arr[i] << " ";
    			}
    			cerr << el;
    		};
    		//dbgarray(a, "a");
     
    		assert(nxt != pos[-cur + n].end());
    		assert(a[*nxt] == -cur);
     
    		vi temp;
    		rep(i, 0, ind) {
    			temp.pb(a[i]);
    		}
    		//dbgarray(temp, "temp");
    		temp.pb((cur < 0 ? cur : -cur));
    		temp.pb((cur < 0 ? -cur : cur));
    		//dbgarray(temp, "temp");
    		rep(i, ind + 1, 2*n)  {
    			if (i == *nxt) continue;
    			temp.pb(a[i]);
    		}
    		//dbgarray(temp, "temp");
    		ans += (*nxt) - ind - (cur < 0 ? 1 : 0);
    		assert(sz(temp) == 2 * n);
    		ind += 2;
     
    		swap(a, temp);
    		pos.assign(2 * n + 1, vll());
    		rep(i, 0, 2*n) {
    			pos[a[i] + n].pb(i);
    		}
    	}
    	return ans;
    }
     

Compilation message (stderr)

shoes.cpp: In function 'll count_swaps(std::vector<int>)':
shoes.cpp:38:12: warning: variable 'dbgarray' set but not used [-Wunused-but-set-variable]
   38 |       auto dbgarray = [&](vi &arr, string s) -> void {
      |            ^~~~~~~~
#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...