sgu148: B-Station

题目大意:
地铁站里有n个平台,每个平台都有一定的积水w、容量l、炸毁的费用p。如果一个平台被毁坏,其中所有的水都会流向下一个平台。如果一个的积水超过容量,这个平台将会自动冲毁。现在恐怖分子要毁掉最后一个平台,求最小的费用。

暴力:枚举从哪个平台开始炸。
SGU数据是不是变水了。。。。
n15000O(n2) 居然能过。。。。
用时93ms

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

inline int read()
{
    int x = 0, f = 1, t = getchar();
    while(t < '0' || t > '9') t == '-' ? f = -1 : 0, t = getchar();
    while(t >= '0' && t <= '9') x = (x<<1) + (x<<3) + t - '0', t = getchar();
    return x * f;
}

const int maxn = 15005;
const int oo = 0x3f3f3f3f;
int n, w[maxn], l[maxn], p[maxn];

namespace BF
{
    void solve()
    {
        register int i, j, x, cost, flow, ans = oo;
        for(i = 1; i <= n; ++i)
        {
            flow = 0, cost = 0;
            for(j = i; j <= n; ++j)
            {
                flow += w[j];
                if(flow <= l[j]) cost += p[j];
            }
            if(cost < ans) ans = cost, x = i;
        }
        flow = 0;
        for(i = x; i <= n; ++i)
        {
            flow += w[i];
            if(flow <= l[i]) printf("%d\n", i);
        }
    }
}
void init()
{
    register int i;
    n = read();
    for(i = 1; i <= n; ++i)
        w[i] = read(), l[i] = read(), p[i] = read();
}
void work()
{
    BF::solve();
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    init();
    work();

    #ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}

优化:设前 i 个平台的积水之和为为Si,如果从 i 号平台开始炸,j号平台需要炸毁的条件是 SjSi1ljSjljSi1
由于 S 是单调的,我们从后往前扫,用大根堆维护Sjlj,在堆中保留暂时无需炸毁的平台,统计答案即可。
用时15ms

#include <cstdio>
#include <queue>
#define mp make_pair
#define X first
#define Y second
using namespace std;
typedef pair<int, int> pii;

inline int read()
{
    int x = 0, f = 1, t = getchar();
    while(t < '0' || t > '9') t == '-' ? f = -1 : 0, t = getchar();
    while(t >= '0' && t <= '9') x = (x<<1) + (x<<3) + t - '0', t = getchar();
    return x * f;
}

const int maxn = 15005;
const int oo = 0x3f3f3f3f;
int n, w[maxn], l[maxn], p[maxn];
priority_queue<pii> que;

void init()
{
    register int i;
    n = read();
    for(i = 1; i <= n; ++i)
        w[i] = read() + w[i-1], l[i] = read(), p[i] = read();
}
void work()
{
    register int i, x, cost = 0, flow = 0, ans = oo;
    for(i = n; i >= 1; --i)
    {
        while(!que.empty() && que.top().X > w[i-1])
            cost -= p[que.top().Y], que.pop();
        cost += p[i];
        que.push(mp(w[i] - l[i], i));
        if(cost < ans) ans = cost, x = i;
    }
    for(i = x; i <= n; ++i)
    {
        flow += w[i] - w[i-1];
        if(flow <= l[i]) printf("%d\n", i);
    }
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    init();
    work();

    #ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值