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的连续序列 ...
随机推荐
- web@HTML常用标签分类,标签嵌套规则
一.html标签又叫做html元素,它分为块级元素和内联元素(也可以叫做行内元素),都是html规范中的概念.1.块级元素块级元素是指本身属性为display:block;的元素.因为它自身的特点,我 ...
- matlab常用命令
clc; %清屏 clear; %清除变量 close all; %关闭 doc %查看文档 meshgrid%采样mesh %网格曲面surf %光滑曲面plot %ezplotdiff figur ...
- Android apk互调
1.启动另外一个应用程序的主Activity. //这些代码是启动另外的一个应用程序的主Activity,当然也可以启动任意一个Activity ComponentName componetName ...
- <转载>ford-fulkerson算法
原文链接http://blog.csdn.net/ivan_zgj/article/details/51580993 最大流问题常常出现在物流配送中,可以规约为以下的图问题.最大流问题中,图中两个顶点 ...
- CSS rem长度单位
1. 概述 1.1 说明 rem是css3中新增的一个单位属性(font size of the root element),根据页面的根节点(html)的字体大小进行转换的单位,通过此单位属性可以进 ...
- Jquyer轮播带数字和提示文字
效果图如下: 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http: ...
- Sql 08数据库还原数据库时一直提示数据库被占用
直接试试这个sql语句吧 ALTER DATABASE [datebase] SET OFFLINE WITH ROLLBACK IMMEDIATE ALTER database [datebase] ...
- Confluence 6 安装补丁类文件
Atlassian 支持或者 Atlassian 缺陷修复小组可能针对有一些关键问题会提供补丁来解决这些问题,但是这些问题还没有放到下一个更新版本中.这些问题将会使用 Class 类文件同时在官方 J ...
- flask中的wtforms使用
一.简单介绍flask中的wtforms WTForms是一个支持多个web框架的form组件,主要用于对用户请求数据进行验证. 安装: pip3 install wtforms 二.简单使用wtfo ...
- Python操作MySQL案例
最近都在学习Python代码,希望学会Python后,能给我带来更高的工作效率,所以每天坚持学习和拷代码,下面是一个Python操作MySQL的一个实例,该实例可以让更多的人更好了解MySQLdb模块 ...