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

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 number bi 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 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
Input
10 5
1 2 3 4 5 6 7 8 9 10
2 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 1000006
int 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]);
		}
		//保留的数的位置存入set
		else{
			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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值