Snowy Smile (HDU - 6638,稀疏矩阵子矩阵最大和)

一.题目链接:

HDU-6638

二.题目大意:

求稀疏矩阵子矩阵最大和.

三.分析:

离散 x 坐标,枚举 x.

线段树维护区间最大子段和

将  的算法优化到了 

详见代码.

四.代码实现:

#include <set>
#include <map>
#include <ctime>
#include <queue>
#include <cmath>
#include <stack>
#include <bitset>
#include <vector>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define eps 1e-8
#define lc k * 2
#define rc k * 2 + 1
#define pi acos(-1.0)
#define ll long long
#define ull unsigned long long
using namespace std;

const int M = (int)2e3;
const int mod = 99991;
const int inf = 0x3f3f3f3f;

struct node1
{
    int x, y, w;
}s[M + 5];
int len, y[M + 5];

struct node2
{
    ll sum, max_sum, max_prefix, max_suffix;
}tree[M * 4 + 5];

bool cmp(node1 a, node1 b)
{
    return a.x < b.x;
}

int tofind(int x)
{
    return lower_bound(y + 1, y + len + 1, x) - y;
}

void push_up(int k)
{
    tree[k].sum = tree[lc].sum + tree[rc].sum;
    tree[k].max_sum = max(max(tree[lc].max_sum, tree[rc].max_sum), tree[lc].max_suffix + tree[rc].max_prefix);
    tree[k].max_prefix = max(tree[lc].max_prefix, tree[lc].sum + tree[rc].max_prefix);
    tree[k].max_suffix = max(tree[rc].max_suffix, tree[rc].sum + tree[lc].max_suffix);
}

void update(int k, int l, int r, int pos, int v)
{
    if(l == r)
    {
        tree[k].max_prefix = tree[k].max_suffix = tree[k].max_sum = tree[k].sum += v;
        return;
    }
    int mid = (l + r) >> 1;
    if(pos <= mid)
        update(lc, l, mid, pos, v);
    else
        update(rc, mid + 1, r, pos, v);
    push_up(k);
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        len = 0;
        int n;
        scanf("%d", &n);
        for(int i = 1; i <= n; ++i)
        {
            scanf("%d %d %d", &s[i].x, &s[i].y, &s[i].w);
            y[++len] = s[i].y;
        }
        sort(s + 1, s + n + 1, cmp), sort(y + 1, y + len + 1);
        len = unique(y + 1, y + len + 1) - (y + 1);
        ll ans = 0;
        for(int i = 1; i <= n; ++i)
        {
            if(i != 1 && s[i].x == s[i - 1].x)  continue;///一次性枚举所有 ≥ 此 x 的点.
            memset(tree, 0, sizeof(tree));
            for(int j = i; j <= n; ++j)
            {
                update(1, 1, len, tofind(s[j].y), s[j].w);
                if(j == n || s[j].x != s[j + 1].x)
                ///只有当与该 x 相等的点都加进去之后,才统计最大值.
                    ans = max(ans, tree[1].max_sum);
            }
        }
        printf("%lld\n", ans);
    }
    return 0;
}

 

全部评论

相关推荐

腾讯,字节一面挂,拼多多笔试挂,淘天笔试挂,阿里云一面挂,阿里国际一面挂,准备了这么久终究还是什么都得不到吗。。。
迷茫的大四🐶:因为这些都是大家耳熟能详的大厂,各种高学历人才都会投递,但佬学历在这,找个满意的工作不是问题的
投递阿里国际数字商业集团等公司10个岗位
点赞 评论 收藏
分享
来个厂收我吧:首先,市场侧求职我不是很懂。 但是,如果hr把这份简历给我,我会觉得求职人不适合做产品经理。 问题点: 1,简历的字体格式不统一,排版不尽如人意 2,重点不突出,建议参考star法则写个人经历 3,印尼官方货币名称为印度尼西亚卢比(IDR),且GMV690000印尼盾换算为305人民币,总成交额不高。 4,右上角的意向职位在发给其他公司时记得删除。 5,你所有的经历都是新媒体运营,但是你要投市场营销岗位,jd和简历不匹配,建议用AI+提示词,参照多个jd改一下经历内容。 修改建议: 1,统一字体(中文:思源黑体或微软雅黑,英文数字:time new romans),在word中通过表格进行排版(b站学) 2,校招个人经历权重:实习经历=创业经历(大创另算)>项目经历>实训经历>校园经历 3,请将项目经历时间顺序改为倒序,最新的放最上方。 4,求职方向不同,简历文字描述侧重点也需要不同。
点赞 评论 收藏
分享
07-07 12:25
门头沟学院 Java
程序员牛肉:你这个智邮公司做的就是那个乐山市税务系统的服务吗?
点赞 评论 收藏
分享
08-16 22:00
复旦大学 运营
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务