社区讨论
Always WA#3,不同方式排序不同输出
P2742【模板】二维凸包 / [USACO5.1] 圈奶牛Fencing the Cows参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @mhj3p0cn
- 此快照首次捕获于
- 2025/11/03 20:12 4 个月前
- 此快照最后确认于
- 2025/11/03 20:12 4 个月前
rt.参照第一篇题解最后一种做法
换了好几种排序方式,测#3的数据都是不同错法。。
CPP# include <bits/stdc++.h>
//#define int long long
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
int n, lst;
double lst1[100005], lst2[100005], sum, l = 1e6;
pair <double, double> a[100005];
stack <int> st;
bool cmp (const pair <double, double>& a, const pair <double, double>& b) {
return a.second != b.second ? a.second < b.second : a.second <= l ? a.first < b.first: a.first > b.first;
}
double dis (const double x, const double y) {
return sqrt (x * x + y * y);
}
int main () {
ios::sync_with_stdio (0);
cin.tie (0);
cout.tie (0);
cin >> n;
for (int i = 1; i <= n; ++ i)
cin >> a[i].first >> a[i].second, l = min (l, a[i].second);
sort (a + 1, a + 1 + n);
// sort (a + 1, a + 1 + n, cmp);
lst1[1] = lst2[1] = 0, st.push (1);
for (int i = 2; i <= n; ++ i) {
// (a[i].first - a[st.top ()].first) / (a[i].second - a[st.top ()].second) <= lst1 / lst2
// cerr << i << ':' << a[i].first << ',' << a[i].second << ':' << st.size () << '\n';
// cerr << (a[i].first - a[st.top ()].first) * lst2 << ',' << lst1 << '*' << (a[i].second - a[st.top ()].second) << '\n';
// cerr << (a[i].first - a[1].first) * lst2 << ',' << lst1 << '*' << (a[i].second - a[1].second) << '\n';
while ((a[i].first - a[st.top ()].first) * lst2[st.top ()] >= lst1[st.top ()] * (a[i].second - a[st.top ()].second))
st.pop ();
// cerr << i << ':' << a[i].first << ',' << a[i].second << ':' << st.size () << ':' << st.top () << '\n';
lst1[i] = a[i].first - a[st.top ()].first, lst2[i] = (a[i].second - a[st.top ()].second);
st.push (i);
}
lst = st.top (), st.pop ();
while (! st.empty ()) //cerr << st.top () << "->" << lst << ':' << a[st.top ()].first << ',' << a[st.top ()].second << "->" << a[lst].first << ',' << a[lst].second << '\n',
sum += dis (a[lst].first - a[st.top ()].first, a[lst].second - a[st.top ()].second), lst = st.top (), st.pop ();
// cerr << sum << '\n';
/*--------------------------------------------*/
lst1[n] = lst2[n] = 0, st.push (n);
for (int i = n - 1; i; -- i) {
//(a[i].first - a[st.top ()].first) / (a[i].second - a[st.top ()].second) <= lst1 / lst2
while ((a[i].first - a[st.top ()].first) * lst2[st.top ()] >= lst1[st.top ()] * (a[i].second - a[st.top ()].second))
st.pop ();
lst1[i] = a[i].first - a[st.top ()].first, lst2[i] = (a[i].second - a[st.top ()].second);
st.push (i);
}
lst = st.top (), st.pop ();
while (! st.empty ())
sum += dis (a[lst].first - a[st.top ()].first, a[lst].second - a[st.top ()].second), lst = st.top (), st.pop ();
cout << fixed << setprecision (2) << sum;
return 0;
}
回复
共 0 条回复,欢迎继续交流。
正在加载回复...