3.24 test summary

A gasoline supply

Questions surface: 51nod 1288
Solution: greedy + monotonous stack
Obviously each point can be added to the right of the first fuel is cheaper than a place to this point

#include<bits/stdc++.h>
using namespace std;
inline void read(int& x)
{
	x = 0; char c = getchar();
	while (!isdigit(c)) c = getchar();
	while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
}
#define maxn 100005
#define ll long long
int n, t, d[maxn], p[maxn], r[maxn];
ll s[maxn], ans;
stack<int> st;
int main()
{
	read(n), read(t);
	for (int i = 1; i <= n; ++i)
	{
		read(d[i]); read(p[i]);
		if (d[i] > t)
		{
			puts("-1");
			return 0;
		}
		s[i] = s[i - 1] + d[i];
	}
	s[n + 1] = s[n];
	for (int i = n; i; --i)
	{
		while (!st.empty() && p[st.top()] > p[i]) st.pop();
		r[i] = (st.empty()) ? n + 1 : st.top();
		st.push(i);
	}
	for (int las = 0, tp, i = 1; i <= n; ++i)//las是当前油量
	{
		tp = min(s[r[i] - 1] - s[i - 1], 1ll * t);//需要油量,和邮箱容量取个min.注意r[i]-1.
		if (tp > las)
		{
			ans += 1ll * (tp - las) * p[i];
			las = tp;
		}
		las -= d[i];
	}
	printf("%lld\n", ans);
	return 0;
}

B Site

Questions surface: 51nod 2558
Solution: Shortest greedy +
code implementation capabilities or too weak ah
well Tucao about this solution to a problem, not \ (LaTex \) is also a large section really is blind see


Beautify the original explanations bit
first with \ (\ text {floyd} \ ) or \ (\ text {dijskra} \ ) obtaining the full source shortest
enumerate each side \ ((u, v, w
) \) for all points (said solution to a problem in other points, but such examples are not over), according to the \ (U \) in descending order from the
enumeration \ (I \) , represents a path: \ (A_ { i} \ rightarrow u \ rightarrow v
\ rightarrow \ max (a_ {1} \ sim a_ {i-1}) \) where the \ (\ max \) represents \ (V \) to the longest distance of those points
that under case, the optimal solution will inevitably get in the path of the midpoint of the
back, then realized that \ (\ max \) do not always go to scan, save directly down just fine (I actually did not think I would like to do this complexity right) ah
complexity: \ (O (nm \ log n-) \)


Correctness proof:
Q: Why for \ (a_ {i} \ sim a_ {n} \) as long as we consider the \ (u \) to these points?
A: If the elapsed \ (V \) has a shorter path, the path from point to point, is not necessarily the midpoint \ (a_ {i} \) path length
if the first elapsed from point \ (U \) then \ (A_. 1} {\ A_ SIM-I. 1} {\) , these paths must be compared to the midpoint \ (a_ {i} \) long, i.e. has previously been considered a
due answer must be a strip the midpoint of the path, so we will certainly take into account this path, so this algorithm is correct

#include<bits/stdc++.h>
using namespace std;
template<typename T>
inline void read(T& x)
{
	x = 0; char c = getchar();
	while (!isdigit(c)) c = getchar();
	while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
}
#define maxn 205
#define maxm 40005
int n, m, tp[maxn], a[maxm], b[maxm], w[maxm];
double dis[maxn][maxn], ans = 1e18, tmp;
inline void floyd()
{
	for (int k = 1; k <= n; ++k)
		for (int i = 1; i <= n; ++i)
			for (int j = 1; j <= n; ++j)
				if (dis[i][k] + dis[k][j] < dis[i][j])
					dis[i][j] = dis[i][k] + dis[k][j];
}

int main()
{
	read(n), read(m);
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= n; ++j) dis[i][j] = 1e18;
	for (int i = 1; i <= n; ++i) dis[i][i] = 0;
	for (int i = 1; i <= m; ++i)
		read(a[i]), read(b[i]), read(w[i]), dis[a[i]][b[i]] = dis[b[i]][a[i]] = w[i];
	floyd();
	for (int i = 1, cnt; i <= m; ++i)
	{
		cnt = 0; tmp = 0;
		for (int j = 1; j <= n; ++j) tp[++cnt] = j;
		sort(tp + 1, tp + cnt + 1, [&](int x, int y) {return dis[a[i]][x] > dis[a[i]][y]; });
		for (int j = 1; j <= cnt; ++j)
		{
			if (tmp >= dis[a[i]][tp[j]] - w[i] && tmp <= dis[a[i]][tp[j]] + w[i]) 
				ans = min(ans, (dis[a[i]][tp[j]] + w[i] + tmp) / 2);//答案是这条路径长度的一半
			tmp = max(tmp, dis[tp[j]][b[i]]);
		}
	}
	printf("%lf\n", ans);
	return 0;
}

C dyed plaid

Meaning of the questions: 51nod 2564
Solution: network flow
Gugu Gu

Guess you like

Origin www.cnblogs.com/123789456ye/p/12576158.html