题目链接:http://codeforces.com/problemset/problem/1151/E

题目大意:

  n个人排成一个序列,标号为 1~n,第 i 个人的学习成绩为 ai,现在要选出学习成绩在区间 [l, r] 中的人,被选出的人如果他们在序列中相邻,就将他们划分到一个小组,设 f(l, r) 表示一共可以分出的组的个数。求这个和:$ \sum_{l = 1}^n \sum_{r = 1}^n f(l, r) $。

分析:

  对于每一个学生,都有作为它所在小组区间左端点和右端点的时候,每个点的贡献就是它作为左端点或右端点的次数,但我们并不需要都考虑,只需要单独考虑左端点(右端点),这是因为这个点作为右端点的贡献恰好就是其他点作为左端点的贡献,把每个同学作为左端点的贡献累加起来就是答案。
  对于 ai ,它是否能成为左端点是由 ai-1 的值所决定的:
当 ai-1 == ai 时,没有贡献,因为 ai 肯定不能成为左端点。
当 ai-1 > ai 时,区间端点必须满足 1 <= l <= ai ,ai <= r < ai-1 。贡献为 ai * (ai-1 - ai)。
当 ai-1 < ai 时,区间端点必须满足 ai-1 < l <= ai ,ai <= r <= n 。贡献为 (n - ai + 1) * (ai - ai-1)。

代码如下:

 #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std; #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef pair< int, int > PII;
typedef set< int > SI;
typedef vector< int > VI;
typedef map< int, int > MII;
const double EPS = 1e-;
const int inf = 1e9 + ;
const LL mod = 1e9 + ;
const int maxN = 1e5 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555; LL n, a[maxN];
LL ans; int main(){
INIT();
cin >> n;
For(i, , n) cin >> a[i]; For(i, , n) {
if(a[i - ] < a[i]) ans += (a[i] - a[i - ]) * (n - a[i] + );
else if(a[i - ] > a[i]) ans += a[i] * (a[i - ] - a[i]);
} cout << ans << endl;
return ;
}

CodeForces 1151E Number of Components的更多相关文章

  1. Codeforces 1270H - Number of Components(线段树)

    Codeforces 题目传送门 & 洛谷题目传送门 首先需发现一个性质,那就是每一个连通块所对应的是一个区间.换句话说 \(\forall l<r\),若 \(l,r\) 在同一连通块 ...

  2. Codefores 1151E Number of Components

    大意:给定n元素序列$a$, $1\le a_i \le n$, 定义函数$f(l,r)$表示范围在$[l,r]$以内的数构成的连通块个数, 求$\sum\limits_{i=1}^{n}\sum\l ...

  3. 【CF1151E】Number of Components

    [CF1151E]Number of Components 题面 CF 题解 联通块个数=点数-边数. 然后把边全部挂在较小的权值上. 考虑从小往大枚举左端点,等价于每次删掉一个元素,那么删去点数,加 ...

  4. codeforces Hill Number 数位dp

    http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits:  5000 MS   Memory Limits: ...

  5. Codeforces 920 E Connected Components?

    Discription You are given an undirected graph consisting of n vertices and  edges. Instead of giving ...

  6. codeforces 27E Number With The Given Amount Of Divisors

    E. Number With The Given Amount Of Divisors time limit per test 2 seconds memory limit per test 256 ...

  7. Codeforces 27E. Number With The Given Amount Of Divisors (暴力)

    题目链接:http://codeforces.com/problemset/problem/27/E 暴力 //#pragma comment(linker, "/STACK:1024000 ...

  8. Codeforces 235E Number Challenge

    http://codeforces.com/contest/235/problem/E 远距离orz......rng_58 证明可以见这里(可能要FQ才能看到) 还是copy一下证明吧: 记 $$f ...

  9. Codeforces 251C Number Transformation

    Number Transformation 我们能发现这个东西是以2 - k的lcm作为一个循环节, 然后bfs就好啦. #include<bits/stdc++.h> #define L ...

随机推荐

  1. SpringBoot入门教程(十一)过滤器和拦截器

    在做web开发的时候,过滤器(Filter)和拦截器(Interceptor)很常见,通俗的讲,过滤器可以简单理解为“取你所想取”,忽视掉那些你不想要的东西:拦截器可以简单理解为“拒你所想拒”,关心你 ...

  2. Angular CLI 安装和使用

    1.背景介绍 关于Angular版本,Angular官方已经统一命名Angular 1.x同一为Angular JS:Angular 2.x及以上统称Angular: CLI是Command Line ...

  3. 【带着canvas去流浪(6)】绘制雷达图

    目录 一. 任务说明 二. 重点提示 三. 示例代码 示例代码托管在:http://www.github.com/dashnowords/blogs 博客园地址:<大史住在大前端>原创博文 ...

  4. C++项目中采用CLR的方式调用C#编写的dll

    1.注意事项:在编写C#DLL类库时,最好不要出现相同的命名空间,否则在C++中调用可能会出现编译错误.2.将C#的源码生成的“dll”文件复制到C++项目中的Debug目录下3.将C++项目属性设置 ...

  5. C# 构造tree菜单工具方法

    如何构造tree数据结构,做个笔记,方便查阅,本方法是直接返回json字符串: private string ToMenuJson(List<Model> data, string par ...

  6. nginx系列11:负载均衡哈希算法ip_hash与hash模块

    使用默认的round-robin负载均衡算法无法保证某一类请求只能由上游的某一台应用服务器处理,它只适用于AKF扩展中的水平扩展,如果要保证某一类请求只能由上游的某一台应用服务器处理,就需要用到AKF ...

  7. Web前端新学

    本人大学时学的是网络工程,那时候只是大概学了一点HTML和CSS.毕业后没有找IT方面的工作,所以对专业知识忘得差不多了.然由于生活现状,终是决心重新好好学习IT,刚入学的一周学习了C#语言的一些知识 ...

  8. 微信小程序 canvas 绘制圆形状

    page({ // 绘制canvas drawCanvas:function(){ const ctx = wx.createCanvasContext('poster') // 画圆形二维码 thi ...

  9. 微信小程序 选择微信自带的地址 用户授权选择了拒绝

    // 选择微信自带地址 addAddr:function () { wx.chooseAddress({ success: function (res) { self.setData({ addrIn ...

  10. 使用WordPress制作微信小程序

    0 产品由来 微信小程序具有即来即用.轻量化.与微信贴合性好的特点.对于独立产品来说,使用微信小程序能够较好的服务与个人及现在的互联网社群,提升用户体验. 本次设计的微信小程序是面向无人机开发者社区的 ...