Educational Codeforces Round 23D
给n个数求每个子区间的价值,区间的价值是最大值-最小值
套路题= =,分别算最大值和最小值的贡献,用并查集维护,把相邻点连一条边,然后sort,求最大是按边价值(两个点的最大价值)小的排,求最小是按最大排
类似的题:http://www.cnblogs.com/acjiumeng/p/8320666.html
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pil pair<int,ll>
#define pii pair<int,int>
#define ull unsigned long long
#define base 1000000000000000000
#define fio ios::sync_with_stdio(false);cin.tie(0) using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f; struct edge{
int u,v,ma,mi;
}e[N];
int father[N],sz[N],val[N];
int Find(int x)
{
return father[x]==x?x:father[x]=Find(father[x]);
}
bool cmp1(edge a,edge b)
{
return max(val[a.u],val[a.v])<max(val[b.u],val[b.v]);
}
bool cmp2(edge a,edge b)
{
return min(val[a.u],val[a.v])>min(val[b.u],val[b.v]);
}
int main()
{
int n,cnt=;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&val[i]);
if(i>)
{
e[cnt].u=i-,e[cnt].v=i;
cnt++;
}
father[i]=i,sz[i]=;
}
sort(e,e+cnt,cmp1);
// for(int i=0;i<cnt;i++)
// printf("%d %d\n",e[i].u,e[i].v);
ll ans=;
for(int i=;i<cnt;i++)
{
int x=Find(e[i].u),y=Find(e[i].v);
if(x!=y)
{
ans+=(ll)sz[x]*sz[y]*max(val[e[i].u],val[e[i].v]);
father[x]=y,sz[y]+=sz[x];
}
}
for(int i=;i<=n;i++)father[i]=i,sz[i]=;
sort(e,e+cnt,cmp2);
for(int i=;i<cnt;i++)
{
int x=Find(e[i].u),y=Find(e[i].v);
if(x!=y)
{
ans-=(ll)sz[x]*sz[y]*min(val[e[i].u],val[e[i].v]);
father[x]=y,sz[y]+=sz[x];
}
}
printf("%lld\n",ans);
return ;
}
/******************** ********************/
Educational Codeforces Round 23D的更多相关文章
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
- [Educational Codeforces Round 16]C. Magic Odd Square
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...
- [Educational Codeforces Round 16]B. Optimal Point on a Line
[Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...
- [Educational Codeforces Round 16]A. King Moves
[Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...
- Educational Codeforces Round 6 C. Pearls in a Row
Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...
- Educational Codeforces Round 9
Educational Codeforces Round 9 Longest Subsequence 题目描述:给出一个序列,从中抽出若干个数,使它们的公倍数小于等于\(m\),问最多能抽出多少个数, ...
- Educational Codeforces Round 37
Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...
- Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
随机推荐
- Spring 框架的核心功能之AOP技术
1. AOP 的概述 AOP, Aspect Oriented Programming, 面向切面编程; 通过预编译方式和运行期动态代理实现程序功能的统一维护的技术; AOP 采取横向抽取机制,取代了 ...
- 2015-03-12——简析DOM2级事件
DOM2级事件 事件的几种类型:对象事件,鼠标事件,键盘事件,表单事件,W3CDOM事件,以及针对浏览器的事件. 对象事件:window对象,也是javascript对象.load 适用于windo ...
- 【我的Android进阶之旅】如何去除ListView中Header View、Footer View中的分割线
最近的项目中给ListView 加入了一个Header View之后,发现Header View的下方也有了分割线,很难看,UI要求将Header View的分割器去掉,好吧.现在就来说一说如何如何去 ...
- Vue(6)- Vue-router进阶、单页面应用(SPA)带来的问题
一.Vue-router进阶 回顾学过的vue-router,并参考官方文档学习嵌套路由等路由相关知识. 二.单页面应用(SPA)带来的问题 1.虽然单页面应用有优点,但是,如果后端不做服务器渲染(h ...
- 利用Docker快速部署Oracle环境
工作中需要频繁使用Oracle环境,但是每次搭建起来比较消耗时间,本想通过虚拟机模板的方式来快速安装oracle vm,但是每次改ip等环境也很耗时,因此想到docker中有没有已经做好的images ...
- Android学习六---OpenCV for android samples
搭建好OpenCV4Android环境后,就要着手开发自己的应用了.对于一个没有OpenCV基础的人来开发OpenCV应用确实有点难度,不过我们可以从samples里学起,结合javadoc和万能的网 ...
- App doesn't auto-start an app when booting the device in Android
From Android 3.1, newly installed apps are always put into a "stopped" state and the only ...
- boost atomic
文档: http://www.boost.org/doc/libs/1_53_0/doc/html/atomic.html Presenting Boost.Atomic Boost.Atomic i ...
- 测试Windows live Writer
private String GetRandomint(int codeCount) { Random random = new Random(); string min = "" ...
- Nodejs学习计划
此文章已经发表于本人博客. 由于公司要求这段时间在学习Nodejs,基本靠自摸一路走来踩了很多坑浪费很多时间,今天就来这里说下,顺便计划一下接下来的学习计划,目前自己做个博客,项目过程中学习了js类以 ...