codeforces 722C Destroying Array 并查集带权
来源:互联网 发布:网络受限制是什么意思 编辑:程序博客网 时间:2024/06/05 01:59
题意:含有n个元素的数组,每次破坏一个数字,每次计算连续的序列元素之和,取出最大值。直到全部破坏为止。
思路:逆向思维,往数组里添数字,每次添加看左右位置是否添加,有连续的就合并,将位置看作要合并的元素,起始时每个位置权值就等于a数组中对应位置的值。
#include<cstdio>#include<cstring>#include<algorithm>#define ll long long#define Max(a,b) (a)>(b)?(a):(b)const int maxn = 1e5+10;using namespace std;ll f[maxn],w[maxn];//带权并查集 ll a[maxn],p[maxn];bool vis[maxn];ll ans[maxn];int Find(int x){if(f[x]==x)return x;else return f[x]=Find(f[x]);}void init(int n){for(int i=1;i<=n;i++)f[i]=i;memset(vis,0,sizeof(vis));}int main(){int n;//freopen("in.txt","r",stdin);while(~scanf("%d",&n)){init(n);for(int i=1;i<=n;i++)scanf("%I64d",&a[i]);for(int i=1;i<=n;i++)scanf("%I64d",&p[i]);ans[n]=0;for(int i=n;i>1;i--)//从最后一个位置开始 {int pos=p[i];w[pos]=a[pos];if(vis[pos-1]){int x=Find(pos);int y=Find(pos-1);f[x]=y; //将y设为x的父亲结点 w[y]+=w[x];//累加儿子结点权值 }if(vis[pos+1]){int x=Find(pos);int y=Find(pos+1);f[x]=y;w[y]+=w[x];}vis[pos]=1;ans[i-1]=Max(ans[i],w[Find(pos)]);}for(int i=1;i<=n;i++){printf("%I64d\n",ans[i]);}}}
阅读全文
0 0
- codeforces 722C Destroying Array 并查集带权
- codeforces 722C Destroying Array
- codeforces(722c)-Destroying Array
- Codeforces 722C. Destroying Array
- codeforces 722C. Destroying Array
- codeforces 722C Destroying Array ( 并查集 / Multiset )
- codeforces 722C. Destroying Array(并查集||set)
- 【Codeforces 722 C Destroying Array】+ 并查集
- Codeforces 722C Destroying Array(并查集)
- 【37.38%】【codeforces 722C】Destroying Array
- codeforces 722C Destroying Array 【线段树好题】
- codeforces 722C. Destroying Array map+multiset
- Codeforces 722C - Destroying Array By Assassin
- CodeForces - 722C Destroying Array (set)
- Codeforces #722C: Destroying Array 题解
- [codeforces]C. Destroying Array——并查集
- 并查集巧妙用法(codeforces)C. Destroying Array
- codeforces-772C-Destroying Array
- 数据库引擎简介
- 万年历
- python3 [爬虫入门实战]爬虫之scrapy爬取织梦者网站并存mongoDB
- JAVA面向对象6:多态
- C++总结5——继承与多态
- codeforces 722C Destroying Array 并查集带权
- 补码
- Processing【5】终章不存在
- 线程安全与锁优化
- JDBC(2)—Statement
- 十四天
- 【Angular2】页面表格制作
- shiro Filter--拦截器
- CropImageView android上的一个图片裁剪控件