其他分享
首页 > 其他分享> > Codeforces 1689C. Infected Tree

Codeforces 1689C. Infected Tree

作者:互联网

传送门

\(\texttt{Difficulty:1900}\)

题目大意

\(n\cdot m(1\le n,m\le1000)\) 的矩阵 \(A\) ,\(A_ij\) 为 W 或者 B 。设一个点到所有 B 点的曼哈顿距离的最大值为 \(x\) ,求 \(x\) 最小的点 \((i,j)\) 。

思路

考虑只有最右下,左上,左下,右上这 \(4\) 个黑点会起作用,其他的黑点一定可以换成这 \(4\) 个中的其中一个让距离更大,这 \(4\) 个点就分别为 \(i+j\) 最大,最小,和 \(i-j\) 最大最小的黑点,于是求出这些点后再遍历所有的点,求出答案即可,复杂度 \(O(nm)\) 。

代码

#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
using namespace std;
using LL = long long;
using LD = long double;
using ULL = unsigned long long;
using PII = pair<int, int>;
using TP = tuple<int, int, int>;
#define all(x) x.begin(),x.end()
#define mst(x,v) memset(x,v,sizeof(x))
#define mk make_pair
//#define int LL
//#define lc P*2
//#define rc P*2+1
#define endl '\n'
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#pragma warning(disable : 4996)
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
const double eps = 1e-8;
const LL MOD = 1000000009;
const LL mod = 998244353;
const int maxv = 300010;
const int maxn = 1010;

int T, N, M;
char A[maxn][maxn];

void solve()
{
    PII a, b, c, d;
    int aa = -inf, bb = inf, cc = -inf, dd = inf;
    for (int i = 1; i <= N; i++)
    {
        for (int j = 1; j <= M; j++)
        {
            if (A[i][j] == 'W')
                continue;
            if (i + j > aa)
                aa = i + j, a = PII(i, j);
            if (i + j < bb)
                bb = i + j, b = PII(i, j);
            if (i - j > cc)
                cc = i - j, c = PII(i, j);
            if (i - j < dd)
                dd = i - j, d = PII(i, j);
        }
    }
    PII ans;
    int mi = inf;
    for (int i = 1; i <= N; i++)
    {
        for (int j = 1; j <= M; j++)
        {
            int tmp = 0;
            tmp = max(abs(i - a.first) + abs(j - a.second), tmp);
            tmp = max(abs(i - b.first) + abs(j - b.second), tmp);
            tmp = max(abs(i - c.first) + abs(j - c.second), tmp);
            tmp = max(abs(i - d.first) + abs(j - d.second), tmp);
            if (tmp < mi)
                mi = tmp, ans = PII(i, j);
        }
    }
    cout << ans.first << ' ' << ans.second << endl;
}

int main()
{
    IOS;
    cin >> T;
    while (T--)
    {
        cin >> N >> M;
        for (int i = 1; i <= N; i++)
        {
            for (int j = 1; j <= M; j++)
                cin >> A[i][j];
        }
        solve();
    }

    return 0;
}

标签:PII,const,int,Tree,1689C,Infected,using,inf,define
来源: https://www.cnblogs.com/Prgl/p/16435673.html