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;
}
發佈了44 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章