zoukankan      html  css  js  c++  java
  • Codeforces 463C Gargari and Bishops 题解

    题目出处: http://codeforces.com/contest/463/problem/C


    感觉本题还是挺难的。须要好好总结一下。

    计算对角线的公式:

    1 右斜对角线,也叫主对角线的下标计算公式: int index = j-i; 可是为了不使用绝对值,而且分开j = 2, i = 1和j = 1, i = 2的情况,那么公式变为: j-i+n,正方形n为行数或列数

    2 左对角线。也叫第二对角线: int index = i+j;

    例如以下图计算右斜对角线:


    这样计算下标。是为了方便计算每一格斜线的和值。

    计算好每一斜行的和值并存储好是为了方便计算每一格的交叉线的和值。

    计算一格交叉线和值公式:arr[i][j] = d1[i+j] + d2[i-j+N] - arr[i][j];//巧妙地求本格的交叉


    最后为了满足相互之间不攻击,这个推断条件还是很巧妙的就是i+j的奇偶不同才干组合。假设奇偶同样就不能组合。

    没想通之前,真的好难总结出来。只是细致想想。事实上也非常easy,在方格里面举几个样例就知道了。

    #include <stdio.h>
    #include <vector>
    #include <string.h>
    #include <algorithm>
    #include <iostream>
    #include <string>
    #include <limits.h>
    #include <stack>
    #include <queue>
    #include <set>
    #include <map>
    using namespace std;
    
    long long d1[4100], d2[4100];
    long long arr[2100][2100];
    int N;
    
    int main()
    {
    	scanf("%d", &N);
    	for (int i = 1; i <= N; i++)
    	{
    		for (int j = 1; j <= N; j++)
    		{
    			scanf("%I64d", &arr[i][j]);
    			d1[i+j] += arr[i][j];//注意记熟斜对角线的计算表示方法
    			d2[i-j+N] += arr[i][j];
    		}
    	}
    	for (int i = 1; i <= N; i++)
    	{
    		for (int j = 1; j <= N; j++)
    		{
    			arr[i][j] = d1[i+j] + d2[i-j+N] - arr[i][j];//巧妙地求本格的交叉
    		}
    	}
    	int c[4] = {1, 1, 1, 2};
    	for (int i = 1; i <= N; i++)
    	{//一个间隔有条件组合抽象思维
    		for (int j = 1; j <= N; j++)
    		{
    			if ((i+j) & 1)//巧用奇偶排除相互attack的条件
    			{
    				if (arr[i][j] > arr[c[2]][c[3]]) c[2] = i, c[3] = j;
    			}
    			else if (arr[i][j] > arr[c[0]][c[1]]) c[0] = i, c[1] = j;
    		}//选择符合条件的两个最大值组合起来
    	}
    	long long res = arr[c[0]][c[1]] + arr[c[2]][c[3]];
    	printf("%I64d
    %d %d %d %d
    ", res, c[0], c[1], c[2], c[3]) ;
    	return 0;
    }


  • 相关阅读:
    同一个IIS绑定多个Htts 站点问题
    开发小程序心得
    在阿里云里申请免费Https证书SSL
    request:fail 小程序要求的 TLS 版本必须大于等于 1.2
    js封装Cookie操作
    数据库日志文件(databasename_log.ldf)太大 如何清除
    sql with(lock) 与事务
    java.lang.VerifyError: com/google/android/gms/measurement/internal/zzw
    Eclipse 导入 Android studio Exception Ljava/lang/UnsatisfiedLinkEror
    Eclipse工程 导入 Android Studio
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/6819460.html
Copyright © 2011-2022 走看看