Codeforces 387E George and Cards
我们找到每个要被删的数字左边和右边第一个比它小的没被删的数字的位置。然后从小到大枚举要被删的数, 求答案。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long
using namespace std; const int N = 1e6 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-; int n, k, top, p[N], b[N];
int L[N], R[N];
bool ban[N];
PII stk[N];
LL ans;
vector<int> vc; struct Bit {
int a[N];
inline void modify(int x, int v) {
for(int i = x; i < N; i += i & -i)
a[i] += v;
}
inline int sum(int x) {
int ans = ;
for(int i = x; i; i -= i & -i)
ans += a[i];
return ans;
}
inline int query(int L, int R) {
if(L > R) return ;
return sum(R) - sum(L - );
}
} bit; bool cmp(const int& a, const int& b) {
return p[a] < p[b];
} int main() {
scanf("%d%d", &n, &k);
for(int i = ; i <= n; i++) bit.modify(i, );
for(int i = ; i <= n; i++) scanf("%d", &p[i]);
for(int i = ; i <= k; i++) scanf("%d", &b[i]), ban[b[i]] = true;
for(int i = ; i <= n; i++) {
if(ban[p[i]]) {
while(top && stk[top].fi > p[i]) top--;
stk[++top] = mk(p[i], i);
} else {
int pos = lower_bound(stk + , stk + top + , mk(p[i], )) - stk - ;
L[i] = pos ? stk[pos].se : ;
}
}
top = ;
for(int i = n; i >= ; i--) {
if(ban[p[i]]) {
while(top && stk[top].fi > p[i]) top--;
stk[++top] = mk(p[i], i);
} else {
int pos = lower_bound(stk + , stk + top + , mk(p[i], )) - stk - ;
R[i] = pos ? stk[pos].se : n + ;
}
}
for(int i = ; i <= n; i++)
if(!ban[p[i]]) vc.push_back(i);
sort(vc.begin(), vc.end(), cmp);
for(auto& x : vc) {
ans += bit.query(L[x] + , R[x] - );
bit.modify(x, -);
}
printf("%lld\n", ans);
return ;
} /*
*/
Codeforces 387E George and Cards的更多相关文章
- Codeforces Round #227 (Div. 2) E. George and Cards set内二分+树状数组
E. George and Cards George is a cat, so he loves playing very much. Vitaly put n cards in a row in ...
- Codeforces Round #227 (Div. 2) E. George and Cards 线段树+set
题目链接: 题目 E. George and Cards time limit per test:2 seconds memory limit per test:256 megabytes 问题描述 ...
- Codeforces 731 F. Video Cards(前缀和)
Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...
- Codeforces 467C George and Job(DP)
题目 Source http://codeforces.com/contest/467/problem/C Description The new ITone 6 has been released ...
- codeforces B. George and Round 解题报告
题目链接:http://codeforces.com/contest/387/problem/B 题目意思:给出1-n个问题,以及要满足是good rounde条件下这n个问题分别需要达到的compl ...
- codeforces 467C.George and Job 解题报告
题目链接:http://codeforces.com/problemset/problem/467/C 题目意思:给出一条含有 n 个数的序列,需要从中找出 k 对,每对长度为 m 的子序列,使得 找 ...
- Codeforces 467C. George and Job (dp)
题目链接:http://codeforces.com/contest/467/problem/C 求k个不重叠长m的连续子序列的最大和. dp[i][j]表示第i个数的位置个序列的最大和. 前缀和一下 ...
- George and Cards
Codeforces Round #227 (Div. 2) E:http://codeforces.com/contest/387/problem/E 题意:给你一个n个数的序列,然后给你一个标准序 ...
- cf E. George and Cards
http://codeforces.com/contest/387/problem/E 题意:给你n个数,然后在输入k个数,这k个数都在n个数中出现,进行每一次操作就是在n个数中选择长度为w的连续序列 ...
随机推荐
- 通过python操作GeoLite2-City.mmdb库将nginx日志访问IP转换为城市写入数据库
通过python操作GeoLite2-City.mmdb库将nginx日志写入数据库 # 创建存放nginx日志的表accesslog2 CREATE TABLE `accesslog2` ( `id ...
- 抢红包时用到的redis函数
2018-2-8 10:25:11 星期四 抢红包时经常会用redis(等其他nosql)的原子性函数去限流, 防止抢超, 下边列出一些主要的原子性函数 限制每个人只能抢一次 getSet(): 设置 ...
- 虚拟机静态ip设置
1.安装之后,用ifconfig命令发现没有ip信息,如下图,发现没有ip信息(好坑). 网上找了各种资料,最后才发现实自己的网卡没有启动,虽然使用service restart network,但是 ...
- python-函数入门(二)
一.函数对象 什么是函数? 函数是第一类对象,指的是函数名指向的值(函数)可以被当做数据去使用 1.函数的特性 1.函数可以被引用,即函数可以把值赋值给一个变量 def foo(): print('f ...
- Android录制音频的三种方式
对于录制音频,Android系统就都自带了一个小小的应用,可是使用起来可能不是特别的灵活.所以有提供了另外的俩种. 下边来介绍下这三种录制的方式; 1.通过Intent调用系统的录音器功能,然后在录制 ...
- 如果Android真的收费了,你怎么看?
前言 今天突然看到一群里有人发了下面这样一张图片,然后群里又炸了! 于是又和同事讨论了android收费的问题,然后隔壁正在玩农药的UI妹子就笑了... 没错! 安卓可能要收费了!安卓可能要收费了 ...
- 10分钟了解Android的Handler机制
Handler机制是Android中相当经典的异步消息机制,在Android发展的历史长河中扮演着很重要的角色,无论是我们直接面对的应用层还是FrameWork层,使用的场景还是相当的多.分析源码一探 ...
- ajax补充--------FormData等...
一.回顾上节知识点 1.什么是json字符串? 轻量级的数据交换格式 2.定时器:关于setTimeout setTimeout(foo,3000) # 3000表示3秒,foo表示一个函数,3秒后 ...
- kafka消息存储与partition副本原理
消息的存储原理: 消息的文件存储机制: 前面我们知道了一个 topic 的多个 partition 在物理磁盘上的保存路径,那么我们再来分析日志的存储方式.通过 ll /tmp/kafka-logs/ ...
- Oracle logminer 日志挖掘
Table of Contents 1. LOGMNR简介 2. 创建数据字典 2.1. 外部文件存储数据字典 2.2. redo log 存储数据字典 3. 添加需要分析的文件 4. 开始分析文件 ...