Codeforces 387E George and Cards(二分+树状数组)

来源:互联网 发布:ubuntu 无法复制 编辑:程序博客网 时间:2024/06/03 07:57

Codeforces 387E George and Cards

George and Cards

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 numberpi (1 ≤ pi ≤ n).

Vitaly wants the row to have exactlyk cards left. He also wants thei-th card from left to have numberbi written on it. Vitaly gave a task to George, to get the required sequence of cards using the remove operationn - 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 asx1, x2, ..., xw (from the left to the right). After that, George can remove the cardxi, such thatxi ≤ xj for eachj (1 ≤ j ≤ w). After the described operation George getsw 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 integersn andk (1 ≤ k ≤ n ≤ 106) — the initial and the final number of cards.

The second line containsn distinct space-separated integersp1, p2, ..., pn (1 ≤ pi ≤ n) — the initial row of cards.

The third line containsk space-separated integersb1, 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 forn - 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 22 1 31 3
Output
1
Input
10 51 2 3 4 5 6 7 8 9 102 4 6 8 10
Output
30


题意:

给出一个含有n个数的序列,每次删除操作可以选择一段尽量长的连续的子序列,然后删除这段子序列中的最小值,并得到这段子序列长度的奖励,若干次删除操作后得到题目要求的一个含有k个数的序列。

要求输出最大的奖励值。

分析:

删除操作从最小的值开始。如果从最大的值开始删,删除小的值的时候可能会减小序列的最大长度。首先先记录每个值所在的位置,并标记应保留的数,然后按数值从小到大找,找到应保留的数就存入set,找到要删的数就二分查找set中能得到的最大的长度,减掉这个区间内已经删掉的数,然后加到sum中。删掉的数用树状数组存。

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <set>using namespace std;#define N 1000006int n,k,pos[N],m[N],ans[N],x,l,r;//pos存位置,m存保留的数字,ans统计已经删去的数 long long sum=0;int add(int p){int s=0;while(p>0){s+=ans[p];p-=(p&(-p)); }return s;}int main(){scanf("%d%d",&n,&k);memset(m,0,sizeof(m));memset(ans,0,sizeof(ans)); for(int i=1;i<=n;i++){scanf("%d",&x);pos[x]=i;}for(int i=0;i<k;i++){scanf("%d",&x);m[x]=1;}set<int>d;set<int>::iterator t;for(int i=1;i<=n;i++){if(m[i]==1){d.insert(pos[i]);}//保留的数的位置存入setelse{t=d.lower_bound(pos[i]);if(t==d.begin()){if(d.size()==0){l=1;r=n;}else{l=1;r=(*t)-1;}}else if(t==d.end()){l=(*(--t))+1;r=n;}else{r=(*t)-1;l=(*(--t))+1;}sum+=r-l+1;sum-=add(r)-add(l-1);x=pos[i];while(x<=n){ans[x]++;x+=(x&(-x));}//printf("i %d l %d r %d sum %d\n",i,l,r,sum);}}printf("%I64d\n",sum);return 0;}


0 0
原创粉丝点击