BZOJ5289:[HNOI2018]排列
2024-09-08 15:51:06
我对贪心的理解:https://www.cnblogs.com/AKMer/p/9776293.html
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=5289
首先我们转化题意:对于任意一个\(w_i\),只能在\(w_{a_i}\)被取之后才能被取走。存在某一取法\(p\)使得\(\sum\limits_{i=1}^{i=n}i*w_{p_i}\)取值最大,求最大值。
对于每一个\(i\)与\(a_i\)的约束,我们可以建一条边来描述。那么这题就跟Color a Tree一模一样的,不过求的是最大值罢了,那我们每次选平均值最小的与父亲合并就行。如果不能构成树就无解,构成树或者森林就有解。我们以\(0\)为虚根,那么就必然是一棵树了。
如果不存在某个\(a_i\)等于\(0\),或者存在\(a_i=i\)那么就肯定无解,否则必然有解。
然而知道这些你还是\(A\)不了这道题。若你问我而出此言,且听我细细道来:
\(1\)、平均值不开\(long\) \(double\)过不了;
\(2\)、平均值不开\(long\) \(double\)过不了;
\(3\)、平均值不开\(long\) \(double\)过不了;
重要的事情说三遍。
某出题人欺我老无力,
忍能偷偷卡精度。
公然卡我的double,
Bug连天改不出,
归来伏桌自叹息。
取最小值用堆维护一下,合并维护父亲信息用并查集就可以了。
时间复杂度:\(O(nlogn)\)
空间复杂度:\(O(n)\)
代码如下:
#include <queue>
#include <cstdio>
#include <algorithm>
using namespace std;
#define ll long long
#define ld long double
const int maxn=5e5+5;
int n,tot;
ll w[maxn],ans;
bool bo1,bo2,vis[maxn];
int a[maxn],fa[maxn],num[maxn],father[maxn];
int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
}
struct node {
int id;
ld ave;
bool operator<(const node &a)const {
return ave>a.ave;//初始是大根堆,所以要反过来
}
};
priority_queue<node> T;
int find(int x) {
if(fa[x]==x)return x;
return fa[x]=find(fa[x]);
}
void work() {
for(int i=1;i<=n;i++)
T.push((node){i,(ld)w[i]});
while(!T.empty()){
node N=T.top();int u=N.id;T.pop();
if(vis[u])continue;vis[u]=1;//由于每次都是将最小值合并,所以越合并平均值也就会越小,那么我们只需要知道这个node是否是已经合并过的node就行了。对于任意一个没有合并但是被多次插入堆中的node,最新的一次插入肯定是平均值最小的那一次,所以每次取最小值肯定就是最新更新的数据。
int FA=find(father[u]);
ans+=w[u]*num[FA];fa[u]=FA;
w[FA]+=w[u];num[FA]+=num[u];
if(FA)T.push((node){FA,(ld)w[FA]/num[FA]});//具体请见color a tree
}
}
int main() {
n=read();num[0]=1;
for(int i=1;i<=n;i++) {
a[i]=read();num[i]=1;
fa[i]=i;father[i]=a[i];//father记树上父亲节点,fa记录并查集祖先
if(!a[i])bo1=1;
if(a[i]==i)bo2=1;
}
if(!bo1||bo2) {puts("-1");return 0;}//判无解
for(int i=1;i<=n;i++)
w[i]=read();
work();printf("%lld\n",ans);
return 0;
}
最新文章
- Windows下安装MongoDB
- uva 147 Dollars
- DIV+CSS布局网站基本框架
- OGRE 1.9 的第一个程序(OGRE HelloWorld程序)
- shell编程之流程控制
- 35 网络相关函数(三)——live555源码阅读(四)网络
- RFID Hacking④:使用ProxMark3 破解门禁
- NFine - 全球领先的快速开发平台 Dapper Chloe
- 使用icon替换你的网页图标(转)
- 数码管字符产生器GenSym 1.0发布
- my-view-isnt-reflecting-changes-ive-made-to-the-underlying-tables
- Petshop学习第一天
- 基于表单的身份验证(FBA)
- int 价值型追记-time
- 【足迹C++primer】49、超载,变化,运营商
- docker入门(一)
- Python_02
- python使用多进程
- Docker Weave 应用部署
- xinetd网络