题目大意:
地铁站里有n个平台,每个平台都有一定的积水w、容量l、炸毁的费用p。如果一个平台被毁坏,其中所有的水都会流向下一个平台。如果一个的积水超过容量,这个平台将会自动冲毁。现在恐怖分子要毁掉最后一个平台,求最小的费用。
暴力:枚举从哪个平台开始炸。
SGU数据是不是变水了。。。。
n≤15000,O(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
个平台的积水之和为为
由于
S
是单调的,我们从后往前扫,用大根堆维护
用时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;
}