The first thing to know is that each take the minimum to achieve the best, because the smallest will not provide any additional points, only the possibility of reducing the bonus points.
The order of the deleted cards is determined, and the rest is to determine the left and right endpoints of each interval.
Pos[i] represents the position of the number I in the initial sequence.
First enumerates the I (i = 1, n) and, if no deletion is required, puts Pos[i] into set<int> s and, if no deletion is required, finds the upper bound in the second part of S.
The total time complexity is 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;}
Codefoeces 387e-george and Cards greedy + line tree