1025E - Colored Cubes - CodeForces Solution


constructive algorithms implementation matrices *2700

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long 
#define inl inline
#define endl '\n'
#define getchar cin.get
#define push_back emplace_back
const int N=4e5+5;
const int M=6e4+5;
const int inf=0x3f3f3f3f;
const int mod=998244853;
inl int read(){
	int x=0,f=1;char c=getchar();
	while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
	while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
	return x*f;
}
int n,m,vis[55][55],pos1[55],pos2[55];
struct pos{
	int a,b,c,d;
};
vector<pos>v,ans;
struct node{
	int x,y,id;
	friend bool operator<(node a,node b){return a.x^b.x?a.x<b.x:abs(a.x-a.y)<abs(b.x-b.y);}
}a[55];
inl void solvex(int x){
	int xp=a[x].x,yp=a[x].y;
	while(xp>x){
		if(vis[xp-1][yp])solvex(vis[xp-1][yp]);
		v.push_back((pos){xp,yp,xp-1,yp});
		vis[xp][yp]=0;vis[--xp][yp]=x;
	}
	while(xp<x){
		if(vis[xp+1][yp])solvex(vis[xp+1][yp]);
		v.push_back((pos){xp,yp,xp+1,yp});
		vis[xp][yp]=0;vis[++xp][yp]=x;
	}
	a[x].x=xp;
}
inl void solvey(int x){
	int xp=a[x].x,yp=a[x].y;
	while(yp>x){
		v.push_back((pos){xp,yp,xp,yp-1});
		vis[xp][yp]=0;vis[xp][--yp]=x;
	}
	while(yp<x){
		v.push_back((pos){xp,yp,xp,yp+1});
		vis[xp][yp]=0;vis[xp][++yp]=x;
	}
	a[x].y=yp;
}
inl void swapp(int x,int y){
	if(x>y)swap(x,y);
	for(int i=1;i<=y-x;i++)ans.push_back((pos){x+i-1,x,x+i,x});
	for(int i=1;i<=y-x;i++)ans.push_back((pos){y-i+1,y,y-i,y});
	for(int i=1;i<=y-x;i++)ans.push_back((pos){y,x+i-1,y,x+i});
	for(int i=1;i<=y-x;i++)ans.push_back((pos){x,y-i+1,x,y-i});
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	n=read();m=read();
	for(int i=1;i<=m;i++)a[i]={read(),read(),i};
	sort(a+1,a+m+1);
	for(int i=1;i<=m;i++)vis[a[i].x][a[i].y]=i;
	for(int i=1;i<=m;i++)pos1[a[i].id]=i;
	for(int i=1;i<=m;i++)solvex(i);
	for(int i=1;i<=m;i++)solvey(i);
	for(auto i:v)ans.push_back(i);
	for(int i=1;i<=m;i++)vis[i][i]=0;v.clear();
	for(int i=1;i<=m;i++)a[i]={read(),read(),i};
	sort(a+1,a+m+1);
	for(int i=1;i<=m;i++)vis[a[i].x][a[i].y]=i;
	for(int i=1;i<=m;i++)pos2[a[i].id]=i;
	for(int i=1;i<=m;i++)solvex(i);
	for(int i=1;i<=m;i++)solvey(i);
	for(int i=1;i<=m;i++)
		if(pos1[i]^pos2[i]){
			swapp(pos1[i],pos2[i]);
			for(int j=1;j<=m;j++)
				if(pos1[j]==pos2[i]){
					swap(pos1[i],pos1[j]);
					break;
				}
		}
	reverse(v.begin(),v.end());
	for(auto i:v)ans.push_back((pos){i.c,i.d,i.a,i.b});
	cout<<ans.size()<<endl;
	for(auto i:ans)cout<<i.a<<' '<<i.b<<' '<<i.c<<' '<<i.d<<endl;
	return 0;
}
/*
10 10
1 1
1 3
1 5
2 2
2 4
3 1
3 3
3 5
4 2
4 4
4 5
4 3
4 1
3 4
3 2
2 5
2 3
2 1
1 4
1 2

*/


Comments

Submit
0 Comments
More Questions

406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path