U647429 奶牛排队 题解
zhangyoutao0815321 · · 题解
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<double> h(n);
vector<double> a(n);
for (int i = 0; i < n; ++i) cin >> h[i];
for (int i = 1; i < n; ++i) a[i] = (h[i] - h[0]) / i;
// for (int i = 1; i < n; ++i) cout << a[i] << " ";
// puts("");
map<double, int> mp;
vector<double> b = a;
vector<int> c(n);
sort(b.begin(), b.end());
int cnt = 1;
mp[b[0]] = cnt++;
for (int i = 1; i < n; ++i)
if (fabs(b[i] - b[i-1]) > 1e-6) {
mp[b[i]] = cnt++;
}
for (int i = 1; i < n; ++i)
c[i] = mp[a[i]];
// for (int i = 1; i < n; ++i)
// cout << c[i] << " ";
// puts("");
stack<int> st;
vector<int> next(n, n);
for (int i = 1; i < n; ++i) {
while (!st.empty() && c[i] >= c[st.top()]) {
next[st.top()] = i;
st.pop();
}
st.push(i);
}
// for (int i = 1; i < n; ++i) cout << next[i] << " ";
// puts("");
vector<int> f(n, n);
function<void(int)> dfs = [&](int i) -> void {
if (next[i] == n) {
f[i] = 0;
return;
}
dfs(next[i]);
f[i] = f[next[i]] + 1;
};
int ans = 0;
for (int i = 1; i < n; ++i) {
if (f[i] == n) dfs(i);
if (f[i] != n) ans += f[i];
}
cout << ans << endl;
return 0;
}