Codeforces Round #227 (Div. 2) E. George and Cards set内二分+树状数组
2016-09-29 16:28
507 查看
[b]E. George and Cards[/b]
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 ≤ 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 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!
[b]Input[/b]
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.
[b]Output[/b]
Print a single integer — the maximum number of pieces of sausage that George can get if he acts optimally well.
[b]Examples[/b]
[b]input[/b]
[b]output[/b]
[b]题意:[/b]
给你n个数只包含1~n
和一个k,以及k个数
让你从这n个中删除n-k个数, 余留给定的k个数
每次删除一个数,你可以选择连续的w个数,表示删除这个w个数中最小的数,同时获得w分数
问你如何删除 使得最后获得的分数最多,输出分数来
[b]题解:[/b]
也就是尽量重复使用那些必须删除的数
那么 从小到大删除就好了
如何计算答案?
从小到大枚举,
对于必须保留的数,将其位置插入set
对于必须删除的数,查找当前数i的位置在set中的前驱后继,表示答案的区间,
这个区间中可能有些数十被删除了的(比i小)那么 用一个树状数组或者线段树计算区间和即可。
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 ≤ 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 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!
[b]Input[/b]
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.
[b]Output[/b]
Print a single integer — the maximum number of pieces of sausage that George can get if he acts optimally well.
[b]Examples[/b]
[b]input[/b]
3 2 2 1 3 1 3
[b]output[/b]
1
[b]题意:[/b]
给你n个数只包含1~n
和一个k,以及k个数
让你从这n个中删除n-k个数, 余留给定的k个数
每次删除一个数,你可以选择连续的w个数,表示删除这个w个数中最小的数,同时获得w分数
问你如何删除 使得最后获得的分数最多,输出分数来
[b]题解:[/b]
也就是尽量重复使用那些必须删除的数
那么 从小到大删除就好了
如何计算答案?
从小到大枚举,
对于必须保留的数,将其位置插入set
对于必须删除的数,查找当前数i的位置在set中的前驱后继,表示答案的区间,
这个区间中可能有些数十被删除了的(比i小)那么 用一个树状数组或者线段树计算区间和即可。
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<cmath> #include<set> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define ls i<<1 #define rs ls | 1 #define mid ((ll+rr)>>1) #define pii pair<int,int> #define MP make_pair typedef long long LL; const long long INF = 1e18; const double Pi = acos(-1.0); const int N = 1e6+10, M = 1e6, mod = 1e9+7, inf = 2e9; int n,k,a ,pos ,x,vis ; LL ans = 0; int C ; void update(int x,int c) { for(int i = x; i < N; i += i&(-i)) C[i] += c; } int ask(int x) { int s = 0; for(int i = x; i; i -= i&(-i)) s+=C[i]; return s; } int main() { scanf("%d%d",&n,&k); for(int i = 1; i <= n; ++i) scanf("%d",&a[i]),pos[a[i]] = i; for(int i = 1; i <= n; ++i) update(i,1); for(int i = 1; i <= k; ++i) scanf("%d",&x),vis[x] = 1; set<int > s; s.insert(0),s.insert(n+1); for(int i = 1; i <= n; ++i) { if(!vis[i]) { int bef = *(--s.lower_bound(pos[i])); int blc = *(s.lower_bound(pos[i])); ans += ask(blc-1) - ask(bef); update(pos[i],-1); } else { s.insert(pos[i]); } } cout<<ans<<endl; return 0; }
相关文章推荐
- Codeforces Round #227 (Div. 2)B. George and Round
- E. Vladik and cards Codeforces Round #384 (Div. 2) 好题 二分+(贪心+状态压缩DP)判断
- Codeforces Round #129 (Div. 1)B Little Elephant and Cards
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)——A二分——Bear and Elections
- Codeforces Round #235 (Div. 2) A. Vanya and Cards
- Codeforces Round #281 (Div. 2) C. Vasya and Basketball 二分
- Codeforces Round #235 (Div. 2) A. Vanya and Cards
- 【Codeforces Round 262 (Div 2)D】【构造】Little Victor and Set 集合最多取k数使得异或值尽可能小
- Codeforces Round #227 (Div. 2) E. George and Cards 线段树+set
- 【Codeforces Round 331 (Div 2)C】【脑洞 SET贪心写法】Wilbur and Points 锯齿图形差值编号,右上角的最大
- Codeforces Round #267 (Div. 2) C. George and Job DP
- Codeforces Round #281 (Div. 2) C. Vasya and Basketball 枚举+二分
- Codeforces Round #129 (Div. 1) B. Little Elephant and Cards
- Codeforces Round #182 (Div. 1) B. Yaroslav and Time(二分+SPFA变形)
- Codeforces Round #129 (Div. 1)B Little Elephant and Cards
- Codeforces Round #267 (Div. 2) C. George and Job
- 暴力/set Codeforces Round #291 (Div. 2) C. Watto and Mechanism
- Codeforces Round #304 (Div. 2)——Cdfs——Soldier and Cards
- Codeforces Round #129 (Div. 1) B. Little Elephant and Cards
- 【Educational Codeforces Round 6D】【暴力 SET二分】Professor GukiZ and Two Arrays