正文
增加 35 行删除 125 行
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131```cpp// P1073 [NOIP 2009 提高组] 最优贸易#include <iostream>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <cstring>const int MAXN = 1e5 + 5;// GLOBALint n, m, ans;int val[MAXN];std::vector<int> e[MAXN], e2[MAXN], e3[MAXN];// TARJANint sc, timer;int dfn[MAXN], low[MAXN];int scc[MAXN], max[MAXN], min[MAXN];bool inS[MAXN];std::stack<int> stk;// TOPOSORTint inD[MAXN], dp[MAXN];bool vis[MAXN];void Tarjan(int u){dfn[u] = low[u] = ++timer;stk.push(u), inS[u] = 1;for (int v : e[u]){if (!dfn[v]){Tarjan(v);low[u] = std::min(low[u], low[v]);}else if (inS[v])low[u] = std::min(low[u], dfn[v]);}if (dfn[u] == low[u]){int v;sc++;do{v = stk.top();stk.pop(), inS[v] = 0;scc[v] = sc;max[sc] = std::max(max[sc], val[v]);min[sc] = std::min(min[sc], val[v]);} while (v != u);}}void RebuildGraph(){for (int u = 1; u <= n; u++)for (int v : e[u])if (scc[u] != scc[v] && scc[u] && scc[v]) // 注意原图没跑完{e2[scc[u]].push_back(scc[v]); // 建边建的是SCC!e3[scc[v]].push_back(scc[u]);}for (int i = 1; i <= n; i++){std::sort(e2[i].begin(), e2[i].end());e2[i].erase(std::unique(e2[i].begin(), e2[i].end()), e2[i].end());for (int v : e2[i])inD[v]++; // 去重后再统计!}}void DFS(int x) // 从n能到达的点{vis[x] = 1;for (int i : e3[x]) // 反图if (!vis[i])DFS(i);}void Toposort(){std::queue<int> q;q.push(scc[1]); // 注意从一号节点开始DFS(scc[n]); // 原图不一定联通,前面用Tarjan从1到i,现在从n到idp[scc[1]] = min[scc[1]];while (!q.empty()){int u = q.front();q.pop();for (int v : e2[u]){dp[v] = std::min(dp[v], std::min(min[v], dp[u]));if (--inD[v] == 0)q.push(v);}}for (int i = 1; i <= sc; i++)if (vis[i])ans = std::max(ans, max[i] - dp[i]);}int main(){// READstd::cin >> n >> m;for (int i = 1; i <= n; i++)std::cin >> val[i];for (int i = 1; i <= m; i++){int u, v, op;std::cin >> u >> v >> op;if (op == 1)e[u].push_back(v);else{e[u].push_back(v);e[v].push_back(u);}}// MAINmemset(min, 0x3f, sizeof(min));memset(max, -1, sizeof(max));memset(dp, 0x3f, sizeof(dp));Tarjan(1); // 只考虑到达1的点RebuildGraph();Toposort();// OUTPUTstd::cout << ans;return 0;}```1234567891011121314151617181920212223242526272829303132333435363738394041$$(1) |x - 5| + |2x - 13| (x > 5)$$由于 $ x > 5 $,那么 $ |x - 5| = x - 5$。由于 $2x = 10$, 所以 $2x - 13$ 不确定它的符号,故分类讨论:$(1)$ $x \ge \frac{13}{2}$ 时,$ |2x - 13| = 2x - 13$,此时$$\begin{equation*}\begin{split}原式 &= x - 5 + 2x - 13\\&= 3x - 18\end{split}\end{equation*}$$$(2)$ $x \le \frac{13}{2}$ 时,$ |2x - 13| = 13 - 2x$,此时$$\begin{equation*}\begin{split}原式 &= x - 5 + 13 - 2x\\&= -x + 8\end{split}\end{equation*}$$这即是两种情况,综上:$$原式=\begin{cases}3x - 18 & (x \ge \frac{13}{2}) \\-x + 8 & (x \le \frac{13}{2})\end{cases}$$