CF1151E Number of Components
CF1151E Number of Components
首先考虑连通块的个数怎么算
可以发现连通块个数=点数-边数
那么点边就可以分开算了
首先对每个点可能会出现的次数求和
再对每条边可能出现的次数求和
然后相减就可以得到答案了
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, a[100005], ans;
signed main() {
scanf("%lld", &n);
for(int i = 1; i <= n; i ++) scanf("%lld", &a[i]), ans += a[i] * (n - a[i] + 1);//计算点
for(int i = 1; i < n; i ++) {
ans -= min(a[i], a[i + 1]) * (n - max(a[i], a[i + 1]) + 1);//减去边
}
printf("%lld", ans);
return 0;
}
这题的难点主要是问题的转换吧
看来还是要把思路放开啊