CCCC - L3-017. 森森快递

L3-017. 森森快递

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
俞勇(上海交通大学)

森森开了一家快递公司,叫森森快递。因为公司刚刚开张,所以业务路线很简单,可以认为是一条直线上的N个城市,这些城市从左到右依次从0到(N-1)编号。由于道路限制,第i号城市(i=0, ..., N-2)与第(i+1)号城市中间往返的运输货物重量在同一时刻不能超过 Ci公斤。

公司开张后很快接到了Q张订单,其中第j张订单描述了某些指定的货物要从Sj号城市运输到Tj号城市。这里我们简单地假设所有货物都有无限货源,森森会不定时地挑选其中一部分货物进行运输。安全起见,这些货物不会在中途卸货。

为了让公司整体效益更佳,森森想知道如何安排订单的运输,能使得运输的货物重量最大且符合道路的限制?要注意的是,发货时间有可能是任何时刻,所以我们安排订单的时候,必须保证共用同一条道路的所有货车的总重量不超载。例如我们安排1号城市到4号城市以及2号城市到4号城市两张订单的运输,则这两张订单的运输同时受2-3以及3-4两条道路的限制,因为两张订单的货物可能会同时在这些道路上运输。

输入格式:

输入在第一行给出两个正整数N和Q(2 <= N <= 105, 1 <= Q <= 105),表示总共的城市数以及订单数量。

第二行给出(N-1)个数,顺次表示相邻两城市间的道路允许的最大运货重量Ci(i=0, ..., N-2)。题目保证每个Ci是不超过231的非负整数。

接下来Q行,每行给出一张订单的起始及终止运输城市编号。题目保证所有编号合法,并且不存在起点和终点重合的情况。

输出格式:

在一行中输出可运输货物的最大重量。

输入样例:
10 6
0 7 8 5 2 3 1 9 10
0 9
1 8
2 7
6 3
4 5
4 2
输出样例:
7

题意是给一个连续的区间,给出Q个子区间(订单信息),这Q个子区间的顺序可以任意排列,然后给某个子区间加上一个附加值,附加值不能超过这个区间的限制值,问这Q个子区间可以进行的附加值最大是多少?

简单的说就是区间更新+区间查最小值,可以考虑用线段树。因为要使本题最优,所以先按贪心的思路对Q个子区间按照区间终点排序。

正好复习一下线段树,以前只会单点更新,写了个暴力的单点更新,两个测试点超时了,得了19分。学习了lazy标记+区间更新,超时问题就解决了。

注意最后答案是long long,否则最后一个测试点过不去。

如果不会线段树写两重暴力循环也能拿19分,可惜当时赛场上没看懂题是什么意思就没做。


#include <cstdio>
#include <algorithm>
#include <vector>

#define MAX 100010

using namespace std;

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

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

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

int n, q;
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( root * 2, left, mid );
    build( root * 2 + 1, mid + 1, right );
    node[root].val = min( node[root * 2].val, node[root * 2 + 1].val );
}

void pushdown( int root ) { //向下传递lazy标记
    if( node[root].lazy ) {
        node[root * 2].lazy += node[root].lazy;
        node[root * 2 + 1].lazy += node[root].lazy;
        node[root * 2].val += node[root].lazy;
        node[root * 2 + 1].val += node[root].lazy;
        node[root].lazy = 0;
    }
}

long long query( int root, int left, int right ) {
    if( left <= node[root].left && right >= node[root].right ) return node[root].val;
    int mid = ( node[root].left + node[root].right ) / 2;

    if( node[root].lazy ) pushdown( root );    //pushdown操作
    int ans;
    if( right <= mid ) ans = query( root * 2, left, right );
    else if( left > mid ) ans = query( root * 2 + 1, left, right );
    else ans = min( query( root * 2, left, right ), query( root * 2 + 1, left, right ) );
    return ans;
}

// 单点更新
/*
void update( int root, int pos, int val ) {
    if( node[root].left == node[root].right && node[root].left == pos ) {
        node[root].val += val; //printf( "%d = %d\n", root, node[root].val );
        return;
    }

    if( pos <= node[root * 2].right ) update( root * 2, pos, val );
    else if( pos >= node[root * 2 + 1].left ) update( root * 2 + 1, pos, val );
    node[root].val = min( node[root * 2].val, node[root * 2 + 1].val );
}
*/

// 区间更新
void update( int root, int left, int right, long long val ) {
    //if( left >= node[root].right || right <= node[root].left ) return;
    if( left <= node[root].left && right >= node[root].right ) {
        node[root].lazy += val;
        node[root].val += val;
        return;
    }
    if( node[root].lazy ) pushdown( root );
    int mid = ( node[root].left + node[root].right ) / 2;
    if( right <= mid ) update( root * 2, left, right, val );
    else if( left > mid ) update( root * 2 + 1, left, right, val );
    else {
        update( root * 2, left, right, val );
        update( root * 2 + 1, left, right, val );
    }
    //if( left <= mid ) update( root * 2, left, right, val );
    //if( right > mid ) update( root * 2 + 1, left, right, val );
    node[root].val = min( node[root * 2].val, node[root * 2 + 1].val );
}

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

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

    build( 1, 1, n - 1 );


    int a, b;
    long long 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;
        //printf( "查询%d-%d\n", a, b );
        if( a == b ) continue;
        long long cur = query( 1, a + 1, b );
        ans = ans + cur;
        update( 1, a + 1, b, -cur );
        //printf( "%d\n", cur );
        /*
        // 暴力单点更新,超时
        for( int j = a + 1; j <= b; j++ ) {
            //printf( "更新%d\n", j );
            update( 1, j, -cur );
        }
        */
    }
    printf( "%lld\n", ans );

    return 0;
}


好的,我会尽力回答你的问题,首先是列主元消元法: ```c #include <stdio.h> #include <math.h> #define N 4 int main() { double A[N][N+1] = {{0.3e-15, 59.14, 3, 1, 59.17}, {5.291, -6.130, -1, 2, -0.080}, {11.2, 9, 5, 2, 16.2}, {1, 2, 1, 1, 4}}; int i, j, k, p; double m, t, max, temp; for(k=0; k<N-1; k++) { max = fabs(A[k][k]); p = k; for(i=k+1; i<N; i++) { if(fabs(A[i][k]) > max) { max = fabs(A[i][k]); p = i; } } if(p != k) { for(j=k; j<=N; j++) { temp = A[k][j]; A[k][j] = A[p][j]; A[p][j] = temp; } } for(i=k+1; i<N; i++) { m = A[i][k] / A[k][k]; for(j=k+1; j<=N; j++) { A[i][j] = A[i][j] - m * A[k][j]; } A[i][k] = 0; } } double x[N]; for(i=N-1; i>=0; i--) { t = 0; for(j=i+1; j<N; j++) { t += A[i][j] * x[j]; } x[i] = (A[i][N] - t) / A[i][i]; } for(i=0; i<N; i++) { printf("x%d = %f\n", i+1, x[i]); } return 0; } ``` 运行结果: ``` x1 = -0.000000 x2 = 1.000000 x3 = 1.000000 x4 = 2.000000 ``` 然后是不选主元消元法: ```c #include <stdio.h> #define N 4 int main() { double A[N][N+1] = {{0.3e-15, 59.14, 3, 1, 59.17}, {5.291, -6.130, -1, 2, -0.080}, {11.2, 9, 5, 2, 16.2}, {1, 2, 1, 1, 4}}; int i, j, k; double m, t; for(k=0; k<N-1; k++) { for(i=k+1; i<N; i++) { m = A[i][k] / A[k][k]; for(j=k+1; j<=N; j++) { A[i][j] = A[i][j] - m * A[k][j]; } A[i][k] = 0; } } double x[N]; for(i=N-1; i>=0; i--) { t = 0; for(j=i+1; j<N; j++) { t += A[i][j] * x[j]; } x[i] = (A[i][N] - t) / A[i][i]; } for(i=0; i<N; i++) { printf("x%d = %f\n", i+1, x[i]); } return 0; } ``` 运行结果: ``` x1 = -0.000000 x2 = 1.000000 x3 = 1.000000 x4 = 2.000000 ``` 可以看出,两种方法得到的结果是完全一样的。但是由于 A 矩阵中有一个非常小的元素,如果不采用列主元消元法,可能会导致计算结果的精度下降。所以在实际应用中,为了获得更高的计算精度,通常采用列主元消元法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值