Codeforces Round #227 (Div. 2) E. George and Cards 线段树+set

时间:2023-01-16 22:23:31

题目链接:

题目

E. George and Cards

time limit per test:2 seconds

memory limit per test:256 megabytes

问题描述

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!

输入

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.

输出

Print a single integer — the maximum number of pieces of sausage that George can get if he acts optimally well.

样例

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子序列,要你把不属于这个子序列的数都删了,你可以选择的操作是每次选连续的w个数,然后删除最小的那个,并且你能够获得w的贡献值,问你如何操作n-k次使贡献值的和最大。

题解

只要我们按序列的值递增来做,并且每次贪心取最大的区间,就能得到最优,一开始我用线段树维护sum和min,在每次操作时用二分+区间min找到左右界,用sum统计区间内还存在的数的和。复杂度O(nlogn+n(logn)^2)第八组数据就t了。(orz)

正解是一开始就用一个set只维护那些比当前数小的并且最后一定会保留下来的数(不会保留下来的,比当前数小的数都已经删除了,所以根本不要放到set里面,这样子做的话每次找x的前驱后继就可以做完了,再加一个线段树或树状数组维护一下区间和就可以了,时间复杂度只有O(nlogn)。

代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<set>
#define X first
#define Y second
#define M l+(r-l)/2
#define lson (o<<1)
#define rson ((o<<1)|1)
using namespace std; const int maxn = 1e6+10;
const int INF = 0x3f3f3f3f;
typedef __int64 LL; int minv[maxn << 2];
LL sumv[maxn << 2];
int used[maxn],id[maxn];
int n, k;
set<int> st; int _p, _v;
void update(int o, int l, int r) {
if (l == r) {
sumv[o] = _v;
}
else {
if (_p <= M) update(lson, l, M);
else update(rson, M + 1, r);
minv[o] = min(minv[lson], minv[rson]);
sumv[o] = sumv[lson] + sumv[rson];
}
} int ql, qr;
LL _sum;
void query(int o, int l, int r) {
if (ql <= l&&r <= qr) {
_sum += sumv[o];
}
else {
if (ql <= M) query(lson, l, M);
if (qr > M) query(rson, M + 1, r);
}
} int main() {
memset(used, 0, sizeof(used));
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
int x; scanf("%d", &x);
id[x] = i;
_p = i; _v = 1;
update(1, 1, n);
}
for (int i = 1; i <= k; i++) {
int x; scanf("%d",&x);
used[x] = 1;
}
LL ans = 0;
st.insert(0);
st.insert(n + 1);
for (int i = 1; i <= n; i++) {
if (!used[i]) {
set<int>::iterator it = st.upper_bound(id[i]);
qr = *it - 1; ql = *(--it) + 1;
_sum = 0;
query(1, 1, n);
ans += _sum;
_p = id[i], _v = 0;
update(1, 1, n);
}
else {
st.insert(id[i]);
}
}
printf("%I64d\n", ans);
return 0;
}

乱七八糟:

这道题和之前做过的用线段树来维护一个元素的名次的动态查询点这里有异曲同工之妙,都利用了数本身的有序性来排除其他干扰的元素。 这道题也体现了离线处理的巧妙,比如说排个序啥的是吧。

Codeforces Round #227 (Div. 2) E. George and Cards 线段树+set的更多相关文章

  1. Codeforces Round &num;227 &lpar;Div&period; 2&rpar; E&period; George and Cards set内二分&plus;树状数组

    E. George and Cards   George is a cat, so he loves playing very much. Vitaly put n cards in a row in ...

  2. Codeforces Round &num;292 &lpar;Div&period; 1&rpar; C&period; Drazil and Park 线段树

    C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...

  3. Codeforces Round &num;254 &lpar;Div&period; 1&rpar; C&period; DZY Loves Colors 线段树

    题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...

  4. Codeforces Round &num;337 &lpar;Div&period; 2&rpar; D&period; Vika and Segments 线段树扫描线

    D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...

  5. Codeforces Round &num;337 &lpar;Div&period; 2&rpar; D&period; Vika and Segments &lpar;线段树&plus;扫描线&plus;离散化&rpar;

    题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...

  6. Codeforces Round &num;149 &lpar;Div&period; 2&rpar; E&period; XOR on Segment &lpar;线段树成段更新&plus;二进制&rpar;

    题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...

  7. Codeforces Round &num;321 &lpar;Div&period; 2&rpar; E&period; Kefa and Watch 线段树hash

    E. Kefa and Watch Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/prob ...

  8. Codeforces Round &num;271 &lpar;Div&period; 2&rpar; E题 Pillars(线段树维护DP)

    题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...

  9. Codeforces Round &num;207 &lpar;Div&period; 1&rpar; A&period; Knight Tournament (线段树离线)

    题目:http://codeforces.com/problemset/problem/356/A 题意:首先给你n,m,代表有n个人还有m次描述,下面m行,每行l,r,x,代表l到r这个区间都被x所 ...

随机推荐

  1. MyBatis的动态SQL操作--更新

    更新条件不确定,需要根据具体的情况生成sql语句. id是主键,一般不会去更新. 1.只更新name的值 update student set name = ? where id = ? 2.只更新s ...

  2. maven创建web项目

    上一次自己使用Maven还是在大三在学校做项目时.现在公司有个新项目,想重新使用一下maven,顺便记下一些步骤 1.安装maven 1.1 访问(http://maven.apache.org/), ...

  3. C&num;开发微信门户及应用-使用地理位置扩展相关应用

    C#开发微信门户及应用-使用地理位置扩展相关应用 我们知道,地理位置信息可以用来做很多相关的应用,除了我们可以知道用户所在的位置,还可以关联出一些地理位置的应用,如天气,热映影片,附近景点,附近影院, ...

  4. 自定义类似QMutexLocker的CMutexLocker

    最近做项目遇到一个需求,有一个buttonSlot()执行要耗点时间,为了防止用户无限制的乱点出现问题,考虑加一个互斥锁,使得每次执行完后才允许执行下一次.大概意思是: //QMutex  m_mut ...

  5. POJ 3892 RSA Factorization

    题目地址:http://poj.org/problem?id=3892 题目大意:RSA分解. 这儿的N比较大,要用高精度,如果一般的肯定分解不了,但是这儿有一个限制 |q-kp|<=10000 ...

  6. 【百度地图API】如何利用PhoneGap制作地图APP

    原文:[百度地图API]如何利用PhoneGap制作地图APP 摘要:百度地图API是一套由javascript编写的地图程序接口,按说它应该运行在浏览器上.现在,只要利用PhoneGap,我们就能开 ...

  7. 理解iOS软件开发框架

    iOS软件开发框架理解 这个东西是硬伤,框架?自带的mvc? 自带的UIViewController UIView UINavigationController 这些算不算?当然算的,cocoa框架嘛 ...

  8. Asp&period;net web api 知多少

    本系列主要翻译自<ASP.NET MVC Interview Questions and Answers >- By Shailendra Chauhan,想看英文原版的可访问http:/ ...

  9. Python可视化:Seaborn库热力图使用进阶

    前言 在日常工作中,经常可以见到各种各种精美的热力图,热力图的应用非常广泛,下面一起来学习下Python的Seaborn库中热力图(heatmap)如何来进行使用. 本次运行的环境为: windows ...

  10. 利用ASP&period;netCore自带DI&lpar;DependencyInjection&rpar;实现批量依赖注入

    ASP.net Core自带DI(依赖注入),用法如下: services.AddScoped(typeof(IProductService), typeof(ProductService)); 如果 ...