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. Wijmo 5 与Breeze 的组合,及与METRONIC 的集成

    1.Wijmo 5  是支持ANGULARJS 的HTML5 控件   http://wijmo.gcpowertools.com.cn/ 官方试用版  C1Wijmo-Eval_5.20151.42 ...

  2. poj 1182

    http://poj.org/problem?id=1182 一个利用并查集的经典题目. 思路:在网上看到别人的思路,觉得方法还是挺不错的. 首先,开辟一个3*n的数组belg,用来存b和c的关系,在 ...

  3. 新建samba配置步骤

    Linux系统默认已经安装了Samba,但是没有安装Samba服务: 1,先查看安装情况:rpm -qa|grep samba 根据系统的安装情况选择下载或者通过光驱安装所缺的rpm包. 我的安装情况 ...

  4. Linux安装字体

    用惯了Win7的字体,感觉雅黑看着很舒服,就动手在Linux安装下,简单描述下: 第一步:百度一下,找到微软雅黑字体(.ttf)下载 第二步:把下载的字体放到cd /usr/share/fonts/z ...

  5. UESTC 250

    windy数 基本的数位DP,需要判断当前位是否为起始位. #include <cstdio> #include <cmath> #include <cstring> ...

  6. java web 学习 --第三天(Java三级考试)

    第二天的学习内容这里:http://www.cnblogs.com/tobecrazy/p/3446646.html Jsp中的动作标签 <jsp:include> 实现动态包含,在一个文 ...

  7. 4.kvm克隆虚拟机

    virt-clone 作用简介 virt-clone 主要是用来克隆kvm虚拟机,并且通过 Options.General Option.Storage Configuration.Networkin ...

  8. sql语句按照汉字拼音首字母排序

    oracle : 在oracle9i中新增了按照拼音.部首.笔画排序功能.设置NLS_SORT值SCHINESE_RADICAL_M 按照部首(第一顺序).笔划(第二顺序)排序SCHINESE_STR ...

  9. PDO(数据访问抽象层)

    自带事务功能,多条sql同时执行时,如果其中一条执行失败,那么所有的都执行失败.开启了事务,可以进行回滚操作,让程序变得更安全. 1.访问不同的数据库2.自带事务功能3.防止SQL注入:分两次发送 / ...

  10. Qt 扫描进程列表以及获取进程信息

    使用方法: QMap<QString,qint64> app_pid; getAllAppPidList( app_pid ); #include <tlhelp32.h>// ...