7-16 森森快递 (30 分)

题目

森森开了一家快递公司,叫森森快递。因为公司刚刚开张,所以业务路线很简单,可以认为是一条直线上的N个城市,这些城市从左到右依次从0到(N−1)编号。由于道路限制,第i号城市(i=0,⋯,N−2)与第(i+1)号城市中间往返的运输货物重量在同一时刻不能超过C​i​​公斤。
公司开张后很快接到了Q张订单,其中j张订单描述了某些指定的货物要从S​j​​号城市运输到T​j​​号城市。这里我们简单地假设所有货物都有无限货源,森森会不定时地挑选其中一部分货物进行运输。安全起见,这些货物不会在中途卸货。
为了让公司整体效益更佳,森森想知道如何安排订单的运输,能使得运输的货物重量最大且符合道路的限制?要注意的是,发货时间有可能是任何时刻,所以我们安排订单的时候,必须保证共用同一条道路的所有货车的总重量不超载。例如我们安排1号城市到4号城市以及2号城市到4号城市两张订单的运输,则这两张订单的运输同时受2-3以及3-4两条道路的限制,因为两张订单的货物可能会同时在这些道路上运输。

输入格式

在这里插入图片描述

输出格式

在这里插入图片描述

输入样例

10 6
0 7 8 5 2 3 1 9 10
0 9
1 8
2 7
6 3
4 5
4 2

输出样例

7

样例提示

我们选择执行最后两张订单,
即把5公斤货从城市4运到城市2,
并且把2公斤货从城市4运到城市5,
就可以得到最大运输量7公斤。

思路: 最开始想暴力写的,但是!!!想想暴力估计没希望,于是想到线段树维护最小值,看到网上题解又知道了要先贪心排列区间,然后再线段树维护最小值,得到最大的可用。

/*
线段树 + 贪心
查找区间最小值(利用线段树),然后先贪心排序(按照区间前后)
*/
/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   '=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
       buddha blesses   never get BUG
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

#define ls(x) x << 1
#define rs(x) x << 1 | 1
typedef long long ll;

const int M = 1e5 + 5;

typedef struct {
	int left, right, lazy;
	ll val;//最小值
}Node; 

typedef struct point {
	point(int xx, int yy) { x = xx; y = yy; }
	int x, y;
}Point;

Node node[M * 4];
vector<Point>vec;
int num[M];

void push_up(int root)
{
	node[root].val = min(node[ls(root)].val, node[rs(root)].val);
}

void push_down(int root)//标记下传
{
	if (node[root].lazy)
	{
		node[ls(root)].lazy += node[root].lazy;
		node[rs(root)].lazy += node[root].lazy;
		node[ls(root)].val += node[root].lazy;
		node[rs(root)].val += node[root].lazy;
		node[root].lazy = 0;
	}
}

void build(int root, int left, int right)//建树
{
	node[root].left = left;
	node[root].right = right;
	node[root].lazy = 0;

	if (left == right)
	{
		node[root].val = num[left];
		return;
	}
	
	int mid = (left + right) / 2;
	build(ls(root), left, mid);
	build(rs(root), mid + 1, right);

	push_up(root);
}

ll query(int root, int left, int right)//查询
{
	if (left <= node[root].left && right >= node[root].right) 
		return node[root].val;

	if (node[root].lazy) push_down(root);

	push_up(root);

	int mid = (node[root].left + node[root].right) / 2;

	ll ans;

	if (right <= mid) ans = query(ls(root), left, right);
	else if (left > mid) ans = query(rs(root), left, right);
	else ans = min(query(rs(root), mid + 1, right), query(ls(root), left, mid));

	return ans;
}

void update(int root, int left, int right, ll val)//修改
{
	if (left <= node[root].left && node[root].right <= right)
	{
		node[root].lazy += val;
		node[root].val += val;
		return;
	}

	if (node[root].lazy) push_down(root);

	int mid = (node[root].left + node[root].right) / 2;

	if (right <= mid) update(ls(root), left, right, val);
	else if (left > mid) update(rs(root), left, right, val);
	else
	{
		update(rs(root), mid + 1, right, val);
		update(ls(root), left, mid, val);
	}

	push_up(root);
}

bool cmp(Point a, Point b)
{
	if (a.y != b.y) return a.y < b.y;
	else a.x < b.x;
}

int main()
{
	int n, q;

	scanf("%d%d", &n, &q);

	for (int i = 1; i < n; ++i)
		scanf("%d", &num[i]);

	build(1, 1, n - 1);

	int a, b;
	ll ans = 0;

	for (int i = 0; i < q; ++i)
	{
		scanf("%d %d", &a, &b);

		if (a > b) swap(a, b);

		vec.push_back(Point(a, b));
	}

	sort(vec.begin(), vec.end(), cmp);

	for (int i = 0; i < q; ++i)
	{
		a = vec[i].x;
		b = vec[i].y;

		if (a == b) continue;

		ll cur = query(1, a + 1, b);

		ans += cur;

		update(1, a + 1, b, -cur);
	}

	printf("%lld\n", ans);

	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值