题解 | #合并果子#

合并果子

https://www.nowcoder.com/practice/854e7118eb08464ab8ce7a0bca8b276c

解题思路

这是一道关于二维平面上点的合并问题。主要思路如下:

  1. 问题分析:

    • 个果子在二维平面上,每个果子有位置和重量
    • 每次合并消耗的体力为 乘 Manhattan距离
    • 需要找到一个最优的合并中心点,使得总体力消耗最小
  2. 解题思路:

    • 利用重量中位数性质找到最优合并点
    • 分别在 轴和 轴上找到重量中位数对应的坐标
    • 选取距离该点最近的几个点作为候选合并中心
    • 计算所有点到每个候选中心的总体力消耗,取最小值
  3. 关键点:

    • 坐标和 坐标分别排序
    • 找到重量中位数对应的位置
    • 选取靠近重量中心的前10个点作为候选点

代码

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

struct node {
    int x, y, w;
};

// 找到重量中位数对应的点
node find(vector<node>& pos, LL total) {
    LL target = total >> 1;
    LL tmp = 0;
    for(auto it : pos) {
        tmp += it.w;
        if(tmp > target) return it;
    }
    return pos.back();
}

int main() {
    int n;
    cin >> n;
    vector<node> pos(n);
    LL total = 0;
    for(int i = 0; i < n; ++i) {
        cin >> pos[i].x >> pos[i].y >> pos[i].w;
        total += pos[i].w;
    }
    
    // 找x坐标的重量中位数
    sort(pos.begin(), pos.end(), [](node& a, node& b) { return a.x < b.x; });
    int x = find(pos, total).x;
    
    // 找y坐标的重量中位数
    sort(pos.begin(), pos.end(), [](node& a, node& b) { return a.y < b.y; });
    int y = find(pos, total).y;
    
    // 按到重量中心的距离排序
    sort(pos.begin(), pos.end(), [=](node& a, node& b) {
        return abs(a.x - x) + abs(a.y - y) < abs(b.x - x) + abs(b.y - y);
    });
    
    // 计算最小体力消耗
    LL res = LLONG_MAX;
    for(int j = 0; j < min(10, n); ++j) {
        LL tmp = 0;
        for(auto it : pos) {
            tmp += (LL)it.w * (abs(it.x - pos[j].x) + abs(it.y - pos[j].y));
        }
        res = min(res, tmp);
    }
    cout << res << endl;
    return 0;
}
import java.util.*;

public class Main {
    static class Node {
        int x, y, w;
        Node(int x, int y, int w) {
            this.x = x;
            this.y = y;
            this.w = w;
        }
    }
    
    static Node find(List<Node> pos, long total) {
        long target = total >> 1;
        long tmp = 0;
        for(Node node : pos) {
            tmp += node.w;
            if(tmp > target) return node;
        }
        return pos.get(pos.size() - 1);
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        List<Node> pos = new ArrayList<>();
        long total = 0;
        
        for(int i = 0; i < n; i++) {
            Node node = new Node(sc.nextInt(), sc.nextInt(), sc.nextInt());
            pos.add(node);
            total += node.w;
        }
        
        // 找x坐标的重量中位数
        pos.sort((a, b) -> a.x - b.x);
        int x = find(pos, total).x;
        
        // 找y坐标的重量中位数
        pos.sort((a, b) -> a.y - b.y);
        int y = find(pos, total).y;
        
        // 按到重量中心的距离排序
        final int finalX = x, finalY = y;
        pos.sort((a, b) -> {
            int distA = Math.abs(a.x - finalX) + Math.abs(a.y - finalY);
            int distB = Math.abs(b.x - finalX) + Math.abs(b.y - finalY);
            return distA - distB;
        });
        
        // 计算最小体力消耗
        long res = Long.MAX_VALUE;
        for(int j = 0; j < Math.min(10, n); j++) {
            long tmp = 0;
            Node center = pos.get(j);
            for(Node node : pos) {
                tmp += (long)node.w * (Math.abs(node.x - center.x) + Math.abs(node.y - center.y));
            }
            res = Math.min(res, tmp);
        }
        System.out.println(res);
    }
}
class Node:
    def __init__(self, x, y, w):
        self.x = x
        self.y = y
        self.w = w

def find(pos, total):
    target = total >> 1
    tmp = 0
    for node in pos:
        tmp += node.w
        if tmp > target:
            return node
    return pos[-1]

n = int(input())
pos = []
total = 0

# 读入数据
for _ in range(n):
    x, y, w = map(int, input().split())
    pos.append(Node(x, y, w))
    total += w

# 找x坐标的重量中位数
pos.sort(key=lambda node: node.x)
x = find(pos, total).x

# 找y坐标的重量中位数
pos.sort(key=lambda node: node.y)
y = find(pos, total).y

# 按到重量中心的距离排序
pos.sort(key=lambda node: abs(node.x - x) + abs(node.y - y))

# 计算最小体力消耗
res = float('inf')
for j in range(min(10, n)):
    tmp = 0
    for node in pos:
        tmp += node.w * (abs(node.x - pos[j].x) + abs(node.y - pos[j].y))
    res = min(res, tmp)

print(res)

算法及复杂度分析

  • 算法:排序
  • 时间复杂度:
    • 排序需要
    • 查找候选点需要
    • 计算消耗需要
  • 空间复杂度:
全部评论

相关推荐

头像
10-13 18:10
已编辑
东南大学 C++
。收拾收拾心情下一家吧————————————————10.12更新上面不知道怎么的,每次在手机上编辑都会只有最后一行才会显示。原本不想写凉经的,太伤感情了,但过了一天想了想,凉经的拿起来好好整理,就像象棋一样,你进步最快的时候不是你赢棋的时候,而是在输棋的时候。那废话不多说,就做个复盘吧。一面:1,经典自我介绍2,项目盘问,没啥好说的,感觉问的不是很多3,八股问的比较奇怪,他会深挖性地问一些,比如,我知道MMU,那你知不知道QMMU(记得是这个,总之就是MMU前面加一个字母)4,知不知道slab内存分配器-&gt;这个我清楚5,知不知道排序算法,排序算法一般怎么用6,写一道力扣的,最长回文子串反问:1,工作内容2,工作强度3,关于友商的问题-&gt;后面这个问题问HR去了,和中兴有关,数通这个行业和友商相关的不要提,这个行业和别的行业不同,别的行业干同一行的都是竞争关系,数通这个行业的不同企业的关系比较微妙。特别细节的问题我确实不知道,但一面没挂我。接下来是我被挂的二面,先说说我挂在哪里,技术性问题我应该没啥问题,主要是一些解决问题思路上的回答,一方面是这方面我准备的不多,另一方面是这个面试写的是“专业面试二面”,但是感觉问的问题都是一些主管面/综合面才会问的问题,就是不问技术问方法论。我以前形成的思维定式就是专业面会就是会,不会就直说不会,但事实上如果问到方法论性质的问题的话得扯一下皮,不能按照上面这个模式。刚到位置上就看到面试官叹了一口气,有一些不详的预感。我是下午1点45左右面的。1,经典自我介绍2,你是怎么完成这个项目的,分成几个步骤。我大致说了一下。你有没有觉得你的步骤里面缺了一些什么,(这里已经在引导我往他想的那个方向走了),比如你一个人的能力永远是不够的,,,我们平时会有一些组内的会议来沟通我们的所思所想。。。。3,你在项目中遇到的最困难的地方在什么方面4,说一下你知道的TCP/IP协议网络模型中的网络层有关的协议......5,接着4问,你觉得现在的socket有什么样的缺点,有什么样的优化方向?6,中间手撕了一道很简单的快慢指针的问题。大概是在链表的倒数第N个位置插入一个节点。————————————————————————————————————10.13晚更新补充一下一面说的一些奇怪的概念:1,提到了RPC2,提到了fu(第四声)拷贝,我当时说我只知道零拷贝,知道mmap,然后他说mmap是其中的一种方式,然后他问我知不知道DPDK,我说不知道,他说这个是一个高性能的拷贝方式3,MMU这个前面加了一个什么字母我这里没记,别问我了4,后面还提到了LTU,VFIO,孩子真的不会。
走呀走:华子二面可能会有场景题的,是有些开放性的问题了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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