「hdu6638」Snowy Smile【稀疏矩阵最大子矩阵和】

本文介绍了一种解决最大子矩阵和问题的算法,通过枚举宽度并利用线段树维护子矩阵的最大前缀、后缀、区间最值和区间和,实现高效查找最大子矩阵和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Snowy Smile

Time Limit: 4000/4000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 147 Accepted Submission(s): 48

Problem Description

There are n pirate chests buried in Byteland, labeled by 1,2,…,n.1,2,…,n.1,2,,n. The iii-th chest’s location is (xi,yi)(x_i,y_i)(xi,yi), and its value is wi,wiw_i, w_iwi,wi can be negative since the pirate can add some poisonous gases into the chest. When you open the iii-th pirate chest, you will get wiw_iwi value.

You want to make money from these pirate chests. You can select a rectangle, the sides of which are all paralleled to the axes, and then all the chests inside it or on its border will be opened. Note that you must open all the chests within that range regardless of their values are positive or negative. But you can choose a rectangle with nothing in it to get a zero sum.

Please write a program to find the best rectangle with maximum total value.

Input

The first line of the input contains an integer T(1≤T≤100)T(1\leq T\leq 100)T(1T100), denoting the number of test cases.

In each test case, there is one integer n(1≤n≤2000)n(1\leq n\leq 2000)n(1n2000) in the first line, denoting the number of pirate chests.

For the next n lines, each line contains three integers xi,yi,wi(−109≤xi,yi,wi≤109)x_i,y_i,w_i(−10^9\leq x_i,y_i,w_i\leq 10^9)xi,yi,wi(109xi,yi,wi109), denoting each pirate chest.

It is guaranteed that ∑n≤10000\sum n\leq 10000n10000.

Output

For each test case, print a single line containing an integer, denoting the maximum total value.

Sample Input

2
4
1 1 50
2 1 50
1 2 50
2 2 -500
2
-1 1 5
-1 1 1

Sample Output

100
6

Source

2019 Multi-University Training Contest 6

题意

  • 就是有一个矩阵,这个矩阵中只有200020002000个点有值,其他位置值为0,让你求最大子矩阵和【矩阵大小可以为0】

题解

  • 其实之前牛客多校也出过一个类似的题,处理方法都很套路,就是枚举宽度然后要很快的找最长的那个矩阵,记得上次牛客是枚举宽度+单调队列处理,而这题,对于宽度为[l,r+1][l,r+1][l,r+1]的矩形,其实它的信息可以在宽度为[l,r][l,r][l,r]的子矩形的基础上多加上第r+1r+1r+1行的信息,这个信息就是每一列的和,然后怎么快速知道最大子矩阵呢?还记得线段树入门题目最大子矩阵和吗?只用线段树维护最大前缀,后缀,区间最值,区间和就行了,pushuppushuppushup操作很简单可以参考代码
  • 签到速度不够,题目刷的太少,学过的东西不知道用!!!
#include<bits/stdc++.h>

using namespace std;
const int maxn=2005;
#define inf 0x3f3f3f3f3f3f3f3f
struct node{
    long long pre,suf,sum,maxx;
    node(long long a=0,long long b=0,long long c=0,long long d=0) {
        pre=a;suf=b;sum=c;maxx=d;
    }
}tree[maxn<<2];

struct point{
    int x,y;long long val;
    point(int a=0,int b=0) {
        x=a;y=b;
    }
}p[maxn];

void pushup(int id)
{
    tree[id].pre=max(tree[id<<1].pre,tree[id<<1].sum+tree[id<<1|1].pre);
    tree[id].suf=max(tree[id<<1|1].suf,tree[id<<1|1].sum+tree[id<<1].suf);
    tree[id].maxx=max(tree[id<<1].maxx,max(tree[id<<1|1].maxx,tree[id<<1].suf+tree[id<<1|1].pre));
    tree[id].sum=(tree[id<<1].sum+tree[id<<1|1].sum);
}

void build(int id,int l,int r)
{
    tree[id]=node(0,0,0,0);
    if(l==r) return;
    int mid=(l+r)>>1;
    build(id<<1,l,mid);
    build(id<<1|1,mid+1,r);
}

void update(int id,int L,int R,int pos,long long val)
{
    if(L==R) {
        tree[id].sum+=val;
        tree[id].pre+=val;
        tree[id].suf+=val;
        tree[id].maxx+=val;
        return;
    }
    int mid=(L+R)>>1;
    if(pos<=mid) {
        update(id<<1,L,mid,pos,val);
    }else {
        update(id<<1|1,mid+1,R,pos,val);
    }
    pushup(id);
}


int n,c[maxn],d[maxn];
vector<point> vec[maxn];

int main()
{
    int t;scanf("%d",&t);
    while(t--) {
        scanf("%d",&n);
        for(int i=1;i<=n;i++) {
            scanf("%d %d %lld",&p[i].x,&p[i].y,&p[i].val);
            c[i]=p[i].x;
            d[i]=p[i].y;
        }
        sort(c+1,c+n+1);sort(d+1,d+n+1);
        int tot1=unique(c+1,c+n+1)-c-1,tot2=unique(d+1,d+n+1)-d-1;

        for(int i=1;i<=n;i++) {
            p[i].x=lower_bound(c+1,c+tot1+1,p[i].x)-c;
            p[i].y=lower_bound(d+1,d+tot2+1,p[i].y)-d;

            vec[p[i].x].push_back(p[i]);
        }

        long long ans=0;
        for(int i=1;i<=tot1;i++) {
            build(1,1,tot2);
            for(int j=i;j<=tot1;j++) {
                for(auto p1:vec[j]) {
                    update(1,1,tot2,p1.y,p1.val);
                }
                ans=max(ans,tree[1].maxx);
            }
        }
        printf("%lld\n",ans);

        for(int i=1;i<=tot1;i++) vec[i].clear();
    }
}
### HDU 2544 题目分析 HDU 2544 是关于最短路径的经典问题,可以通过多种方法解决,其中包括基于邻接矩阵的 Floyd-Warshall 算法。以下是针对该问题的具体解答。 --- #### 基于邻接矩阵的 Floyd-Warshall 实现 Floyd-Warshall 算法是一种动态规划算法,适用于计算任意两点之间的最短路径。它的时间复杂度为 \( O(V^3) \),其中 \( V \) 表示节点的数量。对于本题中的数据规模 (\( N \leq 100 \)),此算法完全适用。 下面是具体的实现方式: ```cpp #include <iostream> #include <algorithm> using namespace std; const int INF = 0x3f3f3f3f; int dist[105][105]; int n, m; void floyd() { for (int k = 1; k <= n; ++k) { // 中间节点 for (int i = 1; i <= n; ++i) { // 起始节点 for (int j = 1; j <= n; ++j) { // 结束节点 if (dist[i][k] != INF && dist[k][j] != INF) { dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } } } } } int main() { while (cin >> n >> m && (n || m)) { // 初始化邻接矩阵 for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (i == j) dist[i][j] = 0; else dist[i][j] = INF; } } // 输入边的信息并更新邻接矩阵 for (int i = 0; i < m; ++i) { int u, v, w; cin >> u >> v >> w; dist[u][v] = min(dist[u][v], w); dist[v][u] = min(dist[v][u], w); // 如果是有向图,则去掉这一行 } // 执行 Floyd-Warshall 算法 floyd(); // 输出起点到终点的最短距离 cout << (dist[1][n] >= INF ? -1 : dist[1][n]) << endl; } return 0; } ``` --- #### 关键点解析 1. **邻接矩阵初始化** 使用二维数组 `dist` 存储每一对节点间的最小距离。初始状态下,设所有节点对的距离为无穷大 (`INF`),而同一节点自身的距离为零[^4]。 2. **输入处理** 对于每条边 `(u, v)` 权重 `w`,将其存储至邻接矩阵中,并取较小值以防止重边的影响[^4]。 3. **核心逻辑** Floyd-Warshall 的核心在于三重循环:依次尝试通过中间节点优化其他两节点间的距离关系。具体而言,若从节点 \( i \) 到 \( j \) 可经由 \( k \) 达成更优解,则更新对应位置的值[^4]。 4. **边界条件** 若最终得到的结果仍为无穷大(即无法连通),则返回 `-1`;否则输出实际距离[^4]。 --- #### 性能评估 由于题目限定 \( N \leq 100 \),因此 \( O(N^3) \) 的时间复杂度完全可以接受。此外,空间需求也较低,适合此类场景下的应用。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值