Basic algorithm-Tanabata festival

Source: Internet
Author: User
Tags abs

Title Description

Tanabata Festival is a "Valentine's Day" hat because of the legend of the Cowherd and Weaver. So TYVJ this year held an off-line Tanabata festival. Vani classmate this year successfully invited to CL classmate accompany him to spend the tanabata, so they decided to TYVJ Tanabata festival play.
The TYVJ Tanabata Festival and the summer Festival of the 11 district are very similar in form. The rectangular Festival Hall consists of a total of nxm stalls in the N row M column. Although there are a wide variety of stalls, CL is only interested in one of the stalls, such as octopus burning, apple sugar, cotton candy, and the house that shoots. What Vani Pre-contacted the person in charge of the Tanabata festival Zhq, hoping to be able to properly decorate the venue, so that the line of CL interested in the number of stalls, as well as the column of CL interested in the number of stalls as much. But Zhq told Vani that the stalls had been arranged, the only way to adjust was to exchange two adjacent stalls. Two stalls adjacent, when and only if they are in the same row or adjacent to the same column. As the TYVJ development team led by ZHQ successfully distorted space, the first and last positions of each row or column counted as adjacent. Now Vani wants to know how many of his two requirements can be met. In this context, at least how many stalls need to be exchanged.

Input

The first line consists of three integers n and M and T. T indicates how many stalls a CL is interested in.
Next T line, two integers per line, x, Y, indicates that CL is interested in stalls in row x, column Y.

Output

First, a string is output. If you can meet all two requirements of Vani, output both; If you can only make the same number of stalls in each row, the output row is only as many as the number of stalls interested in CL in each column, output column, if none are satisfied, the output is impossible.
If the output string is not impossible, then the minimum number of interchanges is output, separated from the string by a space.

Sample input

2 3 4
1 3
2 1
2 2
2 3

Sample output

Row 1
1

Tips

For 30% of data, n,m≤100.
For 70% of data, n,m≤1000.
For 100% of data, 1≤n,m≤100000,0≤t≤min (nm,100000), 1≤x≤n,1≤y≤m.

Analysis
    • First, it is not difficult to find that columns and rows are not affected by each other, so they can be handled separately.
    • If we want to make all the rows the same amount, this is actually a "card-sharing problem", and in the subject is circular.
    • After averaging each person should have average card, the number of each of the existing cards minus average, then we have to divide the purpose and become everyone has 0 cards
    • For non-annular cards, the minimum number of moves is the sum of the absolute value of the prefix and, so for the ring we can enumerate the prefix and start the starting point, and then take the minimum value of all the answers.
    • Then we can see that each prefix and minus pre[k] that takes the first element as the starting point is exactly the prefix and the beginning of the k+1 element.
    • The problem is converted to minus which pre[k] can make the prefix and the absolute value of the smallest, that is \ (\sum_{i=1}^{n}|pre[i]-pre[k]|\) the smallest
    • Obviously but Pre[k] is the smallest of the prefixes and the median of the array
Code
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include < Algorithm>using namespace Std;typedef long long ll;const int maxn=100050;const ll mod=1e9+7;int a[maxn],b[maxn];ll sum    [Maxn];int Main () {int n,m,t;    scanf ("%d%d%d", &n,&m,&t);        for (int i = 0; i < T; ++i) {int x, y;        scanf ("%d%d", &x,&y);        a[x]++;    b[y]++;        } if (t%n&&t%m) {printf ("impossible\n");    return 0;    } if (t%n==0&&t%m==0) printf ("both");    else if (t%n==0) printf ("Row");    else printf ("column");    ll S1=0,s2=0;        if (t%n==0) {int p=t/n;            for (int i = 1; I <= n; ++i) {a[i]-=p;        Sum[i]=sum[i-1]+a[i];        } sort (sum+1,sum+1+n);        ll mid=sum[(n+1)/2];        for (int i = 1; I <= n; ++i) {s1+=abs (sum[i]-mid);        }} if (t%m==0) {int p=t/m;  for (int i = 1; I <= m; ++i) {b[i]-=p;          Sum[i]=sum[i-1]+b[i];        } sort (sum+1,sum+1+m);        ll mid=sum[(m+1)/2];        for (int i = 1; I <= m; ++i) {s2+=abs (sum[i]-mid);    }} printf ("%lld\n", S1+S2); return 0;}

Basic algorithm-Tanabata festival

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.