Artifact Content
Not logged in

Artifact ffc08068b2a48f887f07313b689fa27c6d9c0662


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

template<typename T>
class IdGen
{
	map<T, int> v2id_;
	vector<T>   id2v_;
public:
	int v2id(const T& v) {
		if( !v2id_.count(v) ) { v2id_[v] = size(); id2v_.push_back(v); }
		return v2id_[v];
	}
	const T& id2v(int i) const { return id2v_[i]; }
	int size() const { return id2v_.size(); }
};

template<typename Vert=pair<int,int>, typename Flow=LL, int NV=2048>
class MaxFlow
{
	IdGen<Vert> idgen;
	vector<int> G[NV];
	Flow F[NV][NV];

public:
	void addEdge( Vert s_, Vert t_, Flow f )
	{
		const int s = idgen.v2id(s_), t = idgen.v2id(t_);
		G[s].push_back(t);
		G[t].push_back(s);
		F[s][t] = f;
		F[t][s] = 0;
	}

	Flow calc( Vert s_, Vert t_ )
	{
		const int S = idgen.v2id(s_), D = idgen.v2id(t_);
		for( Flow total=0 ;; ) {
			// Do BFS and compute the level for each node.
			int LV[NV] = {0};
			vector<int> Q(1, S);
			for(int lv=1; !Q.empty(); ++lv) {
				vector<int> Q2;
				for(size_t i=0; i!=Q.size(); ++i) {
					const vector<int>& ne = G[Q[i]];
					for(size_t j=0; j!=ne.size(); ++j)
						if( F[Q[i]][ne[j]] && !LV[ne[j]] && ne[j]!=S )
							LV[ne[j]]=lv, Q2.push_back(ne[j]);
				}
				Q.swap(Q2);
			}

			// Destination is now unreachable. Done.
			if( !LV[D] )
				return total;

			// Iterating DFS.
			bool blocked[NV] = {};
			total += dinic_dfs( S, D, LV, 0x7fffffff, blocked );
		}
	}

private:
	Flow dinic_dfs( int v, int D, int LV[], Flow flow_in, bool blocked[] )
	{
		Flow flow_out = 0;
		for(size_t i=0; i!=G[v].size(); ++i) {
			int u = G[v][i];
			if( LV[v]+1==LV[u] && F[v][u] ) {
				Flow f = min(flow_in-flow_out, F[v][u]);
				if( u==D || !blocked[u] && (f=dinic_dfs(u,D,LV,f,blocked))>0 ) {
					F[v][u]  -= f;
					F[u][v]  += f;
					flow_out += f;
					if( flow_in == flow_out ) return flow_out;
				}
			}
		}
		blocked[v] = (flow_out==0);
		return flow_out;
	}
};

class Singing { public:
	int solve(int N, int low, int high, vector <int> pitch)
	{
		map<pair<int,int>, int> edge;
		for(int i=0; i+1<pitch.size(); ++i) {
			int p0 = min(pitch[i], pitch[i+1]);
			int p1 = max(pitch[i], pitch[i+1]);
			if(p0 != p1)
				edge[make_pair(p0,p1)] ++;
		}

		MaxFlow<>* mf = new MaxFlow<>;
		const LL INF = 0x3fffffff;

		enum{BOB, LEFT, RIGHT, ALICE};
		pair<int,int> Bob(BOB,0);
		pair<int,int> Alice(ALICE,0);
		for(int v=1; v<=N; ++v) {
			if(v>high)
				mf->addEdge(Bob, make_pair(LEFT,v), INF);
			mf->addEdge(make_pair(LEFT,v), make_pair(RIGHT,v), INF);
			if(v<low)
				mf->addEdge(make_pair(RIGHT,v), Alice, INF);
		}
		for(auto e: edge) {
			int v = e.first.first;
			int u = e.first.second;
			int c = e.second;
			mf->addEdge(make_pair(RIGHT,v), make_pair(LEFT,u), c);
			mf->addEdge(make_pair(RIGHT,u), make_pair(LEFT,v), c);
		}

		LL ans = mf->calc(Bob, Alice);
		delete mf;
		return int(ans);
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, Singing().solve(N, low, high, pitch));}
int main(){

CASE(0)
	int N = 3; 
	int low = 2; 
	int high = 2; 
	int pitch_[] = {1,2,3,2,1,2};
	  vector <int> pitch(pitch_, pitch_+sizeof(pitch_)/sizeof(*pitch_)); 
	int _ = 2; 
END
CASE(1)
	int N = 10; 
	int low = 3; 
	int high = 7; 
	int pitch_[] = {4,4,5,5,6,5,3,6};
	  vector <int> pitch(pitch_, pitch_+sizeof(pitch_)/sizeof(*pitch_)); 
	int _ = 0; 
END
CASE(2)
	int N = 6; 
	int low = 2; 
	int high = 5; 
	int pitch_[] = {5,3,1,6,4,2};
	  vector <int> pitch(pitch_, pitch_+sizeof(pitch_)/sizeof(*pitch_)); 
	int _ = 1; 
END
CASE(3)
	int N = 10; 
	int low = 4; 
	int high = 5; 
	int pitch_[] = {1,4,3,5,2,5,7,5,9};
	  vector <int> pitch(pitch_, pitch_+sizeof(pitch_)/sizeof(*pitch_)); 
	int _ = 3; 
END
CASE(4)
	int N = 100; 
	int low = 20; 
	int high = 80; 
	int pitch_[] = {2,27,3,53,53,52,52,60,85,89,100,53,60,2,3,53,100,89,40,42,2,53,2,85};
	  vector <int> pitch(pitch_, pitch_+sizeof(pitch_)/sizeof(*pitch_)); 
	int _ = 5; 
END
/*
CASE(5)
	int N = ; 
	int low = ; 
	int high = ; 
	int pitch_[] = ;
	  vector <int> pitch(pitch_, pitch_+sizeof(pitch_)/sizeof(*pitch_)); 
	int _ = ; 
END
CASE(6)
	int N = ; 
	int low = ; 
	int high = ; 
	int pitch_[] = ;
	  vector <int> pitch(pitch_, pitch_+sizeof(pitch_)/sizeof(*pitch_)); 
	int _ = ; 
END
*/
}
// END CUT HERE