Codefoeces 387E,codefoeces387e

來源:互聯網
上載者:User

Codefoeces 387E,codefoeces387e

首先要知道每次拿走最小才會達到最優,因為最小的不會給其他的提供任何加分,只有可能減小加分。

刪除卡片的次序確定了,剩下的就是確定每段區間的左右端點。


pos[i] 表示數字 i 在初始序列中的位置。

首先枚舉i (i = 1 -> n),如果不需刪除,則將pos[i]放入set<int> S中,如果不需刪除,則在S中二分尋找上下界。

總的時間複雜度為o(  (n-k)*log(k)  )。

#include <algorithm>#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <queue>#include <cmath>#include <stack>#include <map>#include <set>#include <ctime>#include <iomanip>#pragma comment(linker,"/STACK:1024000000");#define EPS (1e-6)#define LL long long#define ULL unsigned long long#define INF 0x3f3f3f3f#define Mod 1000000007#define mod 1000000007/** I/O Accelerator Interface .. **/#define g (c=getchar())#define d isdigit(g)#define p x=x*10+c-'0'#define n x=x*10+'0'-c#define pp l/=10,p#define nn l/=10,ntemplate<class T> inline T& RD(T &x){    char c;    while(!d);    x=c-'0';    while(d)p;    return x;}template<class T> inline T& RDD(T &x){    char c;    while(g,c!='-'&&!isdigit(c));    if (c=='-')    {        x='0'-g;        while(d)n;    }    else    {        x=c-'0';        while(d)p;    }    return x;}inline double& RF(double &x)      //scanf("%lf", &x);{    char c;    while(g,c!='-'&&c!='.'&&!isdigit(c));    if(c=='-')if(g=='.')        {            x=0;            double l=1;            while(d)nn;            x*=l;        }        else        {            x='0'-c;            while(d)n;            if(c=='.')            {                double l=1;                while(d)nn;                x*=l;            }        }    else if(c=='.')    {        x=0;        double l=1;        while(d)pp;        x*=l;    }    else    {        x=c-'0';        while(d)p;        if(c=='.')        {            double l=1;            while(d)pp;            x*=l;        }    }    return x;}#undef nn#undef pp#undef n#undef p#undef d#undef gusing namespace std;int num[1000010];int pos[1000010];bool ap[1000010];int st[4001000];set<int> s;int Init(int site,int l,int r){    if(l == r)        return st[site] = 1;    int mid = (l+r)>>1;    return st[site] = Init(site<<1,l,mid) + Init(site<<1|1,mid+1,r);}int Query(int site,int L,int R,int l,int r){    if(L == l && R == r)        return st[site];    int mid = (L+R)>>1;    if(r <= mid)        return Query(site<<1,L,mid,l,r);    if(mid < l)        return Query(site<<1|1,mid+1,R,l,r);    return Query(site<<1,L,mid,l,mid) + Query(site<<1|1,mid+1,R,mid+1,r);}void Update(int site,int l,int r,int x){    if(l == r)    {        st[site] = 0;        return ;    }    int mid = (l+r)>>1;    if(x <= mid)        Update(site<<1,l,mid,x);    else        Update(site<<1|1,mid+1,r,x);    st[site] = st[site<<1] + st[site<<1|1];}int main(){    int n,k,i,j,x;    scanf("%d %d",&n,&k);    for(i = 1;i <= n; ++i)        scanf("%d",&num[i]),pos[num[i]] = i;    memset(ap,false,sizeof(ap));    for(i = 1;i <= k; ++i)        scanf("%d",&x),ap[x] = true;    set<int>::iterator it;    LL sum = 0;    Init(1,1,n);    s.insert(n+1);    s.insert(0);    for(i = 1;i <= n; ++i)    {        if(ap[i])        {            s.insert(pos[i]);            continue;        }        it = s.upper_bound(pos[i]);        int r = *it-1;        int l = *(--it)+1;        sum += Query(1,1,n,l,r);        Update(1,1,n,pos[i]);    }    cout<<sum<<endl;    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.