【CF1151E】Number of Components
【CF1151E】Number of Components
题面
题解
联通块个数=点数-边数。
然后把边全部挂在较小的权值上。
考虑从小往大枚举左端点,等价于每次删掉一个元素,那么删去点数,加上边数,修改一下当前值就行了。
这个东西对于任意形态的树都可以做。
#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
#define ll long long
#define MAX 100100
inline int read()
{
int x=0;bool t=false;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=true,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return t?-x:x;
}
int n,a[MAX],b[MAX];ll ans,now;
vector<int> E[MAX];
int main()
{
n=read();
for(int i=1;i<=n;++i)a[i]=read(),b[a[i]]+=1;
for(int i=1,s=0;i<=n;++i)now+=(s+=b[i]);
for(int i=1;i<n;++i)
{
int u=min(a[i],a[i+1]),v=max(a[i],a[i+1]);
E[u].push_back(v);now-=n-v+1;
}
ans+=now;
for(int i=1;i<=n;++i)
{
now-=1ll*(n-i+1)*b[i];
for(int v:E[i])now+=n-v+1;
ans+=now;
}
printf("%lld\n",ans);
return 0;
}
【CF1151E】Number of Components的更多相关文章
- 【BZOJ3275】Number 最小割
[BZOJ3275]Number Description 有N个正整数,需要从中选出一些数,使这些数的和最大.若两个数a,b同时满足以下条件,则a,b不能同时被选1:存在正整数C,使a*a+b*b=c ...
- 【05】Number图解
[05]Number图解
- 【leetcode】Number of 1 Bits
题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...
- 【leetcode】Number of Islands(middle)
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- 【KMP】Number Sequence
KMP算法 KMP的基处题目,数字数组的KMP算法应用. 主要是next[]数组的构造,next[]存储的是字符的当前字串,与子串前字符匹配的字符数. 移动位数 = 已匹配的字符数 - 对应的部分匹配 ...
- 【Oracle】number类型保留小数位
SQL> SELECT TO_CHAR(, '9990.00') A, TO_CHAR(5.8, '9990.00') B, TO_CHAR(., '9990.00') C FROM dual; ...
- 【js】Number与数组
定义和用法 Number() 函数把对象的值转换为数字. 语法 Number(object):参数必须是对象 如果参数是 Date 对象,Number() 返回从 1970 年 1 月 1 日至今的毫 ...
- 【LeetCode】Number of Islands
Number of Islands 问题描写叙述 Given a 2d grid map of '1's (land) and '0's (water), count the number of is ...
- 【poj1019】 Number Sequence
http://poj.org/problem?id=1019 (题目链接) 题意 给出一个数:1 12 123 1234 12345 123456 1234567 12345678 123456789 ...
随机推荐
- Redis环境搭建和代码测试及与GIS结合的GEO数据类型预研
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 1.1传统MySQL+ Memcached架构遇到的问题 My ...
- PJProject(2.6) 工程介绍
pjlib pjlib\build\pjlib.vcproj pjlib_test pjlib\build\pjlib_test.vcproj pjsip_core pjsip\build\pjsip ...
- 网络中的NAT模式
一.概述 NAT英文全称是"Network Address Translation",中文意思是"网络地址转换",它是一个IETF(Internet Engin ...
- 我的ElasticSearch之ElasticSearch安装配置环境
最近一段时间比较忙,都很少来园子逛了,刚好,用到了ElasticSearch,感觉还不错,所以就给大家推荐一下,自己也顺便学习:虽然公司选择用ElasticSearch,但是以前都没有用过这个,而且公 ...
- #022 Python 实验课
拍7游戏 描述 “拍7游戏”规则是:一堆人围成一圈,开始时,任意指定一人说出数字“1”后,一圈人按顺时针方向,每人按整数由小到大的顺序一人一个地报出后续数字“2”.“3”......,当遇到为“7”的 ...
- day8-基础函数的学习(三)
开始今日份总结 今日目录 1.生成器 2.列表推导式 3.匿名函数 4.装饰器 开始今日份总结 1.生成器 1.1 生成器的定义 定义:生成器本质就是迭代器,生成器是自己用python代码写的迭代器 ...
- day16-面向对象基础(三)
今日摘要 今天主要整理一下这俩天学习的内容,面向对象也快学完了,深刻的认识到面向对象就是一个思想,怎么把思想理解了,其他也就不是什么事了 1.类的约束 2.类的类方法与静态方法 3.类的反射 4.类的 ...
- 授权普通非DBA用户可以有权限查看执行计划的方法
drop table PLAN_TABLE; 删除原plan表 执行ORACLE自带的创建脚本 @?/rdbms/admin/utlxplan.sql 创建同义词 create or re ...
- How to move lobsegment and lobindex to a different Tablespace
Hi, Assuming that I have table "TEST" in USERS TableSpace CREATE TABLE TEST ( TEST_ID NUMB ...
- Python 隔离环境 virtualenv
1) 安装 $ sudo pip3 install virtualenv 2) 创建并进入工程目录,例如 myproject $ mkdir myproject $ cd myproject 3) 在 ...