taha_bayat_ir's blog

By taha_bayat_ir, history, 17 minutes ago, In English

Hi

Today I want to talk about a very simple trick that is useful in many places and can be used in a lot of ways: Coordinate Compression.

Where is it useful?

Let's start with a problem:

Order of Verses

  • Time limit: 1 second
  • Memory limit: 256 MB

Kianoush is applying for membership in the OC organization. On the third day of the interview, the organization is testing him on literature.

In this interview, five people sit in front of Kianoush. They have chosen a poem, and each of them once shuffles the verses of that poem and tells Kianoush the verses in a random order. That is, they first consider the originally chosen poem, then select some of its verses, remove them from the poem, and add them somewhere else in the poem. Kianoush must find the order of the verses in the original poem. We know that each verse has been removed and moved in at most one of these 5 poems.

When the verses are shuffled, the verses are added in an arbitrary order and at an arbitrary position after being removed. The positions of the verses that were not removed may also change during this process.

Given the verses that were read as input, determine what the original order was. It is guaranteed that in the given inputs, the original order is uniquely determined.

Input

The first line of the input contains only the number n, which represents the number of verses in the poem that was read.

Then in the next 5n lines, five times, each time in n lines, and in each line one verse of the poem is given. We identify the verses with distinct integers between 0 and 10^9.

1 ≤ n ≤ 20000

Output

The output of the program must contain n lines, each line containing one verse of the poem. The order of the verses must be equal to the originally chosen order.

Example

Input 5 1 2 3 4 5 2 1 3 4 5 3 1 2 4 5 4 1 2 3 5 5 1 2 3 4 Output 1 2 3 4 5

In this example, each person, before reading the poem, removes one verse from it and moves it to the beginning of the poem, and then reads it. So each verse has been moved in at most one poem.

Solution

The best thing to do is to sort them — but how? We need a function that tells us, for two verses x and y, which one comes before the other. On the other hand, we know that the two poems differ in at most two verses, and this difference is only in one poem. So we can tell which one comes first by looking at the rest of the poems.

For this, it's better to have a 2D array called wh that tells us in which position verse i is located in poem j.

But there is an issue: since a[i] <= 1e9 but n <= 1e5, we have a lot of empty space in the array that is left unused, and to find the elements we have to loop through this array, which costs us a lot of time. So it's better to reduce the values of these verses, because only the relations between them matter to us. In fact, what we want is:

When we have an array whose count is smaller than the largest value in the array, and we only need the relations between these numbers, we can make these numbers smaller and avoid working with larger numbers. This is called compress.

(Note: There is another approach using a map data structure, which offers better time complexity; however, the compression method is more customizable.)

Now let's explain how compression works, and then solve the problem based on it.

1. Converting the numbers

It works like this: first we take a copy of the original array, sort all the numbers, and then make them unique. Then we call it compressed. That's it!

void compress()
{
	b = a[0];
	sort(b.begin(), b.end());
	b.erase(unique(b.begin(), b.end()), b.end());
}

Now how do we use it?

The first case is when you want to convert a large number into a small number. What should you do? You can simply use binary search to find your number and get its index.

int bts(int x)
{
	return lower_bound(b.begin(), b.end(), x) - b.begin();
}

The next question: when I have a small number and want to convert it to a large one? Again, it's very easy — we just return the value of that number in the compressed array.

int stb(int x)
{
	return b[x];
}

2.Back to the problem:

Now that we are familiar with compression, we can store the elements very easily and the problem is solved.

// IN THE NAME OF ALLAH
// YA ALI!

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define pb push_back

const int maxn = 1e5;
int n, wh[5][maxn];
vector<int> b;
vector<vector<int>> a(5);

void compress()
{
	b = a[0];
	sort(b.begin(), b.end());
	b.erase(unique(b.begin(), b.end()), b.end());
}

int bts(int x)
{
	return lower_bound(b.begin(), b.end(), x) - b.begin();
}

int stb(int x)
{
	return b[x];
}

bool cmp(int a, int b)
{
	vector<int> x1; vector<int> x2;
	int y1 = bts(a), y2 = bts(b), cnt[2] = {0,0};
	for(int i = 0; i < 5; i++)
	{
		x1.pb(wh[i][y1]); x2.pb(wh[i][y2]);
	}

	for(int i = 0; i < 5; i++)
	{
		if(x1[i] > x2[i]) cnt[0]++;
		else cnt[1]++;
	}

	return cnt[1] > cnt[0];
}

void solve()
{
	cin >> n;
	for(int i = 0; i < 5; i++)
	{
		for(int j = 0; j < n; j++)
		{
			int x; cin >> x;
			a[i].pb(x);
		}
	}

	compress();
	for(int i = 0; i < 5; i++)
	{
		for(int j = 0; j < n; j++)
		{
			wh[i][bts(a[i][j])] = j;
		}
	}

	vector<int> tmp = a[0];
	sort(tmp.begin(), tmp.end(), cmp);
	for(int i = 0; i < n; i++) cout << tmp[i] << '\n';
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);

	int t = 1;
//	cin >> t;
	while(t--)
		solve();

	return 0;
}

Time Complexity : O(log n)

  • Vote: I like it
  • 0
  • Vote: I do not like it