SPOJ - WMELON(离散化+暴力or离散化+线段树)【待完善】

题意:

给出n个点坐标和他们各自的值,求满足条件"矩形内的点值的和不小于k"的最小矩形面积。

1、离散+暴力

思路,先对y坐标离散化,然后确定两条平行与x轴的直线,求两条直线内满足条件的最小的矩形面积。

n^3的复杂度,过的有点危险。

代码如下:

#include <cstdio>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>

using namespace std;

#define M 1005

struct node{
    int x, y, v;
    bool operator < (const node &xx) const {
        return y<xx.y;
    }
    node(int a, int b, int c): x(a), y(b), v(c){}
};
int n, k, minx, maxx, ans, x[M], y[M];
vector<node>q;
void solve(int i, int j)
{
    int l, r, cur;
    int w = y[j]-y[i];
    l = r = minx;
    cur = x[l];
    while(r<=maxx)
    {
        if(cur<k)
        {
            r++;
            cur+=x[r];
        }
        else
        {
            ans = min(ans, w*(r-l));
            cur-=x[l];
            l++;
        }
    }
}
int main ()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        q.clear();
        scanf("%d %d",&n, &k);
        int a, b, c;
        minx = 0xffffffff;
        maxx = 0;
        for(int i = 0; i < n; ++i)
        {
            scanf("%d %d %d",&a, &b, &c);
            minx = min(minx,a);
            maxx = max(maxx,a);
            y[i] = b;
            q.push_back(node(a,b,c));
        }
        sort(y,y+n);
        sort(q.begin(),q.end());
        int len = unique(y,y+n)-y;
        if(len==1) { printf("0\n"); continue; }
        ans = 0xfffffff;
        for(int i = 0; i < len; ++i)
            for(int j = i+1; j < len; ++j)
            {
                memset(x,0,sizeof(x));
                for(int k = 0; k < (int)q.size(); ++k)
                    if(q[k].y>y[j]) break;
                    else if(q[k].y>=y[i]&&q[k].y<=y[j])
                        x[q[k].x]+=q[k].v;
                solve(i,j);
            }
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用\[1\]中的描述,p1168问题使用了线段解法。在构造的过程中,需要遍历整棵,所以时间复杂度为O(n)。但是在改变一个元素的值时,时间复杂度只有O(log(n))。求和的时候,的节点表示一个索引范围内元素值之和,只要将区间分割对应上,平均时间复杂度是O(log(n)),最坏情况下不会超过O(n*log(n))。\[1\] 根据引用\[2\]中的描述,QUERY 5 12应该是第4、5条边的极大值。\[2\] 根据引用\[3\]中的描述,代码中的if(L<=MID)和else if(R>MID)的判断条件是为了确保查询范围在左子或右子中。如果加上else,会导致错误。\[3\] #### 引用[.reference_title] - *1* [leetCode307:线段解法](https://blog.csdn.net/cyd1999/article/details/123963164)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Spoj 375 Qtree 链剖分 + 线段 解法](https://blog.csdn.net/niuox/article/details/8145842)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值