Sort ------ Tanabata Festival

Tanabata Festival because Vega Legend and labeled a "Valentine's Day" hat.
So TYVJ Tanabata festival this year was held the next line.
Vani students successfully invited this year to cl students to accompany him to spend Tanabata, so they decided to go TYVJ Tanabata Festival play.
In the form of TYVJ Summer Festival Tanabata Festival and 11 district like.
Festival venue rectangle composed of N columns × N rows M M Total number stalls.
Although a wide range of stalls, but only interested in stalls cl part of it, such as takoyaki, what candy apple, marshmallow, fire house .......
Vani previously contacted the person in charge of the Tanabata Festival zhq, we want to be able to properly arranged through the venue, so that the number of stalls in each row cl interest as much, and the number of stalls in each column cl interested too much.
But zhq told Vani, stalls have been arranged at random finished, if you want to meet the requirements of cl, the only way to adjust is to swap two adjacent stalls.
Two adjacent stalls, if and only if they are in the same row or the same column on adjacent positions.
Because zhq led TYVJ development team succeeded in distorting the space, the first position and the last position of each row and each column is also counted as neighbors.
Now Vani wanted to know two of his most able to meet the requirements of how many.
In this context, how many stalls at least need to be exchanged.
Input format
The first line contains three integers N and M and T, T represents the number of stalls cl bought.
Next T lines of two integers x, y, cl expressed interest of stalls in row x and column y of.
Output Format
first outputs a string.
If Vani meet both of the requirements, the output of both;
only so that the number of stalls in each row as many cl interest, by adjusting the output if the row;
If only the number of stalls in each column cl of interest as much as the output column;
if not meet, output impossible.
If the output string is not Impossible, the output of the next smallest number of exchanges, and spaced apart by a space between the strings.
Data range
1≤N, M≤1000001≤N, M≤100000,

0≤T≤min(N∗M,100000)0≤T≤min(N∗M,100000),

1≤x≤N1≤x≤N,

1≤y≤M1≤y≤M
Input Sample:
2. 4. 3
. 1. 3
2. 1
2 2
2. 3

Sample output:
Row 1

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long LL;
const int N = 100010;
int row[N], col[N], s[N], c[N];
LL work(int n, int a[]){
 for (int i = 1; i <= n; i ++)   s[i] = s[i - 1] + a[i];
  if (s[n] % n)   return -1;
 int avg = s[n] / n;
 c[1] = 0;
  for (int i = 2; i <= n; i ++)    c[i] = s[i - 1] - (i - 1) * avg;;
  sort(c + 1, c + n + 1);
  LL res = 0;
 for (int i = 1; i <= n; i ++)   res += abs(c[i] - c[(n + 1) / 2]);
 return res;
}
int main(){
 int n, m, cnt;
 scanf("%d%d%d", &n, &m, &cnt);
 
 while(cnt --){
  int x,y;
  scanf("%d%d",&x, &y);
  row[x] ++, col[y] ++;
 }
  LL r = work(n, row);
 LL c = work(m, col);
 if (r != -1 && c != -1)   printf("both %lld\n",r + c);
 else   if (r != -1)       printf("row %lld\n", r);
 else   if (c != -1)       printf("column %lld\n", c);
 else   printf("impossible");
 return 0;
}
Published 106 original articles · won praise 67 · views 5439

Guess you like

Origin blog.csdn.net/qq_45772483/article/details/104763934