# Problem description

Tanabata Festival has been labeled "Valentine's Day" because of the legend of Cowherd and weaver girl. So TYVJ held an offline Tanabata Festival this year. Vani successfully invited cl to spend Tanabata with him this year, so they decided to go to TYVJ Tanabata Festival.
TYVJ Tanabata Festival is very similar to the summer festival in zone 11. The rectangular sacrificial venue consists of N rows and m columns, totaling n × M stalls. Although there are many kinds of stalls, cl is only interested in some of them, such as Octopus stew, apple candy, marshmallow, shooting house... And so on. Vani contacted the person in charge zhq of the Tanabata Festival in advance, hoping that by properly arranging the venue, cl in each row will have as many stalls as cl in each column.
However, zhq told Vani that the stalls had been arranged at will. If you want to meet cl's requirements, the only way to adjust is to exchange two adjacent stalls. Two stalls are adjacent if and only if they are adjacent in the same row or column. Because the TYVJ development team led by zhq successfully distorts the space, the first position and the last position of each row or column are also counted as adjacent. Now Vani wants to know how many of his two requirements can be met at most. On this premise, at least how many stalls need to be exchanged.

Input format

```　The first line contains three integers N and M and T. T express cl How many stalls are you interested in.
next T Rows, two integers per row x, y，express cl Yes, at No x Line number y Column stalls are interested.
```

Output format

```　First, output a string. If you can meet Vani All two requirements, output both；If you pass the adjustment
The whole can only make cl As many stall points of interest, output row；If you can only make each column cl Sense of interest
There are as many stalls as fun, output column；If none can be met, output impossible.
If the output string is not impossible， Next, the minimum number of exchanges is output, which is between the and the string
Separated by a space.

```

sample input

```Sample input 1
2 3 4
1 3
2 1
2 2
2 3

Sample input 2
3 3 3
1 3
2 2
2 3
```

sample output

```Sample output 1
row 1

Sample output 2
both 2
```

Original question: Tanabata Festival

# Train of thought analysis

After analysis, we can find that exchanging the left and right adjacent stalls will only change the stalls of interest to c1 in a certain two columns, and will not change the stalls of interest to c1 in each row.
Exchange two adjacent stalls. The same is true.
Therefore, we can divide the problem into two independent parts, that is, the analogy is the problem of "ring equally dividing cards" in the direction of rows and columns. The problem of "ring equally divided cards" can be compared to the problem of "linear equally divided cards" with a broken ring from the median.

Linear average card analysis: Look here

Analysis of circular equally divided Solitaire: Look here

Namely:
1. Through the least number of left and right exchanges, the number of stalls of interest to c1 in each column is the same.
2. The number of stalls of interest to c1 in each row is the same through the least number of left and right exchanges.

Taking the first case as an example, our goal is to have T/M stalls of c1 interest in each column, so as to simplify this problem into a basic ring equally divided card problem.
As usual, we break the ring from the median, and then reduce it to the basic linear equipartition card problem, and then find the minimum cost according to the idea of prefix sum.
The second case is the same.
According to the above analysis, the third case is the minimum cost of the first case plus the minimum cost of the second case.

# code

```#include<iostream>
using namespace std;
#include<string>
#include<algorithm>

long X;
long Y;
long num_X = { 0 };
long num_Y = { 0 };
long S;
long S2;

void main()
{
// N,M,T
long N, M, T;
cin >> N >> M >> T;
// x row y column
long x, y;
for (long i = 0; i < T; i++)
{
cin >> x >> y;
X[i] = x;
num_X[x]++;
Y[i] = y;
num_Y[y]++;

}
string ret = "impossible";
if (T % M && T % N)
cout << ret;
// You can only have as many lines
else if (!(T % N) && (T % M))
{
ret = "row";
S[N - 1] = 0;
long avg = T / N;
long ans = 0;
// Disconnect from median
for (long i = N - 2; i >= 0; i--)
S[i] = S[i + 1] - (num_X[X[i]] - avg);
sort(S, S + N);
for (long i = 0; i < N; i++)
ans += abs(S[i] - S[(N / 2)]);

cout << ret << " " << ans;

}
// Only the same number of columns can be made
else if (!(T % M) && (T % N))
{
ret = "column";
S[M - 1] = 0;
long avg = T / M;
long ans = 0;
// Disconnect from median
for (long i = M - 2; i >= 0; i--)
S[i] = S[i + 1] - (num_Y[Y[i]] - avg);
sort(S, S + M);
for (long i = 0; i < M; i++)
ans += abs(S[i] - S[(M / 2)]);

cout << ret << " " << ans;

}
// Both ranks can meet the requirements
else
{
ret = "both";
S[N - 1] = 0;
long avg1 = T / N;
long ans = 0;
// Disconnect from median
for (long i = N - 2; i >= 0; i--)
S[i] = S[i + 1] - (num_X[X[i]] - avg1);
sort(S, S + N);
for (long i = 0; i < N; i++)
ans += abs(S[i] - S[(N / 2)]);

S2[M - 1] = 0;
long avg2 = T / M;
// Disconnect from median
for (long i = M - 2; i >= 0; i--)
S2[i] = S2[i + 1] - (num_Y[Y[i]] - avg2);
sort(S2, S2 + M);
for (long i = 0; i < M; i++)
ans += abs(S2[i] - S2[(M / 2)]);

cout << ret << " " << ans;

}

}
```

Posted on Fri, 24 Sep 2021 22:10:28 -0400 by pas07920