제출 #246222

#제출 시각아이디문제언어결과실행 시간메모리
246222EvirirArranging Shoes (IOI19_shoes)C++17
100 / 100
476 ms33160 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define watch(x) cout<<(#x)<<"="<<(x)<<'\n'
#define mset(d,val) memset(d,val,sizeof(d))
#define setp(x) cout<<fixed<<setprecision(x)
#define forn(i,a,b) for(int i=(a);i<(b);i++)
#define fore(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
#define F first
#define S second
#define pqueue priority_queue
#define fbo find_by_order
#define ook order_of_key
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef long double ld;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
void amin(ll &a, ll b){ a=min(a,b); }
void amax(ll &a, ll b){ a=max(a,b); }
void YES(){cout<<"YES\n";} void NO(){cout<<"NO\n";}
void SD(int t=0){ cout<<"PASSED "<<t<<endl; }
const ll INF = ll(1e18);
const int MOD = 998244353;

struct FenwickPoint{
	vector<ll> fw;
	int siz;
	FenwickPoint(): fw(vector<ll>()), siz(0) {}
	FenwickPoint(int N)
	{
		fw.assign(N+1,0);
		siz = N+1;
	}
	void reset(int N)
	{
		fw.assign(N+1,0);
		siz = N+1;
	}
	void add(int p, ll val)
	{
		for(p++; p<siz; p+=(p&(-p)))
		{
			fw[p]+=val;
		}
	}
	ll sum(int p)
	{
		ll res=0;
		for(; p; p-=(p&(-p)))
		{
			res+=fw[p];
		}
		return res;
	}
	ll query(int l, int r)
	{
		l++; r++;
		if(r<l) return 0;
		if(l==0) return sum(r);
		return sum(r)-sum(l-1);
	}
	void modify(int p, ll val)
	{
		add(p, val-query(p,p));
	}
};

bool DEBUG = 0;
const int MAXN = 100005;

map<int,set<int>> loc;

ll count_swaps(vector<int> a)
{
	int n = a.size();
	FenwickPoint fw(n);
	forn(i,0,n) fw.add(i,1);
	
	forn(i,0,n){
		loc[a[i]].insert(i);
	}
	
	ll ans = 0;
	
	forn(i,0,n)
	{
		if(DEBUG) SD(i);
		if(fw.query(i,i)==0){
			if(DEBUG) cout<<"continued"<<endl;
			continue;
		}
		
		if(a[i]>0){
			if(DEBUG) cout<<"flip"<<endl;
			ans++;
		}
		
		int nxt = *loc[-a[i]].begin();
		if(DEBUG) cout<<"nxt="<<nxt<<endl;
		
		ans += fw.query(i+1,nxt-1);
		
		fw.add(i,-1);
		fw.add(nxt,-1);
		loc[a[i]].erase(loc[a[i]].begin());
		loc[-a[i]].erase(loc[-a[i]].begin());
		
		if(DEBUG) cout<<"ans="<<ans<<endl;
	}
	
	return ans;
}
#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...