CodeForces 1151E Number of Components

题目链接: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)。

代码如下:

 1 #pragma GCC optimize("Ofast")
 2 #include <bits/stdc++.h>
 3 using namespace std;
 4  
 5 #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0);
 6 #define Rep(i,n) for (int i = 0; i < (n); ++i)
 7 #define For(i,s,t) for (int i = (s); i <= (t); ++i)
 8 #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
 9 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
10 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
11 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
12 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
13  
14 #define pr(x) cout << #x << " = " << x << "  "
15 #define prln(x) cout << #x << " = " << x << endl
16  
17 #define LOWBIT(x) ((x)&(-x))
18  
19 #define ALL(x) x.begin(),x.end()
20 #define INS(x) inserter(x,x.begin())
21  
22 #define ms0(a) memset(a,0,sizeof(a))
23 #define msI(a) memset(a,inf,sizeof(a))
24 #define msM(a) memset(a,-1,sizeof(a))
25 
26 #define MP make_pair
27 #define PB push_back
28 #define ft first
29 #define sd second
30  
31 template<typename T1, typename T2>
32 istream &operator>>(istream &in, pair<T1, T2> &p) {
33     in >> p.first >> p.second;
34     return in;
35 }
36  
37 template<typename T>
38 istream &operator>>(istream &in, vector<T> &v) {
39     for (auto &x: v)
40         in >> x;
41     return in;
42 }
43  
44 template<typename T1, typename T2>
45 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
46     out << "[" << p.first << ", " << p.second << "]" << "\n";
47     return out;
48 }
49  
50 typedef long long LL;
51 typedef unsigned long long uLL;
52 typedef pair< double, double > PDD;
53 typedef pair< int, int > PII;
54 typedef set< int > SI;
55 typedef vector< int > VI;
56 typedef map< int, int > MII;
57 const double EPS = 1e-10;
58 const int inf = 1e9 + 9;
59 const LL mod = 1e9 + 7;
60 const int maxN = 1e5 + 7;
61 const LL ONE = 1;
62 const LL evenBits = 0xaaaaaaaaaaaaaaaa;
63 const LL oddBits = 0x5555555555555555;
64 
65 LL n, a[maxN];
66 LL ans;
67 
68 int main(){
69     INIT();
70     cin >> n;
71     For(i, 1, n) cin >> a[i];
72     
73     For(i, 1, n) {
74         if(a[i - 1] < a[i]) ans += (a[i] - a[i - 1]) * (n - a[i] + 1);
75         else if(a[i - 1] > a[i]) ans += a[i] * (a[i - 1] - a[i]);
76     }
77     
78     cout << ans << endl;
79     return 0;
80 }
View Code

 

posted @ 2019-04-28 21:56  梦樱羽  阅读(379)  评论(0编辑  收藏  举报
Live2D