George and Cards
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的更多相关文章
- 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 ...
- 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 问题描述 ...
- Codeforces 387E George and Cards
George and Cards 我们找到每个要被删的数字左边和右边第一个比它小的没被删的数字的位置.然后从小到大枚举要被删的数, 求答案. #include<bits/stdc++.h> ...
- cf B George and Cards
题意:给你一个只有‘.’和'#'的n*n的格子,问所有的'#'是不是只属于一个十字叉,如果不是输出NO,否则输出YES. #include <cstdio> #include <cs ...
- cf E. George and Cards
http://codeforces.com/contest/387/problem/E 题意:给你n个数,然后在输入k个数,这k个数都在n个数中出现,进行每一次操作就是在n个数中选择长度为w的连续序列 ...
- BZOJ 1004 【HNOI2008】 Cards
题目链接:Cards 听说这道题是染色问题的入门题,于是就去学了一下\(Bunside\)引理和\(P\acute{o}lya\)定理(其实还是没有懂),回来写这道题. 由于题目中保证"任意 ...
- 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 ...
- bzoj 1004 Cards
1004: [HNOI2008]Cards Description 小春现在很清闲,面对书桌上的N张牌,他决定给每张染色,目前小春只有3种颜色:红色,蓝色,绿色.他询问Sun有 多少种染色方案,Sun ...
- 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 ...
随机推荐
- CSU1312:榜单(模拟)
Description ZZY很喜欢流行音乐,每周都要跟踪世界各地各种榜单,例如Oricon和Billboard,现在给出每周各个单曲的销量请给出每周的TOP5以及TOP5中各个单曲的浮动情况. 量的 ...
- 颜色渐变的RGB计算
均匀渐变 渐变(Gradient)是美学中一条重要的形式美法则,与其相对应的是突变.形状.大小.位置.方向.色彩等视觉因素都可以进行渐变.在色彩中,色相.明度.纯度也都可以产生渐变效果,并会表现出具有 ...
- Mysql新建用户和数据库并授权
测试环境:Centos 6.3和Mysql 5.3 一.新建用户 //登录MYSQL@>mysql -u root -p@>密码//创建用户mysql> insert into my ...
- EsayUI datagrid 刷新问题
最近使用esayui 实现前台界面,在对父页面中datagrid列表项进行操作后,如果操作子页面是依附于父页面弹出的窗体,那么调用parent.$("#grid").datagri ...
- Configuring Robolectric
There are numerous ways to customize how Robolectric behaves at runtime. Config Annotation The prima ...
- 第四篇:python 高级之面向对象初级
python 高级之面向对象初级 python 高级之面向对象初级 本节内容 类的创建 类的构造方法 面向对象之封装 面向对象之继承 面向对象之多态 面向对象之成员 property 1.类的创建 ...
- table 数据少时 ,tr高度变化
table设置固定高度,如果点击分页,数据条数发生变化时,tr的高度会变化. 解决办法:table外 加div层 将table隔离.
- React组件的生命周期各环节运作流程
'use strict'; React.createClass({ //1.创建阶段 getDefaultProps:function(){ //在创建类的时候被调用 console.log('get ...
- 知识点摸清 - - position属性值之relative与absolute
两者共同特点是: 改变文档流 激活元素left.top.right.bottom.z-index属性 让元素”浮起来“,z-index>0 不同的是: 1.position:relative 会 ...
- maven 依赖冲突的问题
如果原始系统的jarA依赖jarB1.0 而新引入的jarC依赖jarB2.0 导致系统启动报错,提示冲突,这时为了保证原始系统的稳定性, 在pom.xml文件中引入jarC时,需要exclude掉j ...