ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Codeforces 1689C. Infected Tree

2022-07-01 19:04:09  阅读:184  来源: 互联网

标签:PII const int Tree 1689C Infected using inf define


传送门

\(\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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有