题目链接: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. spring-security实现的token授权

    在我的用户密码授权文章里介绍了spring-security的工作过程,不了解的同学,可以先看看用户密码授权这篇文章,在 用户密码授权模式里,主要是通过一个登陆页进行授权,然后把授权对象写到sessi ...

  2. .netcore 模块积累

    最全的 demo https://github.com/XiaoFaye/netcore-samples http://files.cnblogs.com/files/kellynic/practic ...

  3. 今天筹备了一件大事:重学JS

    最近在阮大神的博客上看到一篇文章,讲的是关于如何自学计算机技术,原文出自 Teach Yourself Computer Science.看完以后我明白自己的缺陷在哪里,基础不够牢固是我最大的问题. ...

  4. 并发系列(4)之 AbstractQueuedSynchronizer 源码分析

    本文将主要讲述 AbstractQueuedSynchronizer 的内部结构和实现逻辑,在看本文之前最好先了解一下 CLH 队列锁,AbstractQueuedSynchronizer 就是根据 ...

  5. C#工具:MySQL忘记密码解决方法

    1.进入管理员控制台停止mysql服务:net stop mysql; 2.进入mysql的安装路径,如我的安装路径为C:\Program Files\MySQL\MySQL Server 5.5,打 ...

  6. 前端知识复习: JS选中变色

    前端知识复习:JS选中变色 上篇文章 :前端知识复习:Html DIV 图文混排(文字放在图片下边) Js选中图片效果 <!DOCTYPE html> <html xmlns=&qu ...

  7. Spring Cloud中Feign如何统一设置验证token

    代码地址:https://github.com/hbbliyong/springcloud.git 原理是通过每个微服务请求之前都从认证服务获取认证之后的token,然后将token放入到请求头中带过 ...

  8. vs2017和vs2019专业版和企业版

    步骤:打开vs2017,依次点击--->帮助----->注册产品 专业版: Professional: KBJFW-NXHK6-W4WJM-CRMQB-G3CDH 企业版: Enterpr ...

  9. Java笔记(day9~day10)

    继承: 好处:1.提高代码复用性:   2.让类之间产生关系,给多态提供了前提: 父类.子类 Java中支持单继承,不直接支持多继承,但对C++的多继承进行了改良 单继承:一个子类只能有一个直接复类 ...

  10. Java并发——线程介绍

    前言: 互联网时代已经发展到了现在.从以前只考虑小流量到现在不得不去考虑高并发的问题.扯到了高并发的问题就要扯到线程的问题.你是否问过自己,你真正了解线程吗?还是你只知道一些其他博客里写的使用方法.下 ...