Submission #601386


Source Code Expand

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
using namespace std;
using ll=long long ;

// #define DEBUG

int main(void){
	int n,m;
	cin >> n >> m;
	vector<int> x,y;

	bool tmp[3010];
	rep(i,n) tmp[i]=false;
	rep(i,m){
		int arg;
		cin >> arg;
		tmp[arg-1]=true;
	}
	
	rep(i,n) if(tmp[i]==false) x.push_back(i);

	rep(i,n) tmp[i]=false;
	rep(i,m){
		int arg;
		cin >> arg;
		tmp[arg-1]=true;
	}

	rep(i,n) if(tmp[i]==false) y.push_back(i);

	int sz=x.size();
	vector<int> xx,yy;
	rep(i,sz) xx.push_back((x[(i+1)%sz]-x[i]+n)%n);
	rep(i,sz) yy.push_back((y[(i+1)%sz]-y[i]+n)%n);

	ll ans=-1LL;

	bool can_mov=false;
	rep(i,sz) if(xx[i]>=3) can_mov=true;		

	#ifdef DEBUG
		rep(i,sz) cerr << (i?" ":"") << x[i]; 
		cerr << endl;
		rep(i,sz) cerr << (i?" ":"") << y[i];
		cerr << endl;
		cerr << endl;
	#endif

	if(sz==1){
		ll left,right;
		bool ok=true;
		if(x[0]<y[0])	
			right=y[0]-x[0],left=n+x[0]-y[0];
		else
			left=x[0]-y[0],right=n+y[0]-x[0];
		
		if(n&1){
			if(left&1) left+=n;
			if(right&1) right+=n;
		}else{
			if(left&1) ok=false;
			if(right&1) ok=false;
		}

		#ifdef DEBUG
			cerr << left << " " << right << endl;
		#endif

		if(ok&&(ans==-1||ans>abs(left/2LL))) ans=abs(left/2LL);
		if(ok&&(ans==-1||ans>abs(right/2LL))) ans=abs(right/2LL);
	
	}else{
		if(can_mov){
	
			rep(i,sz){

				bool ok=true;
				ll left,right;
				if(x[0]<y[i]) 
					right=y[i]-x[0],left=n+x[0]-y[i];
				else
					left=x[0]-y[i],right=n+y[i]-x[0];
				
				if(n&1){
					if(left&1) left+=n;
					if(right&1) right+=n;
				}else{
					if(left&1) ok=false;
					if(right&1) ok=false;
				}

				if(ok==false) continue;

				#ifdef DEBUG
					cerr << left << " " << right << endl;
				#endif

				ll cur[3010];

				// left ->
				ok=true;
				rep(j,sz) cur[j]=xx[j];

				#ifdef DEBUG
					rep(j,sz) cerr << (j?" ":"") << cur[j];
					cerr << endl;
				#endif

				ll ret=left/2LL;
				
				#ifdef DEBUG
					rep(j,sz) cerr << (j?" ":"") << cur[j];
					cerr << endl;
				#endif

				rep(j,sz-1){
					ll parity=(yy[(i+j)%sz]-cur[j])%2;
					if(parity&1) ok=false;
					ll cost=(yy[(i+j)%sz]-cur[j]);
					cur[j]+=cost,cur[(j+1)%sz]-=cost;
					ret+=abs(left/2LL-cost/2LL);
				}			

							
				#ifdef DEBUG
					rep(k,sz) cerr << (k?" ":"") << cur[k];
					cerr << endl << ret << endl;
				#endif 

				rep(j,sz) if(cur[j]!=yy[(i+j)%sz]) ok=false;
				if(ok&&(ans==-1||ans>abs(ret))) ans=abs(ret);

				// right
				ok=true;
				rep(j,sz) cur[j]=xx[j];
				ret=right/2LL;
				
				#ifdef DEBUG	
					rep(j,sz) cerr << (j?" ":"") << cur[j];
					cerr << endl;
				#endif

				rep(j,sz-1){
					ll parity=(yy[(i+j)%sz]-cur[j])%2;
					if(parity&1) ok=false;
					ll cost=(yy[(i+j)%sz]-cur[j]);
					cur[j]+=cost,cur[(j+1)%sz]-=cost;
					ret+=abs(right/2LL+cost/2);
				}

				
				#ifdef DEBUG
					rep(j,sz) cerr << (j?" ":"") << cur[j];
					cerr << endl << ret << endl << endl;
				#endif	
				rep(j,sz) if(cur[j]!=yy[(i+j)%sz]) ok=false;
				if(ok&&(ans==-1||ans>abs(ret))) ans=abs(ret);
				// cerr << lcost << " " << rcost << endl;
			}
		}
		else{
			ans=0LL;
			rep(j,sz) if(x[j]!=y[j]) ans=-1LL;  
		}
	}
	cout << ans << endl;
	return 0;
}


// oooox
// ooxoo
// xoooo

// xoooo

Submission Info

Submission Time
Task K - Leapfrog
User Hec
Language C++11 (GCC 4.8.1)
Score 100
Code Size 3375 Byte
Status AC
Exec Time 456 ms
Memory 928 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 3
AC × 54
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All sample_01.txt, sample_02.txt, sample_03.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt, test_22.txt, test_23.txt, test_24.txt, test_25.txt, test_26.txt, test_27.txt, test_28.txt, test_29.txt, test_30.txt, test_31.txt, test_32.txt, test_33.txt, test_34.txt, test_35.txt, test_36.txt, test_37.txt, test_38.txt, test_39.txt, test_40.txt, test_41.txt, test_42.txt, test_43.txt, test_44.txt, test_45.txt, test_46.txt, test_47.txt, test_48.txt, test_49.txt, test_50.txt, test_51.txt
Case Name Status Exec Time Memory
sample_01.txt AC 27 ms 916 KB
sample_02.txt AC 25 ms 744 KB
sample_03.txt AC 452 ms 796 KB
test_01.txt AC 25 ms 920 KB
test_02.txt AC 25 ms 920 KB
test_03.txt AC 28 ms 920 KB
test_04.txt AC 27 ms 824 KB
test_05.txt AC 451 ms 788 KB
test_06.txt AC 453 ms 920 KB
test_07.txt AC 454 ms 916 KB
test_08.txt AC 456 ms 768 KB
test_09.txt AC 28 ms 792 KB
test_10.txt AC 27 ms 920 KB
test_11.txt AC 134 ms 916 KB
test_12.txt AC 133 ms 916 KB
test_13.txt AC 28 ms 800 KB
test_14.txt AC 29 ms 920 KB
test_15.txt AC 27 ms 928 KB
test_16.txt AC 28 ms 916 KB
test_17.txt AC 29 ms 928 KB
test_18.txt AC 28 ms 728 KB
test_19.txt AC 79 ms 796 KB
test_20.txt AC 83 ms 920 KB
test_21.txt AC 81 ms 800 KB
test_22.txt AC 77 ms 804 KB
test_23.txt AC 83 ms 920 KB
test_24.txt AC 76 ms 808 KB
test_25.txt AC 87 ms 844 KB
test_26.txt AC 85 ms 796 KB
test_27.txt AC 80 ms 924 KB
test_28.txt AC 63 ms 812 KB
test_29.txt AC 81 ms 812 KB
test_30.txt AC 26 ms 800 KB
test_31.txt AC 134 ms 928 KB
test_32.txt AC 134 ms 928 KB
test_33.txt AC 133 ms 796 KB
test_34.txt AC 134 ms 804 KB
test_35.txt AC 134 ms 796 KB
test_36.txt AC 135 ms 828 KB
test_37.txt AC 134 ms 924 KB
test_38.txt AC 137 ms 804 KB
test_39.txt AC 135 ms 928 KB
test_40.txt AC 135 ms 864 KB
test_41.txt AC 136 ms 800 KB
test_42.txt AC 133 ms 804 KB
test_43.txt AC 82 ms 924 KB
test_44.txt AC 81 ms 800 KB
test_45.txt AC 80 ms 808 KB
test_46.txt AC 82 ms 920 KB
test_47.txt AC 134 ms 920 KB
test_48.txt AC 132 ms 920 KB
test_49.txt AC 135 ms 920 KB
test_50.txt AC 136 ms 916 KB
test_51.txt AC 134 ms 920 KB