【Henu ACM Round#24 B】Gargari and Bishops

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

如果写过n皇后问题。 肯定都知道 某个点(i,j)和它在同一条对角线上的点分别是i+j的值和i-j的值相同的点。

然后会发现选择的两个点其实就对应了两组i+j和i-j
且每组i+j和i-j
i+j的奇偶性和i-j的奇偶性要是一样的
假设第一组i+j和i-j的奇偶性都是x
第二组i+j和i-j的奇偶性是y
那么x和y要不一样才行。
不然会有重复的点。
会发现只要满足这个就能不重复了。
(画图就知道了

那么我们处理出来i+j和i-j的所有和就好。
排个序然后两重循环找最大的奇和最大的偶就好。。
(i+j和i-j最多2*n-1组,所以O(N^2)是可以的

【代码】

#include <bits/stdc++.h>
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define all(x) x.begin(),x.end()
#define pb push_back
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;

const double pi = acos(-1);
const int dx[4] = {0,0,1,-1};
const int dy[4] = {1,-1,0,0};
const int N = 2000;
const LL INF = 1e16;

int n;
LL a[N+10][N+10];
map<int,LL> zheng,fu;
vector<pair<LL,int> > v1,v2;

int main(){
	#ifdef LOCAL_DEFINE
	    freopen("rush_in.txt", "r", stdin);
	#endif
	ios::sync_with_stdio(0),cin.tie(0);
    cin >> n;
    rep1(i,1,n)
        rep1(j,1,n)
            cin >> a[i][j];

    rep1(i,1,n)
        rep1(j,1,n){
            zheng[i-j]+=a[i][j];
            fu[i+j]+=a[i][j];
        }

    for (auto temp:zheng){
        v1.push_back(make_pair(temp.second,temp.first));
    }

    for (auto temp:fu){
        v2.push_back(make_pair(temp.second,temp.first));
    }
    sort(v1.begin(),v1.end());
    reverse(all(v1));

    sort(all(v2));
    reverse(all(v2));

    vector<int> ans;ans.clear();
    LL tt = 0;
    int tempx = -1,tempy = -1;LL ttt = -INF;

    for (auto x:v1){
        if (x.second&1){
            for (auto y:v2)
                if (y.second&1){
                    int i = (x.second+y.second)/2;
                    int j = (y.second-x.second)/2;
                    if (i>=1 && i<=n && j>=1 &&j<=n) {
                        LL temp = x.first+y.first-a[i][j];
                        if (temp>ttt){
                            ttt = temp;
                            tempx = i,tempy = j;
                        }
                    }
                }
        }
    }
    tt+=ttt;
    ans.push_back(tempx),ans.push_back(tempy);

    tempx = -1,tempy = -1;ttt = -INF;

    for (auto x:v1){
        if ((x.second&1)==0){
            for (auto y:v2)
                if ((y.second&1)==0){
                    int i = (x.second+y.second)/2;
                    int j = (y.second-x.second)/2;
                    if (i>=1 && i<=n && j>=1 &&j<=n) {
                        LL temp = x.first+y.first-a[i][j];
                        if (temp>ttt){
                            ttt = temp;
                            tempx = i,tempy = j;
                        }
                    }
                }
        }
    }
    tt+=ttt;
    ans.push_back(tempx),ans.push_back(tempy);

    cout<<tt<<endl;
    for (int x:ans)cout<<x<<' ';
	return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/8835570.html