Codeforces Round #227 (Div. 2) E. George and Cards set内二分+树状数组
George is a cat, so he loves playing very much.
Vitaly put n cards in a row in front of George. Each card has one integer written on it. All cards had distinct numbers written on them. Let's number the cards from the left to the right with integers from 1 to n. Then the i-th card from the left contains number pi(1 ≤ pi ≤ n).
Vitaly wants the row to have exactly k cards left. He also wants the i-th card from left to have number bi written on it. Vitaly gave a task to George, to get the required sequence of cards using the remove operation n - k times.
In one remove operation George can choose w (1 ≤ w; w is not greater than the current number of cards in the row) contiguous cards (contiguous subsegment of cards). Let's denote the numbers written on these card as x1, x2, ..., xw (from the left to the right). After that, George can remove the card xi, such that xi ≤ xj for each j (1 ≤ j ≤ w). After the described operation George gets w pieces of sausage.
George wondered: what maximum number of pieces of sausage will he get in total if he reaches his goal and acts optimally well? Help George, find an answer to his question!
The first line contains integers n and k (1 ≤ k ≤ n ≤ 106) — the initial and the final number of cards.
The second line contains n distinct space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the initial row of cards.
The third line contains k space-separated integers b1, b2, ..., bk — the row of cards that you need to get. It is guaranteed that it's possible to obtain the given row by using the remove operation for n - k times.
Print a single integer — the maximum number of pieces of sausage that George can get if he acts optimally well.
3 2
2 1 3
1 3
1
也就是尽量重复使用那些必须删除的数
那么 从小到大删除就好了
如何计算答案?
从小到大枚举,
对于必须保留的数,将其位置插入set
对于必须删除的数,查找当前数i的位置在set中的前驱后继,表示答案的区间,
这个区间中可能有些数十被删除了的(比i小)那么 用一个树状数组或者线段树计算区间和即可。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18;
const double Pi = acos(-1.0);
const int N = 1e6+, M = 1e6, mod = 1e9+, inf = 2e9; int n,k,a[N],pos[N],x,vis[N];
LL ans = ;
int C[N];
void update(int x,int c) {
for(int i = x; i < N; i += i&(-i)) C[i] += c;
}
int ask(int x) {
int s = ;
for(int i = x; i; i -= i&(-i)) s+=C[i];
return s;
}
int main() {
scanf("%d%d",&n,&k);
for(int i = ; i <= n; ++i) scanf("%d",&a[i]),pos[a[i]] = i;
for(int i = ; i <= n; ++i) update(i,);
for(int i = ; i <= k; ++i) scanf("%d",&x),vis[x] = ;
set<int > s;
s.insert(),s.insert(n+);
for(int i = ; i <= n; ++i) {
if(!vis[i]) {
int bef = *(--s.lower_bound(pos[i]));
int blc = *(s.lower_bound(pos[i]));
ans += ask(blc-) - ask(bef);
update(pos[i],-);
} else {
s.insert(pos[i]);
}
}
cout<<ans<<endl;
return ;
}
Codeforces Round #227 (Div. 2) E. George and Cards set内二分+树状数组的更多相关文章
- 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 Round #365 (Div. 2) D - Mishka and Interesting sum(离线树状数组)
http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的 ...
- Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem (树状数组求逆序数 变形)
题目链接 题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a[j]),求i和j的种类数. 我们可以用map预处理出 ...
- Codeforces Round #263 (Div. 1) C. Appleman and a Sheet of Paper 树状数组暴力更新
C. Appleman and a Sheet of Paper Appleman has a very big sheet of paper. This sheet has a form of ...
- Codeforces Round #381 (Div. 2) D. Alyona and a tree dfs序+树状数组
D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Round #590 (Div. 3)【D题:维护26棵树状数组【好题】】
A题 题意:给你 n 个数 , 你需要改变这些数使得这 n 个数的值相等 , 并且要求改变后所有数的和需大于等于原来的所有数字的和 , 然后输出满足题意且改变后最小的数值. AC代码: #includ ...
- Codeforces Round #348 (VK Cup 2016 Round 2, Div. 2 Edition) E. Little Artem and Time Machine 树状数组
E. Little Artem and Time Machine 题目连接: http://www.codeforces.com/contest/669/problem/E Description L ...
- 01背包 Codeforces Round #267 (Div. 2) C. George and Job
题目传送门 /* 题意:选择k个m长的区间,使得总和最大 01背包:dp[i][j] 表示在i的位置选或不选[i-m+1, i]这个区间,当它是第j个区间. 01背包思想,状态转移方程:dp[i][j ...
- Codeforces Round #267 (Div. 2) C. George and Job(DP)补题
Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently ...
随机推荐
- (Win7 x64)NetBeans 8.0.2 使用Tomcat 8作为服务器
1.下载Apache Tomcat,解压至本地硬盘的根目录. 2.运行CMD,输入: 解压盘符:\apache-tomcat-8.0.xx\bin\service.bat install 3.安装完成 ...
- web开发,关于jsp的常见问题,重复提交,防止后退。
看了网上的,有几种方法:1 在你的表单页里HEAD区加入这段代码: <META HTTP-EQUIV="pragma" CONTENT="no-cache" ...
- Android状态栏微技巧,带你真正意义上的沉浸式
记得之前有朋友在留言里让我写一篇关于沉浸式状态栏的文章,正巧我确实有这个打算,那么本篇就给大家带来一次沉浸式状态栏的微技巧讲解. 其实说到沉浸式状态栏这个名字我也是感到很无奈,真不知道这种叫法是谁先发 ...
- 【mysql】利用Navicat for MySQL的使用
1. 查看sql语句 如果忘记了某个SQL语句怎么写,可以利用Navicat for MySQL的历史日志来查看 在Navicat for MySQL中,直接对数据库进行想要的操作,然后点击工具-&g ...
- addsubview跟insertsubview的区别
子视图是以栈的方式存放的. 每次addsubview时都是在最后面添加. 每次在addsubview前和addsubview后可以看看[self.view.subViews count]: 你看看你的 ...
- php图片防盗链的小测试
test.php <?php $txt = "http://hiphotos.baidu.com/stupidet/pic/item/4f1b8cfb4c33b7254e4aea69. ...
- VS VA助手补丁覆盖目录
以VS2010为例. VA_X.dll 复制到以下文件夹内,覆盖原文件. WinXP系统: %USERPROFILE%\Local Settings\Application Data\Microsof ...
- Xcode - 修改变量名、类名及字符串的替换操作
在做iOS开发代码优化的工作时,优化代码结构之前,我们应该先整理好工程的外貌,将文件和类的命名进行规范,在Xcode中为我们提供了方便而强大的名称修改功能. 第一步:修改类名 将鼠标点击放在类的名称上 ...
- Xcode开发中的6个小技巧
Xcode是iPhone和iPad开发者用来编码或者开发iOS app的IDE.Xcode有很多小巧但很有用的功能,很多时候我们可能没有注意到它们,也或者我们没有在合适的水平使用这些功能简化我们的iO ...
- Struts2拦截器之DefaultWorkflowInterceptor
一.DefaultWorkflowInterceptor是什么 首先说这东西是干嘛来的,在action中可以对传进来的数据进行验证,方法是实现Validateable接口的validate():voi ...