codeforces 722C (并查集)
来源:互联网 发布:华云数据集团有限公司 编辑:程序博客网 时间:2024/04/30 17:26
题目链接:http://codeforces.com/contest/722/problem/C
题意:每次破坏一个数,求每次操作后的最大连续子串和。
思路:并查集逆向操作
#include<bits/stdc++.h>using namespace std;typedef long long ll;const int N = 1e5 + 10;ll sum[N], ans[N];int n, a[N], b[N], father[N], r[N];bool vis[N];int finds(int x){ if(father[x] != x) father[x] = finds(father[x]); return father[x];}void connect(int a,int b){ a = finds(a); b = finds(b); if(r[a] > r[b]) father[b] = a; else if(r[a] < r[b]) father[a] = b; else { father[a] = b; r[b]++; } sum[a] = sum[b] = sum[a] + sum[b];}void init(){ for(int i = 1; i <= n + 1; i++) father[i] = i; vis[0] = vis[n+1] = 1;}int main(){ scanf("%d",&n); for(int i = 1; i <= n; i++) scanf("%d",a+i); for(int i = 1; i <= n; i++) scanf("%d",b+i); init(); for(int i = n; i > 1; i--) { sum[b[i]] = a[b[i]]; if(vis[b[i]-1]) connect(b[i] - 1,b[i]); if(vis[b[i]+1]) connect(b[i] + 1,b[i]); ans[i-1] = max(ans[i],sum[finds(b[i])]); vis[b[i]] = 1; } for(int i = 1; i <= n; i++) printf("%I64d\n",ans[i]); return 0;}
0 0
- codeforces 722C (并查集)
- codeforces 722C. Destroying Array(并查集||set)
- codeforces 722C Destroying Array ( 并查集 / Multiset )
- CodeForces 722C DestroyingArray 并查集+思维题
- 【Codeforces 722 C Destroying Array】+ 并查集
- Codeforces 722C Destroying Array(并查集)
- codeforces 278C. Learning Languages(并查集)
- Codeforces 698C. Fix a Tree (并查集)
- Codeforces 455C Civilization(并查集+树形dp)
- CodeForces 455C Civilization(并查集+树直径)
- 并查集巧妙用法(codeforces)C. Destroying Array
- Codeforces 731C Socks(并查集)
- CodeForces 731C - Socks(并查集)
- CodeForces 731C-Socks(联通图 并查集)
- CodeForces 731 C.Socks(贪心+并查集)
- CodeForces 616C The Labyrinth (二维并查集)
- CodeForces 218C 并查集
- 【CodeForces】445C Civilization 并查集
- C++ Primer Plus(再谈重载-一个矢量类)
- 范式说明和数据库脏读、幻读、不可重复读
- 关于ext用到了自己定义的插件时间(精确到秒)一些注意的地方
- HihoCoder #1369 : 网络流一·Ford-Fulkerson算法
- GLM Intro - Matrix Transform Example
- codeforces 722C (并查集)
- android_07_表格布局
- 安卓混淆
- ROS导航-----slam_gmapping使用Log数据创建地图
- HDU 5514 Frogs(容斥原理 gcd)
- hdu2688 Rotate(树状数组)
- Java的自学之路-return与break的差别
- iOS10 Xcode8问题汇总
- python 3 mysql操作