Submission #251532

#TimeUsernameProblemLanguageResultExecution timeMemory
251532ryanseeArranging Shoes (IOI19_shoes)C++14
100 / 100
330 ms191720 KiB
#include "shoes.h"

#include "bits/stdc++.h"
using namespace std;

#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr<<"hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x,y) (lower_bound(all(x),y)-x.begin())
#define ubd(x,y) (upper_bound(all(x),y)-x.begin())
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());    //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return rng() % (y+1-x) + x; } //inclusivesss
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?llabs(b):gcd(b%a,a); }

using ll=long long; 
using ld=long double;
#define FOR(i,s,e) for(ll i=s;i<=ll(e);++i)
#define DEC(i,s,e) for(ll i=s;i>=ll(e);--i)
using pi=pair<ll,ll>; using spi=pair<ll,pi>; using dpi=pair<pi,pi>; 

#define LLINF ((long long)1e18)
#define INF int(1e9+1e6)
#define MAXN (200006)
ll n, fw[MAXN];
void update(int x,ll nval){
	++ x;
	for(;x<MAXN;x+=x&(-x)) fw[x]+=nval;
}
ll sum(int x){
	++ x;
	ll res=0;
	for(;x;x-=x&(-x)) res+=fw[x];
	return res;
}
long long count_swaps(std::vector<int> A) {
	ll n=A.size()/2, ans = 0;
	deque<deque<deque<ll>>> sz(n+1, deque<deque<ll>>(2, deque<ll>()));
	vector<pi> e;
	vector<ll> v(2*n,0);
	FOR(i,0,2*n-1){
		bool R = A[i]>0;
		ll x=abs(A[i]);
		sz[x][R].eb(i);
		if(sz[x][0].size()&&sz[x][1].size()){
			e.eb(sz[x][0][0],sz[x][1][0]),sz[x][0].pop_front(), sz[x][1].pop_front();
		}
	}
	assert(e.size() == n);
	FOR(i,0,n-1)A[e[i].f]=-(i+1), A[e[i].s]=i+1;
	for(auto i:e){
		ll a=i.f,b=i.s;
		if(a>b)swap(a,b), ++ans;
		v[b]=v[a]=a;
	}
	FOR(i,0,2*n-1){
		if(v[i] ^ i) ans += sum(MAXN-2) - sum(v[i]);
		update(v[i], 1);
	}
	return ans;
}

Compilation message (stderr)

In file included from /usr/include/c++/7/cassert:44:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:33,
                 from shoes.cpp:3:
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:55:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  assert(e.size() == n);
         ~~~~~~~~~^~~~
#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...