cf B. Guess That Car!

本文介绍了一种通过独立最小化矩阵中特定表达式的算法,该算法能够有效地找到使矩阵方差达到最小值的参数x和y。文章详细阐述了算法的原理,并提供了一个具体的实现示例。

刚开始根本读不懂题目。。。


官方题解

We need to find such x and y that the value of is minimum possible. This expression can be rewritten as . Note that the first part doesn't depend on y and the second part doesn't depend on x, so we can minimize these parts separately. Here is how to minimize , the second part is minimized similarly. As the expression in the brackets doesn't depend on j, this part can be rewritten as , where . Now it's enough to calculate the required value for all possible values of x and choose x for which this value is the smallest. The optimal value of y can be found similarly.

The overall complexity of this solution is O(n·m + n2 + m2).

As the objective function is convex, other approaches to this problem are possible, for example, ternary search, gradient descent or analytical approach (calculation of derivatives).


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
#define N 1010
const long long inf=1LL<<60;
int n,m,a[N][N];
long long dx[N],dy[N];
int main()
{
    scanf("%d%d",&n,&m);
    memset(dx,0,sizeof(dx));
    memset(dy,0,sizeof(dy));
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            scanf("%d",&a[i][j]);
            dx[i]+=a[i][j];
            dy[j]+=a[i][j];
        }
    }
    long long mx=inf,my=inf;
    int nx,ny;
    for(int i=0;i<=n;i++)
    {
        long long ans=0;
        for(int j=0;j<n;j++)
        {
           ans+=(4*i-4*j-2)*(4*i-4*j-2)*dx[j];
        }
        if(ans<mx)
        {mx=ans;nx=i;}
    }
    for(int i=0;i<=m;i++)
    {
        long long ans=0;
        for(int j=0;j<m;j++)
        {
            ans+=(4*i-4*j-2)*(4*i-4*j-2)*dy[j];
        }
        if(ans<my)
        {my=ans;ny=i;}
    }
    printf("%I64d\n",mx+my);
    printf("%d %d\n",nx,ny);
    return 0;
}



08-31 12:42:52.997 10201 17566 17566 F DEBUG : #00 pc 000000000037ea70 /vendor/lib64/libllvm-qgl.so (!!!0000!3e03f5085a129f46fa8ed3d1f1d2aa!eda8e4c1fb!+592) (BuildId: ca318b524af7b4a7abac8e8946098cf9) 08-31 12:42:52.997 10201 17566 17566 F DEBUG : #01 pc 000000000037dfec /vendor/lib64/libllvm-qgl.so (!!!0000!1d0c40ad4baefbab8a24e6fc7c5eba!eda8e4c1fb!+588) (BuildId: ca318b524af7b4a7abac8e8946098cf9) 08-31 12:42:52.997 10201 17566 17566 F DEBUG : #02 pc 0000000000507be4 /vendor/lib64/libllvm-qgl.so (!!!0000!1acb0ec3d67182b35dbf5099bbc199!eda8e4c1fb!+164) (BuildId: ca318b524af7b4a7abac8e8946098cf9) 08-31 12:42:52.997 10201 17566 17566 F DEBUG : #03 pc 0000000000507aa8 /vendor/lib64/libllvm-qgl.so (!!!0000!830669e960b911e7d9f628ab0eae17!eda8e4c1fb!+72) (BuildId: ca318b524af7b4a7abac8e8946098cf9) 08-31 12:42:52.997 10201 17566 17566 F DEBUG : #04 pc 000000000099cffc /vendor/lib64/libllvm-qgl.so (!!!0000!a6c4e54c31882eaccb65b97888ceb7!eda8e4c1fb!+108) (BuildId: ca318b524af7b4a7abac8e8946098cf9) 08-31 12:42:52.997 10201 17566 17566 F DEBUG : #05 pc 000000000099cf80 /vendor/lib64/libllvm-qgl.so (!!!0000!7576c1e64343062569f4cb917ac9cd!eda8e4c1fb!+16) (BuildId: ca318b524af7b4a7abac8e8946098cf9) 08-31 12:42:52.997 10201 17566 17566 F DEBUG : #06 pc 0000000000f26de8 /vendor/lib64/libllvm-qgl.so (!!!0000!13a0d3a1239e7c78308c1116a0936d!eda8e4c1fb!+1352) (BuildId: ca318b524af7b4a7abac8e8946098cf9) 08-31 12:42:52.997 10201 17566 17566 F DEBUG : #07 pc 0000000000edb778 /vendor/lib64/libllvm-qgl.so (!!!0000!5b183a50d5b6220e2148abbc5665b8!eda8e4c1fb!+2296) (BuildId: ca318b524af7b4a7abac8e8946098cf9) 08-31 12:42:52.997 10201 17566 17566 F DEBUG : #08 pc 0000000000ee291c /vendor/lib64/libllvm-qgl.so (!!!0000!2a0e64fac242bfae72d2abfa8ce29b!eda8e4c1fb!+44) (BuildId: ca318b524af7b4a7abac8e8946098cf9) 08-31 12:42:52.997 10201 17566 17566 F DEBUG : #09 pc 00000000011d7190 /vendor/lib64/libllvm-qgl.so (!!!0000!1e9735fa2d7fa7113c5ea09cbdfdc0!eda8e4c1fb!+592) (BuildId: ca318b524af7b4a7abac8e8946098cf9) 08-31 12:42:52.997 10201 17566 17566 F DEBUG : #10 pc 00000000011d6780 /vendor/lib64/libllvm-qgl.so (!!!0000!aefccce6a332610a9b22f30d0961cc!eda8e4c1fb!+736) (BuildId: ca318b524af7b4a7abac8e8946098cf9) 08-31 12:42:52.997 10201 17566 17566 F DEBUG : #11 pc 00000000001b6dc0 /vendor/lib64/hw/vulkan.adreno.so (!!!0000!b46861eb17b97158fc26d3e1e8cc0a!eda8e4c1fb!+2944) (BuildId: 9328836a87ac0ac31a198f14566d30a8) 08-31 12:42:52.997 10201 17566 17566 F DEBUG : #12 pc 00000000001b5fe4 /vendor/lib64/hw/vulkan.adreno.so (!!!0000!2aa5082753cd3c7ad1b8091f24093d!eda8e4c1fb!+420) (BuildId: 9328836a87ac0ac31a198f14566d30a8) 08-31 12:42:52.997 10201 17566 17566 F DEBUG : #13 pc 00000000002187b0 /vendor/lib64/hw/vulkan.adreno.so (qglinternal::vkCreateGraphicsPipelines(VkDevice_T*, VkPipelineCache_T*, unsigned int, VkGraphicsPipelineCreateInfo const*, VkAllocationCallbacks const*, VkPipeline_T**)+17936) (BuildId: 9328836a87ac0ac31a198f14566d30a8)
最新发布
09-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值