codeforces Educational Codeforces Round 65 (补完)
C News Distribution
并查集水题
D Bicolored RBS
括号匹配问题,如果给出的括号序列nesting depth为n,那么最终可以分成两个nesting depth为n / 2的序列。
在进行匹配的时候,如果当前栈中的左括号大于等于 n / 2,那么剩下的括号就要进行标记,最终标记和不标记的分成两个部分。
E Range Deleting
对一个序列去掉一个区间范围内的数字,使得剩下的序列是一个非降序的序列。
这题是一道较好的思维题。
首先可以预处理出1到\(pref\),只保留\([1,pref]\)内的数字,序列是非降序的;也可以预处理出\(suf\)到\(x\),保留\([suf,x]\)内的数字,序列是非降序的。
然后可以发现,如果去掉\([l,r]\)满足条件,那么去掉\([l,r+1],[l,r+2],...,[l,x]\)也一定是满足条件的。
可以枚举\(l\),枚举的范围是\([1,pref+1]\),\(pref+1\)是因为\([1,pref]\)的序列都是有序的,所以去掉\(pref+1\)也是有序的;对于每一个\(l\),要找出满足条件的\(r\)的数量;
因为去掉了\(l\)到某个数字,前面剩下的数字就是\([1,l-1]\),那么\(r\)满足的条件就是首先要大于等于\(suf\),大于等于\(l\),然后还要满足在\([1,l-1]\)当中出现的最大的数字的最后一个下标之前,这个数字没有出现过,换个角度,假设\([1,l-1]\)当中出现的最大的数字的最后一个下标是\(ind\),那么\(r\)的最小值就是\(max(a[1],....,a[ind])+1\),于是\(r\)的最小值就由之前的3个条件共同求出,满足条件的\(r\)的个数就分为两种情况:
1.\(l == r\),那么就有\(x - r + 1\)种;
2.\(l != r\),那么就有\(x - r + 2\)种,因为\(r\)是可以保留在序列当中的,所以可以去掉的范围是\([r-1,x]\),也就是说去掉\([l,r-1]\)这个区间也是满足条件的。
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 1e6 + 10;
vector<int> G[N];
set<int> s;
int pre[N],sub[N];
int a[N];
bool L[N],R[N];
int main()
{
int n,x;
scanf("%d%d",&n,&x);
for (int i = 1;i <= n;i++)
{
scanf("%d",&a[i]);
s.insert(a[i]);
}
if (x == 1)
{
puts("1");
return 0;
}
pre[0] = -inf;
for (int i = 1;i <= n;i++)
{
pre[i] = max(pre[i-1],a[i]);
}
sub[n+1] = inf;
for (int i = n;i >= 1;i--)
{
sub[i] = min(sub[i+1],a[i]);
}
for (int i = 1;i <= n;i++)
{
G[a[i]].push_back(i);
}
int pref,suf;
L[1] = 1;
for (int i = 2;i <= x;i++)
{
if (G[i].empty())
{
L[i] = L[i-1];
}
else
{
int p = G[i][0];
int x = sub[p];
if (x < i)
{
L[i] = 0;
}
else
{
L[i] = L[i-1];
}
}
}
R[x] = 1;
for (int i = x - 1;i >= 1;i--)
{
if (G[i].empty())
{
R[i] = R[i+1];
}
else
{
int sz = G[i].size();
int p = G[i][sz-1];
int x = pre[p];
if (x > i)
{
R[i] = 0;
}
else
{
R[i] = R[i+1];
}
}
}
for (int i = 1;i <= x;i++)
{
if (L[i]) pref = i;
}
for (int i = x;i >= 1;i--)
{
if (R[i]) suf = i;
}
ll ans = 0;
for (int i = 1;i <= x;i++)
{
if (i == 1)
{
for (int j = 1;j <= x;j++)
{
if (R[j+1])
{
ans += x - j + 1;
//printf("%d *\n",x - j + 1);
break;
}
}
}
else
{
if (!L[i-1]) break;
int l = i-1;
if (l < (*s.begin()) || l > (*--s.end()))
{
int tmp = max(l + 1,suf);
if (tmp == l + 1)
{
ans += x - tmp + 1;
}
else
{
ans += x - tmp + 2;
//printf("%d *\n",x - tmp + 2);
}
}
else
{
if (G[l].empty())
{
int xx = *--s.lower_bound(l);
int sz = G[xx].size();
int p = G[xx][sz-1];
int tmp = max(pre[p] + 1,suf);
tmp = max(l + 1,tmp);
if (tmp == l + 1)
{
ans += x - tmp + 1;
}
else
{
ans += x - tmp + 2;
}
}
else
{
int sz = G[l].size();
int p = G[l][sz-1];
int tmp = max(pre[p] + 1,suf);
if (tmp == l + 1)
{
ans += x - tmp + 1;
}
else
{
ans += x - tmp + 2;
}
}
}
}
}
printf("%lld\n",ans);
return 0;
}
F Scalar Queries
题意:
有一个数组\(a\),里面的数字两两不同,\(f(l,r)\)表示选出下标从\(l\)到\(r\)的数字,然后排序,排序之后的数组为\(b\),\(\sum_{i = 1}^{r - l + 1}b_i * i\)。
需要求每一个\(f(l,r)\)的和。
思路:
又是一道很好的思维题。
可以转化为求每一个数字对最终答案的贡献。
假设\(low(l,r,a[i])\)表示在区间\([l,r]\)内小于\(a[i]\)的数字,那么\(a[i]\)对于\((l,r)\)的贡献就是\(a[i] * low(l,r,a[i])+1\)。
\(low(l,r,a[i])+1\)就相当于\(a[i]\)在\((l,r)\)内的rank。
这个rank又转化为每一个小于\(a[i]\)的数字出现的次数之和。
首先对于\(a_i\)本身,它自己出现的次数是\(i * (n - i - 1)\);
然后对于\(a_j < a_i,j < i\)的数字,它的出现次数是\(j * (n - i + 1)\);
对于\(a_j < a_i,j > i\)的数字,它的出现次数是\(i * (n - j + 1)\);
如上三个数字相加,假设为\(sum\),那么\(sum * a_i\)就是\(a_i\)对答案的贡献。
对于小于某个数字的所有数字出现的位置,可以用树状数组求前缀和。大的也同理。
又出现了\(int * int\) 爆\(int\) 的问题!!!
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N = 5e5 + 10;
const ll mod = 1000000000LL + 7;
ll c[N],d[N];
int n;
int lowbit(int x)
{
return x&(-x);
}
void addl(int x,int y)
{
for (int i = x;i <= n;i += lowbit(i)) c[i] += y;
}
void addr(int x,int y)
{
for (int i = x;i <= n;i += lowbit(i)) d[i] += y;
}
ll getlsum(int x)
{
ll ans = 0;
for (int i = x;i >= 1;i -= lowbit(i))
{
ans += c[i];
ans %= mod;
}
return ans;
}
ll getrsum(int x)
{
ll ans = 0;
for (int i = x;i >= 1;i -= lowbit(i))
{
ans += d[i];
ans %= mod;
}
return ans;
}
pii a[N];
int main()
{
scanf("%d",&n);
for (int i = 1;i <= n;i++)
{
scanf("%d",&a[i].first);
a[i].second = i;
}
sort(a+1,a+1+n);
ll ans = 0;
for (int i = 1;i <= n;i++)
{
ll x = getlsum(a[i].second);
ll tmp = 0;
tmp += x * (n-a[i].second+1);
tmp %= mod;
ll y = getrsum(n-a[i].second+1);
tmp += y * a[i].second;
tmp %= mod;
tmp += 1LL * a[i].second * (n-a[i].second + 1);
tmp %= mod;
ans += tmp * a[i].first;
ans %= mod;
addl(a[i].second,a[i].second);
addr(n-a[i].second + 1,n-a[i].second+1);
}
ans += mod;
printf("%lld\n",ans % mod);
return 0;
}
codeforces Educational Codeforces Round 65 (补完)的更多相关文章
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...
- codeforces Educational Codeforces Round 5 A. Comparing Two Long Integers
题目链接:http://codeforces.com/problemset/problem/616/A 题目意思:顾名思义,就是比较两个长度不超过 1e6 的字符串的大小 模拟即可.提供两个版本,数组 ...
- Codeforces Educational Codeforces Round 3 A. USB Flash Drives 水题
A. USB Flash Drives 题目连接: http://www.codeforces.com/contest/609/problem/A Description Sean is trying ...
- codeforces Educational Codeforces Round 16-E(DP)
题目链接:http://codeforces.com/contest/710/problem/E 题意:开始文本为空,可以选择话费时间x输入或删除一个字符,也可以选择复制并粘贴一串字符(即长度变为两倍 ...
- Codeforces Educational Codeforces Round 15 E - Analysis of Pathes in Functional Graph
E. Analysis of Pathes in Functional Graph time limit per test 2 seconds memory limit per test 512 me ...
- Codeforces Educational Codeforces Round 15 D. Road to Post Office
D. Road to Post Office time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces Educational Codeforces Round 15 C. Cellular Network
C. Cellular Network time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Educational Codeforces Round 5 E. Sum of Remainders 数学
E. Sum of Remainders 题目连接: http://www.codeforces.com/contest/616/problem/E Description The only line ...
随机推荐
- PHP非对称加密
加密的类型: 在日常设计及开发中,为确保数据传输和数据存储的安全,可通过特定的算法,将数据明文加密成复杂的密文.目前主流加密手段大致可分为单向加密和双向加密. 单向加密:通过对数据进行摘要计算生成密文 ...
- Newlifex修仙(一) 超级配置文件
新生命团队基础框架X组件,包括日志.数据库.网络.RPC.序列化.缓存.Windows服务.多线程等模块,支持.Net Framework/.netstandard/Mono. 说道配置文件,大家觉得 ...
- PJzhang:kali linux安装virtualbox虚拟机和chrome浏览器
猫宁!!! 参考链接: https://www.cnblogs.com/zhishuai/p/8007410.html kali linux 安装virtualbox. 查询系统的版本 apt-cac ...
- 【miscellaneous】单播、广播和多播IP地址
转自:http://www.cnblogs.com/gaoxing/archive/2012/02/19/2358484.html 除地址类别外,还可根据传输的消息特征将IP地址分为单播.广播或多播. ...
- vue点击编辑按钮,内容变成input可以修改,也可以删除
一.效果 图1 图2,点击报错之后,又变成图1的效果 二.使用到了element UI中的以下组件: <el-button> <el-input> 三.使用的关键点是vue中的 ...
- 菜鸟系列k8s——快速部署k8s集群
快速部署k8s集群 1. 安装Rancher Rancher是业界唯一完全开源的企业级容器管理平台,为企业用户提供在生产环境中落地使用容器所需的一切功能与组件. Rancher2.0基于Kuberne ...
- Autoit安装及启动
1.Autoit下载: 官网下载地址:https://www.autoitscript.com/site/autoit/downloads/ 提供百度网盘下载:https://pan.baidu.co ...
- Centos7 安装部署 Airflow
本人在centos7 的环境下部署,怎么在centos7 下配置静态 IP 关闭防火墙 以及安装jdk在这里不多赘述, centos7 配置静态ip可以参考:https://www.cnblogs.c ...
- [转帖]国内拉取google kubernetes镜像
国内拉取google kubernetes镜像 2019年04月19日 01:19:03 willblog 阅读数 4231 标签: kubernetes 更多 个人分类: kubernetes ...
- 【Python】【demo实验16】【练习实例】【打印所有水仙花数】
题目: 打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例如:153是一个"水仙花数",因为153= ...