Codeforces Round #227 (Div. 2)---E. George and Cards(贪心, 树状数组+set维护, 好题!)

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!
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.
Sample test(s)
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-k次操作,每次操作选择一段连续的序列,可以删掉这段序列的最小值,得到的价值是这段序列的长度
那么显然,每次应该从小的开始删起,而且区间越长越好,一开始我用rmq+二分来确定区间,结果各种T
后来改成set,边操作边往set里丢数字(这些数字的位置),然后可以过了
注意,确定区间后,加答案的时候要去掉这个区间里已经被删掉的数个数
用树状数组就行

/************************************************************************* > File Name: CF-227-E.cpp > Author: ALex > Mail: [email protected] > Created Time: 2015年04月28日 星期二 10时54分20秒 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

static const int N = 1001010;

int arr[N];
int tar[N];
int tmp[N];
PLL qu[N];
int tree[N];
set <int> st;
set <int> :: iterator it;

int lowbit(int x) {
    return x & (-x);
}

void add(int n, int x, int val) {
    for (int i = x; i <= n; i += lowbit(i)) {
        tree[i] += val;
    }
}

int getsum(int x) {
    int ans = 0;
    for (int i = x; i; i -= lowbit(i)) {
        ans += tree[i];
    }
    return ans;
}

int main() {
    int n, k, u;
    while (~scanf("%d%d", &n, &k)) {
        for (int i = 1; i <= n; ++i) {
            scanf("%d", &u);
            tmp[u] = 1;
            arr[u] = i;
            add(n, i, 1);
        }
        for (int i = 1; i <= k; ++i) {
            scanf("%d", &u);
            tmp[u] = 0;
        }
        st.clear();
        st.insert(0);
        st.insert(n + 1);
        LL ans = 0;
        for (int i = 1; i <= n; ++i) {
            if (!tmp[i]) {
                st.insert(arr[i]);
                continue;
            }
            it = st.upper_bound(arr[i]);
            int r = *it - 1;
            int l = *(--it);
            ans += getsum(r) - getsum(l);
            add(n, arr[i], -1);
        }
        printf("%lld\n", ans);
    }
    return 0;
}

你可能感兴趣的:(STL,树状数组,贪心)