제출 #1274581

#제출 시각아이디문제언어결과실행 시간메모리
1274581KluydQArranging Shoes (IOI19_shoes)C++20
100 / 100
461 ms679872 KiB
#include "shoes.h"
#include <bits/stdc++.h>

//define respagold ios_base::sync_with_stdio(0), cin.tie(0);
//#define int long long
#define ll long long
//#define int2 __int128_t
#define FOR( i, x, n, d ) for( int i = x; i <= n; i += d )
#define FORR( i, x, n, d ) for( int i = x; i >= n; i -= d )
//#define F first
//#define S second
//#define all(x) x.begin(), x.end()
//#define sz(x) (int)(x.size())
#define pb push_back
//#define ins insert
//#define lb lower_bound
//#define ub upper_bound
//#define pii pair <int, int>

using namespace std;
 
const int N = 1e6 + 12;
const int mod = 1e9 + 7;
const ll inf = 2e18;

ll n, m, x, y, a[N], b[N], ans, timer, t[N];
deque <ll> g[N];

void update( ll p, ll v )
{
	for(; p <= n; p |= (p + 1)) t[p] += v;
}
ll gt( ll r )
{
	ll res = 0;
	for(; r >= 0; r = (r & r + 1) - 1) res += t[r];
	return res;
}
ll get( ll l, ll r )
{
	return gt(r) - gt(l - 1);
}
long long count_swaps( vector <int> ww )
{
	for( auto i : ww ) a[++ n] = i;
	
	FOR( i, 1, n, 1 )
	{
		x = -a[i] + n;
		
		if( !g[x].empty() )
		{
			ans += (a[i] < 0);
			a[g[x].front()] = a[i] = ++ timer;
			b[g[x].front()] = i, g[x].pop_front();
		}
		else g[a[i] + n].pb(i);
	}
	FOR( i, 1, n, 1 )
	{
		if( b[i] == 0 ) continue;
		
		update(b[i], 1);
		ans += get(i + 1, b[i] - 1) + get(b[i] + 1, n) * 2;
	}
	return ans;
}/*
signed main()
{
	//respagold;
	
	int test = 1;
	if( !test ) cin >> test;
	while( test -- ) solve();
}*/
#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...