http://codeforces.com/contest/387/problem/E

题意:给你n个数,然后在输入k个数,这k个数都在n个数中出现,进行每一次操作就是在n个数中选择长度为w的连续序列,然后删除这w个数中的最小的一个,然后你就会的到w个奖励,如何获得最多奖励?

思路:set+数状数组,数状数组用来记录在每一个连续的区间内数的个数,用来记录删除和添加数的个数,先对a数组中的数记录每一个数在序列中的位置,再对b数组进行标记,然后遍历1-n,被标记数,把它的位置放在set里面,没有被标记的,在set里面二分查找到大于等于它位置的数,可以知道下界和上界,就可以知道这次的w,就可以求出答案。

 #include <cstdio>
#include <cstring>
#include <set>
#include <algorithm>
#define ll long long
#define maxn 1000010
using namespace std; int n,k;
int p[maxn];
int b[maxn];
int c[maxn];
int pos[maxn];
bool vis[maxn];
struct node
{
int x,id;
bool operator <(const node &a)const
{
return x<a.x;
}
} f[maxn]; int lowbit(int x)
{
return x&-x;
} void insert(int x,int d)
{
while(x<maxn)
{
c[x]+=d;
x+=lowbit(x);
}
} int Getsum(int x)
{
int ans=;
while(x>)
{
ans+=c[x];
x-=lowbit(x);
}
return ans;
} int main()
{
while(scanf("%d%d",&n,&k)!=EOF)
{
set<int>q;
set<int>::iterator it;
for(int i=; i<=n; i++)
{
scanf("%d",&p[i]);
insert(i,);
pos[p[i]]=i;
}
for(int i=; i<=k; i++)
{
scanf("%d",&b[i]);
vis[b[i]]=true;
}
ll ans=;
q.insert(); q.insert(n+);
for(int i=; i<=n; i++)
{
if(vis[i])
{
q.insert(pos[i]);
}
else
{
it=q.lower_bound(pos[i]);
int r=*it-;
int l=*(--it);
ans+=Getsum(r)-Getsum(l);
insert(pos[i],-);
}
}
printf("%lld\n",ans);
}
return ;
}

cf E. George and Cards的更多相关文章

  1. cf B George and Cards

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

  2. 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 ...

  3. 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 问题描述 ...

  4. Codeforces 387E George and Cards

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

  5. cf 546C Soldier and Cards

    题目链接:C. Soldier and Cards Two bored soldiers are playing card war. Their card deck consists of exact ...

  6. George and Cards

    Codeforces Round #227 (Div. 2) E:http://codeforces.com/contest/387/problem/E 题意:给你一个n个数的序列,然后给你一个标准序 ...

  7. cf D George and Interesting Graph

    题意:给你一个有趣图的定义:在这个图中有一个根,根与每个点都有边和回边,除了根之外,其他的点的出度和入度都为2,然后给你一个图让你经过几步操作可以使此图变为有趣图,操作为:删边或者加边. 思路:枚举根 ...

  8. cf C. George and Number

    http://codeforces.com/problemset/problem/387/C 题意:给你一个大数,让你求个集合,可以通过操作得到这个数,求集合中个数最大值,操作 :从集合中任意取两个数 ...

  9. cf B George and Round

    题意:输入n,m,下一行为n个数a1<a2<a3......<an:然后再输入m个数b1<=b2<=b3<.....<=bm: 每个ai都必须在b中找到相等的 ...

随机推荐

  1. range() 函数创建并返回一个包含指定范围的元素的数组

    语法 range(first,second,step) 参数 描述 first 必需.规定数组元素的最小值. second 必需.规定数组元素的最大值. step 可选.规定元素之间的步进制.默认是 ...

  2. Qt 学习之路:线程总结

    前面我们已经详细介绍过有关线程的一些值得注意的事项.现在我们开始对线程做一些总结. 有关线程,你可以做的是: 在QThread子类添加信号.这是绝对安全的,并且也是正确的(前面我们已经详细介绍过,发送 ...

  3. Volley的简单二次封装

    新建一个application package com.honghe.myvolley.app; import com.android.volley.RequestQueue; import com. ...

  4. 9.23 noip模拟试题

      Problem 1 抓牛(catchcow.cpp/c/pas) [题目描述] 农夫约翰被通知,他的一只奶牛逃逸了!所以他决定,马上出发,尽快把那只奶牛抓回来. 他们都站在数轴上.约翰在N(O≤N ...

  5. gulp的点点滴滴

    去年用gulp,但一直没有写篇博客,今天有时间无聊写一篇.... 什么是gulp?gulp的官网title上对这个工具有一个比较准确的定义,叫做:基于流的自动化构建工具.如果你查看它的网页源代码,还会 ...

  6. jsp页面表单的遍历要怎么写

    1.传统的方式使用request.getAttribute(“list”);获取表单的值, 2.也可以用struts2提供的标签进行遍历 备注 // 传统的接受参数方法 // String sfz=t ...

  7. Android 通信机制Message、Handler 的用法

    Android中提供了通信机制,Message.Handler 等,Message用于在子线程中传递数据,Handler用于发送数据到主线程中, 下面介绍基于Message.Handler的计时器 i ...

  8. 获取当前页面的url

    var url = window.location.href; var b = url.substring(url.lastIndexOf('/')+1, url.length);

  9. JSP标准标签库的安装以及自定义标签的创建

    JSTL 库安装 Apache Tomcat安装JSTL 库步骤如下: 从Apache的标准标签库中下载的二进包(jakarta-taglibs-standard-current.zip). 官方下载 ...

  10. Sqoop import加载HBase过程中,遇到Permission denied: user=root, access=WRITE, inode="/user":hdfs:supergroup:drwxr-xr-x

    在执行hbase sqoop抽取的时候,遇到了一个错误,如下图: 在执行程序的过程中,遇到权限问题很正常,也容易让人防不胜防,有问题就想办法解决,这个是关键. 解决办法如下: 第一步:su hdfs, ...