E. George and Cards
 

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 ≤ ww 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!

Input

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.

Output

Print a single integer — the maximum number of pieces of sausage that George can get if he acts optimally well.

Examples
input
3 2
2 1 3
1 3
output
1
 
题意:
  给你n个数只包含1~n
  和一个k,以及k个数
  让你从这n个中删除n-k个数, 余留给定的k个数
  每次删除一个数,你可以选择连续的w个数,表示删除这个w个数中最小的数,同时获得w分数
  问你如何删除 使得最后获得的分数最多,输出分数来
题解:

  也就是尽量重复使用那些必须删除的数

  那么 从小到大删除就好了

  如何计算答案?

  从小到大枚举,

    对于必须保留的数,将其位置插入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内二分+树状数组的更多相关文章

  1. 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 问题描述 ...

  2. Codeforces Round #365 (Div. 2) D - Mishka and Interesting sum(离线树状数组)

    http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的 ...

  3. 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预处理出  ...

  4. 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 ...

  5. 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 ...

  6. Codeforces Round #590 (Div. 3)【D题:维护26棵树状数组【好题】】

    A题 题意:给你 n 个数 , 你需要改变这些数使得这 n 个数的值相等 , 并且要求改变后所有数的和需大于等于原来的所有数字的和 , 然后输出满足题意且改变后最小的数值. AC代码: #includ ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 用extern定义全局变量

    1.extern的作用 extern有两个作用,第一个,当它与"C"一起连用时,如: extern "C" void fun(int a, int b); 则告 ...

  2. zookeeper部署及集群测试

    zookeeper部署及集群测试 环境 三台测试机 操作系统: centos7 ; hostname: c1 ; ip: 192.168.1.80 操作系统: centos7 ; hostname: ...

  3. ACM/ICPC 之 BFS(离线)+康拓展开 (HDU1430-魔板)

    魔板问题,一道经典的康拓展开+BFS问题,为了实现方便,我用string类来表示字符串,此前很少用string类(因为不够高效,而且相对来说我对char数组的相关函数比较熟),所以在这里也发现了很多容 ...

  4. EF没有同步更新(转)

    不知道这算不算一个bug,当你新建一个从数据库生成的edmx时,他能正确的生成所有的tt文件,但是当你从数据库更新表结构时,他不能正确的更新tt文件,以建立Model1.edmx为例,在解决方案中展开 ...

  5. JAVA回调接口的理解

    A类持有B接口的对象引用,B接口有一个callBack()方法,C类是B类的实现类,实现了callBack()方法,把C类传入A类,当A类执行完操作后调用callBack()方法,这时候A调用的就是C ...

  6. 【网络】VPN和代理服务器的区别

    来自:http://www.zhihujingxuan.com/19311.html [scotttony的回答(41票)]: VPN和ssh哪个比较好, 要看你怎么定义是“好”. ssh作为一个创建 ...

  7. LeetCode 171 Excel Sheet Column Number

    Problem: Given a column title as appear in an Excel sheet, return its corresponding column number. F ...

  8. cocos2d-x 第一篇 环境搭建

    官网:http://www.cocos2d-x.org/ 下载一个稳定版的cocos2d-x (网址:http://download.cocos2d-x.org/ Github Repository ...

  9. 模拟赛1029d1

    第二题[题目描述]给你两个日期,问这两个日期差了多少毫秒.[输入格式]两行,每行一个日期,日期格式保证为"YYYY-MM-DD hh:mm:ss"这种形式.第二个日期时间一定比第一 ...

  10. Mysql之mysqlbinlog使用

    mysqlbinlog用于BinLog的显示,备份和重做. 默认情况下,mysqlbinlog是以base-64编码的方式呈现的.如: mysqlbinlog  master_bin.000006   ...