Codeforces Round #227 (Div. 2) E:http://codeforces.com/contest/387/problem/E

题意:给你一个n个数的序列,然后给你一个标准序列,现在然后删除原序列的一些数,让原序列变成标准序列。其中,每次查询可以选择一个连续的序列,然后要删除的数字必须是这个序列中的最小的那个。每一次删除,你可以获得这个长度的价值,问你最多可以得到多少价值。

题解:首先,很明显,每次删除要从要删除的数中选择最小的来删除,而且每次删除就是以包含这个数为最小数的最大串。关键是怎么找到这个串。观察可以知道,举个例子,

14 6
7 6 10 9 11 8 14 3 1 13 12 4 5 2
7 10 11 12 4 5

假如说我们要删除8,那么肯定要找离8最近的并且比8小的,没有被删除的数的位子。得到这个区间之后,那么肯定是都是要删除的,要么事已经删除的,要么是没有删除的,那么真正的长度就是去掉那些删除元素的留下来长度。有了这些结论和想法之后。可以想到。每次从最小的数开始,用一个set维护不需要被删除的数的位子。如果当前的数是不要被删除的,那么就把这个数的位子放进set,然后如果这个数是要被删除的,就从set中找到比当前数位子大的位子,因为set中存的是比当前数小并且是不要被删除数的位子。所以lower_bound(ps[i]),得到是ps[i]右边最近的比i小不用被删除的数的位子,左移一位,那么得到就是ps[i]左边最近的比i小的不用被删除的数的位子。然后得到这个区间了,至于,那么已经被删除元素的,可以用树状数组来统计,都是log(n),总的复杂度nlog(n);
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<set>
using namespace std;
const int N=1e6+;
int ct[N];
bool visit[N];
int n,m,temp;
int ps[N];
long long ans;
int lowbit(int x){
return x&(-x);
}
void add(int x){
while(x<=n){
ct[x]++;
x+=lowbit(x);
}
}
int sum(int x){
int ans=;
while(x>){
ans+=ct[x];
x-=lowbit(x);
}
return ans;
}
int main(){
while(~scanf("%d%d",&n,&m)){
memset(ct,,sizeof(ct));
memset(ps,,sizeof(ps));
memset(visit,,sizeof(visit));
for(int i=;i<=n;i++){
scanf("%d",&temp);
ps[temp]=i;
}
for(int i=;i<=m;i++){
scanf("%d",&temp);
visit[temp]=;
}
ans=;
set<int>Q;int l,r;
for(int i=;i<=n;i++){
if(visit[i]==){
Q.insert(ps[i]);
}
else{
set<int>::iterator it=Q.lower_bound(ps[i]);
if(it==Q.end()){
if(Q.size()==){
r=n;
l=;
}
else{
r=n;
l=(*(--it))+;
}
}
else if(it==Q.begin()){
l=;
r=(*it)-;
}
else{
r=(*it)-;
l=(*(--it))+;
}
// printf("%d %d %d\n",i,l,r);
ans+=r-l+;
ans-=sum(r);
ans+=sum(l-);
add(ps[i]);
}
}
printf("%I64d\n",ans);
}
}
 

George and Cards的更多相关文章

  1. Codeforces Round #227 (Div. 2) E. George and Cards set内二分+树状数组

    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 #227 (Div. 2) E. George and Cards 线段树+set

    题目链接: 题目 E. George and Cards time limit per test:2 seconds memory limit per test:256 megabytes 问题描述 ...

  3. Codeforces 387E George and Cards

    George and Cards 我们找到每个要被删的数字左边和右边第一个比它小的没被删的数字的位置.然后从小到大枚举要被删的数, 求答案. #include<bits/stdc++.h> ...

  4. cf B George and Cards

    题意:给你一个只有‘.’和'#'的n*n的格子,问所有的'#'是不是只属于一个十字叉,如果不是输出NO,否则输出YES. #include <cstdio> #include <cs ...

  5. cf E. George and Cards

    http://codeforces.com/contest/387/problem/E 题意:给你n个数,然后在输入k个数,这k个数都在n个数中出现,进行每一次操作就是在n个数中选择长度为w的连续序列 ...

  6. BZOJ 1004 【HNOI2008】 Cards

    题目链接:Cards 听说这道题是染色问题的入门题,于是就去学了一下\(Bunside\)引理和\(P\acute{o}lya\)定理(其实还是没有懂),回来写这道题. 由于题目中保证"任意 ...

  7. Codeforces Round #384 (Div. 2) 734E Vladik and cards

    E. Vladik and cards time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. bzoj 1004 Cards

    1004: [HNOI2008]Cards Description 小春现在很清闲,面对书桌上的N张牌,他决定给每张染色,目前小春只有3种颜色:红色,蓝色,绿色.他询问Sun有 多少种染色方案,Sun ...

  9. codeforces 744C Hongcow Buys a Deck of Cards

    C. Hongcow Buys a Deck of Cards time limit per test 2 seconds memory limit per test 256 megabytes in ...

随机推荐

  1. Delphi十进制和十六进制互转

    Delphi 自带函数 IntToHex 功能说明:该函数用于将“十进制”转换成“十六进制”.该函数有二个参数.第一个参数为要转换的十进制数据,第二个参数是指定使用多少位来显示十六进制数据. 参考实例 ...

  2. linux 上查找pid,筛选出来

    ps -ef | grep httpd find / -name "1000sql.txt" 查找命令

  3. 自定义 textField 的清除 button

    UIButton *clearButton = [self.textField valueForKey:@"_clearButton"]; [clearButton setImag ...

  4. 理解JavaScript的定时器与回调机制

    定时器方法 JavaScript是单线程的.虽然HTML5已经开始支持异步js了. JavaScript的setTimeout与setInterval看起来就像已经是多线程的了.但实际上setTime ...

  5. 一款js、css压缩工具yuicompressor

    //压缩JS java -jar yuicompressor-.jar --type js --charset utf- -v src.js > packed.js //压缩CSS java - ...

  6. 表达式:使用API创建表达式树(6)

    一.ConstantExpression:表示具有常量值的表达式.因为表达式应用过程中,参数据多是 Expressions 类型,算是对常量值的一种包装吧. ConstantExpression使用比 ...

  7. HTML5 <Audio>标签API整理(一)

    简单实例: <audio id="myAudio"></audio> <script> var myAudio = document.getEl ...

  8. 获取IP所在地

    $source=file_get_contents('http://www.ip138.com/ips138.asp?ip='.$ip.'&action=2'); preg_match_all ...

  9. vim备注

    ① 用户path生效 在~/.bashrc中修改path,在~/.profile中source bashrc ② secureCRT着色方案 底色RGB:43 43 43 前景色RGB:221 221 ...

  10. C#堆栈原理(我有两个例子测试你到底会不会)

    背景 上次写了一篇文章关于try finnally的一些疑问(被我用windows live覆盖了,草),后来经过大神们解释,我明白了在我理解了try.finnally运行原理后,还欠缺的就是关于值类 ...