1162B - Double Matrix - CodeForces Solution


brute force greedy *1400

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
// #define int long long
#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__)
template <class S, class T>ostream& operator <<(ostream& os, const pair<S, T>& p) {return os << "(" << p.first << ", " << p.second << ")";}
template <class T>ostream& operator <<(ostream& os, const vector<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const unordered_set<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class S, class T>ostream& operator <<(ostream& os, const unordered_map<S, T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const set<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const multiset<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class S, class T>ostream& operator <<(ostream& os, const map<S, T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T> void dbs(string str, T t) {cerr << str << " : " << t << "\n";}
#define ld long double
#define ss second
#define ff first
#define pb push_back
#define mset(m,v) memset(m,v,sizeof(m))
#define nl cout<<"\n";
typedef pair<int, int> pii;
typedef vector<pii> vpii;
#define all(a) (a).begin(), (a).end()
#define f(x,a,b) for(int x=a;x<b;x++)
#define frev(x,a,b) for(int x=a;x>=b;x--)
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define ff first
#define ss second
int N = 2e5+10;



void solve()
{
	int n,m;
	cin>>n>>m;

	int a[n][m], b[n][m];
	f(i,0,n) f(j,0,m) cin>>a[i][j];
	f(i,0,n) f(j,0,m) cin>>b[i][j];

	for(int i=0; i<n; i++)
	{
		for(int j=1; j<m; j++)
		{
			if((a[i][j] > a[i][j-1]) && ((b[i][j] > b[i][j-1]))) continue;
			else if((b[i][j] > a[i][j-1]) && ((a[i][j] > b[i][j-1])))
			{
				swap(a[i][j], b[i][j]);
			}
			else
			{
				cout<<"Impossible\n"; 
				return; 
			}
		}
	}
	for(int i=0; i<m; i++)
	{
		for(int j=1; j<n; j++)
		{
			if((a[j][i] > a[j-1][i]) && ((b[j][i] > b[j-1][i]))) continue;
			else if((b[j][i] > a[j-1][i]) && ((a[j][i] > b[j-1][i])))
			{
				swap(a[j][i], b[j][i]);
			}
			else
			{
				cout<<"Impossible\n"; 
				return; 
			}
		}
	}
	cout<<"Possible";
}

signed main ()
{
//  ios_base::sync_with_stdio(0);
//  cin.tie(0);cout.tie(0);

//  int t; cin>>t;
//  while(t--) 
 solve();
}


Comments

Submit
0 Comments
More Questions

1173B - Nauuo and Chess
318B - Strings of Power
1625A - Ancient Civilization
864A - Fair Game
1663B - Mike's Sequence
448A - Rewards
1622A - Construct a Rectangle
1620A - Equal or Not Equal
1517A - Sum of 2050
620A - Professor GukiZ's Robot
1342A - Road To Zero
1520A - Do Not Be Distracted
352A - Jeff and Digits
1327A - Sum of Odd Integers
1276A - As Simple as One and Two
812C - Sagheer and Nubian Market
272A - Dima and Friends
1352C - K-th Not Divisible by n
545C - Woodcutters
1528B - Kavi on Pairing Duty
339B - Xenia and Ringroad
189A - Cut Ribbon
1182A - Filling Shapes
82A - Double Cola
45A - Codecraft III
1242A - Tile Painting
1663E - Are You Safe
1663D - Is it rated - 3
1311A - Add Odd or Subtract Even
977F - Consecutive Subsequence