Codeforces 387E George and Cards【思维+RMQ+二分+树状数组】被卡常= =

142 篇文章 0 订阅
28 篇文章 1 订阅

E. 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 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
input
10 5
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10
output
30

题目大意:


给出一个长度为N的序列,每一次我们操作可以选择一段长度为Len的区间,然后将其中最小的那个数删除掉,并且获得Len的价值,我们希望最终数组变成长度为M的那个序列,问我们过程中进行操作能够获得的最多的价值为多少。

保证每个数都只会出现1次。


思路:


①我们肯定是从最小的数开始删起是最优的,因为每一次我们选择一个最小的数进行删除的时候,能够拓展出来的这个长度为Len的区间长度会最大,所以我们考虑先从最小的需要被删除的数字开始删除。


②那么每一次我们取出当前最小需要删除的数,因为每个数只会出现一次,那么定位这个数原来在树中的位子是O(1)可以很容易做到的,将这个位子找到之后,我们向左拓展到最远处,使得最远处到当前这个数的位子的这个区间:【PosL,now】中,没有数字小于这个数。然后再向右拓展到最远处,使得最远处到当前这个数的位子的这个区间:【now,PosR】中,没有数字小于这个数。

因为我们删除的过程是从小到大的,所以我们这里无需考虑原数组中需要被删除的数字的情况,这些数即使在区间【PosL,now】中并且小于a【now】,我们也无需考虑。因为在此操作之前,我们已经将这个小于a【now】的数字已经删除掉了。

那么这里我们可以处理出来另外一个数组b【】;使得需要被删除的位子上的数b【i】=INF,不需要被删除的位子上的数b【i】=a【i】,那么我们二分的过程在b数组上进行即可。


我们要确定一个区间中是否包含一个数小于a【now】,我们还需要预处理一个区间RMQ,预处理一个ST表即可。


③然后我们每一次找到区间并且统计了值之后,需要一颗树状数组来维护哪些位子上还有数,哪些位子上的数已经删除了,用于统计答案过程所用。


问题稍微有些复杂,口述有很多不恰当的描述,具体参考代码理解即可,每一部分都是独立的,很好理解。


注意卡常问题= =


Ac代码(1990+ms过的):

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
struct node
{
    int val,pos;
}del[1450000];
int poww[54];
int Len[1450000];
int vis[1450000];
int a[1450000];
int b[1450000];
int n,m;
int cmp(node a,node b)
{
    return a.val<b.val;
}
/**************************************/
int tree[1540005];
int lowbit(int x)
{
    return x&(-x);
}
int sum(int x)
{
    int sum=0;
    while(x>0)
    {
        sum+=tree[x];
        x-=lowbit(x);
    }
    return sum;
}
void add(int x,int c)
{
    while(x<=n)
    {
        tree[x]+=c;
        x+=lowbit(x);
    }
}
/**************************************/
int minn[1200005][40];
void ST()
{
    int len=floor(log10(double(n))/log10(double(2)));
    for(int j=1;j<=len;j++)
    {
        for(int i=1;i<=n+1-(1<<j);i++)
        {
            minn[i][j]=min(minn[i][j-1],minn[i+(1<<(j-1))][j-1]);
        }
    }
}
int getminn(int a,int b)
{
    int len=Len[b-a+1];
    return min(minn[a][len], minn[b-poww[len]+1][len]);
}
int main()
{
    poww[0]=1;
    for(int i=1;i<=30;i++)
    {
        poww[i]=poww[i-1]*2;
    }
    for(int i=0;i<=1000000;i++)
    {
        Len[i]=floor(log10(double(i))/log10(double(2)));
    }
    while(~scanf("%d%d",&n,&m))
    {
        memset(vis,0,sizeof(vis));
        memset(tree,0,sizeof(tree));
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        for(int i=1;i<=m;i++)
        {
            int x;scanf("%d",&x);
            vis[x]++;
        }
        int cnt=0;
        for(int i=1;i<=n;i++)
        {
            if(vis[a[i]]==0)
            {
                ++cnt;
                del[cnt].val=a[i];
                del[cnt].pos=i;
            }
        }
        for(int i=1;i<=n;i++)
        {
            if(vis[a[i]]==1)b[i]=a[i];
            else b[i]=0x3f3f3f3f;
            minn[i][0]=b[i];
        }
        ST();
        __int64 output=0;
        sort(del+1,del+1+cnt,cmp);
        for(int i=1;i<=n;i++)add(i,1);
        int tot=0;
        for(int i=1;i<=cnt;i++)
        {
            int j=del[i].pos;
            int valj=del[i].val;
            int PosL=-1;
            int l=1;
            int r=j;
            while(r-l>=0)
            {
                int mid=(l+r)/2;
                if(getminn(mid,j)>valj)
                {
                    PosL=mid;
                    r=mid-1;
                }
                else l=mid+1;
            }
            int PosR=-1;
            l=j;
            r=n;
            while(r-l>=0)
            {
                int mid=(l+r)/2;
                if(getminn(j,mid)>valj)
                {
                    PosR=mid;
                    l=mid+1;
                }
                else r=mid-1;
            }
            output+=sum(PosR)-sum(PosL-1);
            add(j,-1);
        }
        printf("%I64d\n",output);
    }
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值