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的连续序列 ...
随机推荐
- virtual box 安装centos min
2018-4-19 22:20:40 星期四 之前不小心把用了很久的centos镜像删掉了.....这里记录下安装最小版centos的步骤 1. 安装centos 2. 开启网络, 并设置为随机启动 ...
- Eclipse中三种设置编码格式的方法
转自:https://blog.csdn.net/rainy_black_dog/article/details/52403735 很早以前听过一位老师说过:咱们中国人不管学习哪种编程语言,总会遇到乱 ...
- 【原创】大叔问题定位分享(31)hive metastore报错
hive metastore在建表时报错 [pool-5-thread-2]: MetaException(message:Got exception: java.net.ConnectExcepti ...
- 常见的SQL调优(SQL Tuning)Tips
建立适当的索引(参考<正确建立数据库索引的姿势>) 用UNION替换OR (适用于索引列) 用exist.not exist代替 in.not in 不要以字符格式声明数字(会 ...
- [swoole]swoole常见问题总汇
1.在daemon模式下Task异步任务写入文件需要采用绝对路径: 1.Task异步任务中操作数据库,如果仅仅只是在启动程序之初进行一次数据库链接,链接会在一定的时间后自动断开,应对这样的情况的最好办 ...
- php array_map与array_walk使用对比
array_map(): 1.array_map() 函数将用户自定义函数作用到数组中的每个值上,并返回用户自定义函数作用后的带有新值的数组,若函数作用后无返回值,则对应的新值数组中为空. 2.回调函 ...
- 重启报错:Failed to open /dev/initctl: No such device or address
[root@WEB-APP-REP-MASTER ~]# rebootError getting authority: Error initializing authority: Error call ...
- mybatis 按in 函数参数顺序排序
使用 FIELD()函数 SELECT * FROM user WHERE id IN (72, 80, 69) ORDER BY FIELD(id, 72, 80, 69)
- java 中int与integer的区别
int与integer的区别从大的方面来说就是基本数据类型与其包装类的区别: int 是基本类型,直接存数值,而integer是对象,用一个引用指向这个对象 1.Java 中的数据类型分为基本数据类型 ...
- easyui combogrid下拉表格的分页/按键/动态搜索
作者:xfl4629712 < easyui combogrid下拉表格的分页/按键/动态搜索 > 需求: 1.下拉框下拉时出现表格: 2.表格带分页功能: 3.可以使用向上键.向下 ...