QOJ14444 跷跷板
Difficulty:Green。\ 应该是 Green,想到这个做法是没什么压力的,但是调了 114514 年于是还是启用了 GPT 发现我是 **。
题目。
我们假设右边比左边重,否则翻转一下就是右边比左边重了。\ 贪心地,因为可以移动任意实数,所以我们肯定贪心地移动能改变答案最大的那个,而显然最开始就是最大值。\ 但是有个问题就是不能跨过别人,那么某个很优的人可能会在某个地方被卡住,如果我们想继续移动这个人的话,那么就需要把卡住 TA 的那个人或者那一群人一起移动。\ 一起移动也就是相当于取平均值,这两群人实际上被我们合并了。\ 那么每次找最大的移动即可。
不会出现合并之后让本来排在前面的人单独走一段的情况,如果出现了,就证明 TA 比后面和 TA 合并的人更优,但是既然后面的人可以追上 TA 并与 TA 合并,也就是说后面的人更优,矛盾。
::::info[代码]
#include <bits/stdc++.h>
using namespace std;
#define de long double
#define N 100005
int n,T[N],c[N];
de now = 0.0,ans = 0.0,p[N],w[N];
int bcj[N];
int Find(const int x){
if(bcj[x] != x) bcj[x] = Find(bcj[x]);
return bcj[x];
}
constexpr de eps = 1e-8;
#define inf 1e18
struct ppp{
de val;
int id,Ti;
bool operator < (const ppp &o)const{
return val < o.val;
}
bool operator > (const ppp &o)const{
return val > o.val;
}
};
priority_queue<ppp>q;
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin >> n;
for(int i = 1;i <= n;++i){
cin >> p[i] >> w[i];
now += p[i] * (de)w[i];
}
if(-eps <= now && now <= eps){
cout << "0.000000\n";
return 0;
}
if(now < 0){
now = -now;
for(int i = 1;i <= n;++i) p[i] = -p[i];
for(int i = 1,j = n;i < j;++i,--j) swap(p[i],p[j]),swap(w[i],w[j]);
}
for(int i = 1;i <= n;++i) q.push((ppp){(de)w[i],i,0});
for(int i = 0;i <= n;++i) bcj[i] = i,c[i] = 1;
p[0] = -inf;
for(int ti = 1;;++ti){
const ppp qfr = q.top();
const de val = qfr.val;
const int id = qfr.id;
const int ct = c[id];
const int Ti = qfr.Ti;
q.pop();
if(Ti != T[id]) continue;
const int fr = Find(id-1);
const de dl = p[id] - p[fr];
if(dl * val * (de)ct >= now){ans += now / val,now = 0;break;}
now -= dl * val * (de)ct,T[fr] = ti,ans += dl * (de)ct,bcj[id] = fr;
const de nv = (val * (de)ct + (de)w[fr]) / (de)(ct+c[fr]);
w[fr] = nv * (de)(ct+c[fr]),c[fr] += ct,c[id] = 0;
q.push((ppp){nv,fr,ti});
}
cout << fixed << setprecision(6) << ans;
return 0;
}
::::