Codeforces 387E George and Cards(二分+树状数组)
来源:互联网 发布:ubuntu 无法复制 编辑:程序博客网 时间:2024/06/03 07:57
Codeforces 387E George and Cards
George and Cards
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!
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.
Print a single integer — the maximum number of pieces of sausage that George can get if he acts optimally well.
3 22 1 31 3
1
10 51 2 3 4 5 6 7 8 9 102 4 6 8 10
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;}
- Codeforces 387E George and Cards(二分+树状数组)
- Codeforces 387E George and Cards(二分+树状数组)
- George and Cards - CodeForces 387E 树状数组
- Codeforces 387E George and Cards【思维+RMQ+二分+树状数组】被卡常= =
- Codeforces 387E George and Cards 树状数组 + 集合set查询
- CodeForces 387E George and Cards
- Codeforces Round #227 (Div. 2)---E. George and Cards(贪心, 树状数组+set维护, 好题!)
- Codeforces #227(Div2) E. George and Cards 贪心+二分
- Codeforces-831E Cards Sorting(树状数组)
- Codefoeces 387E - George and Cards 贪心+线段树
- Codeforces 831 E. Cards Sorting set+树状数组模拟
- Codeforces Round #424 (Div. 2) E. Cards Sorting(树状数组)
- codeforces 191 E (树状数组+二分)
- Codeforces Round #424 (Div. 2)C. Jury Marks 暴力 D. Office Keys 二分 E. Cards Sorting 树状数组
- 【二分+乱搞】Codeforces 743E Vladik and cards
- Codeforces 387E 树状数组
- 【二分图匹配】Codeforces 387D George and Interesting Graph
- Codeforces 387D George and Interesting Graph【思维+二分匹配】
- 371. Sum of Two Integers
- 查找链表倒数的节点
- POJ2533-Longest Ordered Subsequence(LIS)
- python pil 第三方库实战之三:ImageFilter滤镜小试
- #Paper Reading# Graph Regularized Non-negative Matrix Factorization By Maximizing Correntropy
- Codeforces 387E George and Cards(二分+树状数组)
- 22.Identify the logical structure that will never have more than one data segment created for it.
- Ubuntu下lamp环境搭建【学习笔记】
- 对vector等STL容器进行排序
- 进程间通信messager
- Swift 类和结构体总结
- oracle基础复习-2016.7.18
- 弦图判断
- iOS开发-搜索栏UISearchBar和UISearchController