平均數
- 貪心 - APCSC
- https://atcoder.jp/contests/abc236/tasks/abc236_e
平均最大化¶
區間平均¶
區間平均
給定
思路
將
問題就變成取
最大平均區間¶
CF EDU A. Maximum Average Segment
給定
思路
二分搜平均值
也就是看
code
最短平均路¶
最短平均路 CF EDU B. Minimum Average Path
給定一個
思路
跟上面最大平均區間一樣,我們去二分搜平均值
code
#include <bits/stdc++.h>
#define int long long
#define pii pair<int, int>
#define pb push_back
#define mk make_pair
#define F first
#define S second
#define ALL(x) x.begin(), x.end()
using namespace std;
const double INF = 2e18;
const int maxn = 3e5 + 5;
const int M = 1e9 + 7;
int n, m;
double dp[maxn];
int par[maxn];
vector<pii> G[maxn];
bool check(double x) {
fill(dp, dp + n + 1, INF);
fill(par, par + n + 1, -1);
dp[1] = 0;
for (int i = 1; i <= n; i++) {
for (auto [v, w] : G[i]) {
double val = (double)dp[i] + w - x;
if (dp[v] > val) {
dp[v] = val;
par[v] = i;
}
}
}
return dp[n] <= 0;
}
signed main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v, w;
cin >> u >> v >> w;
G[u].pb({v, w});
}
double l = 0, r = 105;
for (int i = 0; i < 100; i++) {
double mid = (l + r) / 2;
if (check(mid)) r = mid;
else l = mid;
}
check(l);
stack<int> stk;
int x = n;
stk.push(x);
while (par[x] != -1) {
x = par[x];
stk.push(x);
}
cout << stk.size() - 1 << '\n';
while (stk.size()) {
cout << stk.top() << ' ';
stk.pop();
}
}