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
以下是对你提供的日志的详细解析: --- ### 📄 日志内容 这是一段典型的 Android 系统 **native crash(本地崩溃)堆栈日志**,发生在 `/vendor/lib64/libllvm-qgl.so` 模块中,与 GPU 渲染相关(Adreno GPU 驱动)。 崩溃发生在进程 `10201` 的线程 `17566` 中,调用栈显示崩溃发生在 `libllvm-qgl.so` 和 `vulkan.adreno.so` 模块之间,具体是 `vkCreateGraphicsPipelines` 函数中。 --- ### 🔍 逐行解析 #### 崩溃信息头 ``` 08-31 12:42:52.997 10201 17566 17566 F DEBUG : ``` - **时间戳**:`08-31 12:42:52.997` - **PID**:`10201`(进程 ID) - **TID**:`17566`(线程 ID) - **Tag**:`DEBUG` - **优先级**:`F`(Fatal 错误) #### 调用栈(Call Stack) ``` #00 pc 000000000037ea70 /vendor/lib64/libllvm-qgl.so (!!!0000!3e03f5085a129f46fa8ed3d1f1d2aa!eda8e4c1fb!+592) ``` - **`pc`**:程序计数器(Program Counter)地址,表示当前执行的指令位置。 - **`libllvm-qgl.so`**:Adreno GPU 的 LLVM 编译器模块。 - **`!!!0000!...`**:符号混淆信息,通常是被剥离(stripped)后的调试信息占位符。 - **`+592`**:表示该地址相对于函数起始位置的偏移量(单位为字节)。 后续栈帧类似,表示函数调用链,最终在 `vulkan.adreno.so` 的 `vkCreateGraphicsPipelines` 函数中触发崩溃。 --- ### 🧠 上下文分析 - **`vkCreateGraphicsPipelines` 是 Vulkan API 的一个函数**,用于创建图形管线。 - 此崩溃表明在创建图形管线时发生了非法访问、无效参数、资源泄漏或驱动 Bug。 - 由于崩溃发生在 `libllvm-qgl.so` 和 `vulkan.adreno.so` 中,可能是: - GPU 驱动 Bug - 应用使用了非法的 Vulkan 资源(如无效的着色器模块、渲染通道) - GPU 内存分配失败或锁存内存不足 - 编译器优化错误(LLVM 编译器模块) --- ### 📁 相关模块说明 | 模块 | 说明 | |------|------| | `libllvm-qgl.so` | Adreno GPU 的 LLVM 编译器模块,负责将着色器编译为 GPU 可执行代码 | | `vulkan.adreno.so` | Adreno GPU 的 Vulkan 实现,提供 Vulkan API 的底层接口 | | `vkCreateGraphicsPipelines` | Vulkan API 函数,用于创建图形管线对象 | --- ### ✅ 最佳实践建议 #### 1. **符号化堆栈** - 使用 `addr2line` 或 Qualcomm 提供的工具对 `pc` 地址进行符号化,找到具体出错函数。 ```bash aarch64-linux-android-addr2line -e libllvm-qgl.so 000000000037ea70 ``` #### 2. **检查 BuildId** - 每个 `.so` 文件都有唯一的 `BuildId`,可用于匹配符号表: ``` BuildId: ca318b524af7b4a7abac8e8946098cf9 ``` 确保你有对应版本的符号文件(`.so` + `.debug`)才能准确调试。 #### 3. **使用调试工具** - **Qualcomm Adreno SDK**:提供 GPU Profiler、Shader Analyzer 等工具。 - **RenderDoc**:Vulkan 图形调试工具,可捕获帧并分析崩溃原因。 - **Android Studio + LLDB**:用于调试 native 崩溃。 #### 4. **检查应用代码** - 如果是你开发的应用,检查是否: - 使用了非法的 Vulkan 资源(如未正确创建的着色器、渲染通道) - 没有正确释放资源导致内存泄漏 - 使用了不兼容的 GPU 功能(如某些设备不支持的扩展) --- ### 💡 常见修复方向 | 问类型 | 修复建议 | |----------|----------| | 驱动 Bug | 升级系统、更新 GPU 驱动、反馈给 Qualcomm | | 应用错误 | 检查 Vulkan API 调用合法性,使用验证层调试 | | 内存问 | 检查内存分配是否超出限制,锁存内存是否过高 | | Shader 编译错误 | 使用 `qcom-llvm` 工具分析着色器编译日志 | ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值