CF1151E Number of components 【构造】
一道神题
非常可惜,我想到一半放弃了,看了题解,看完题解后觉得这题索然无味,甚至有点不想写
真正有趣的是思考的过程
一个性质:联通块个数=点的个数-边的个数
那么这题就分开考虑点的贡献和边的贡献,然后减一下,无了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define gi get_int()
int get_int()
{
int x = 0, y = 1;
char ch = getchar();
while (!isdigit(ch) && ch != '-')
ch = getchar();
if (ch == '-')
y = -1, ch = getchar();
while (isdigit(ch))
x = x * 10 + ch - '0', ch = getchar();
return x * y;
}
int main()
{
int n = gi;
long long ans = 0;
int pre = -1;
for (int i = 0; i < n; i++) {
int num = gi;
ans += 1ll * num * (n - num + 1);
if (pre != -1)
ans -= 1ll * std::min(num, pre) * (n - std::max(num, pre) + 1);
pre = num;
}
std::cout << ans;
return 0;
}

浙公网安备 33010602011771号