题目大意:

输入n,k ;n次操作 找到覆盖次数在k及以上的段的总长

一开始位置在0 左右活动范围为1-1000000000

接下来n行描述每次操作的步数和方向

Sample Input

6 2
2 R
6 L
1 R
8 L
1 R
2 R

Sample Output

6

下面的方法用了 map 和 区间表示法 http://www.cnblogs.com/zquzjx/p/8321466.html(区间表示法看这里)

但是不同于 题解 https://www.luogu.org/problemnew/solution/P2205 扫描线的方法可以得到整段覆盖次数

(蠢哭)存在一种特殊情况

若指令为      则会出现

———1

2——————

——3

3  2            位置      -6   -1    0   1   5    6

R  5                                    +1            -1

L  11                      +1                         -1

R  5                       +1          -1

最后 map内的值为

-6    2

-1    2

0   2

1   2

5   2

6   0

最后结果为11 而答案应该是10 错在-1到0这一段事实上只有一次覆盖

也就是该法实际上只能得知map中当前端点的被覆盖次数

所以将区间表示法

原本的 末尾端点的下一点(key+1) -1

改成 末尾端点的下半点(key+0.5) -1

#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <cstring>
#include <stdio.h>
#include <map>
using namespace std;
int main()
{
//freopen("23695all.in","r",stdin);
int n,w;
while(~scanf("%d%d",&n,&w))
{
map <double,int> m; m.clear();
int key=,ed=; m[key]=;
int a; char op;
while(n--)
{
scanf("%d %c",&a,&op);
if(op=='R')
{
m[key]++; key+=a;
m[key+0.5]--;
ed=max(ed,key);
}
else
{
m[key+0.5]--; key-=a;
m[key]++;
}
} map <double,int>::iterator it;
int sum=;
for(it=m.begin();it!=m.end();it++)
{
sum+=it->second;
m[it->first]=sum;
//printf("%.1f %d\n",it->first,it->second);
}
double le,rig,ans=;
for(it=m.begin();it!=m.end();it++)
{
while(it!=m.end()&&it->second<w) it++;
le=it->first;
if(it==m.end()) break;
while(it!=m.end()&&it->second>=w) it++;
rig=it->first; if(it==m.end()) rig=ed+0.5;
if(rig==(int)rig) ans+=rig--le;
else ans+=rig-0.5-le; ///这部分修改下即可 //printf("%.1f %.1f %.1f %d\n",rig,le,ans,ed);
if(it==m.end()) break;
}
printf("%.0f\n",ans);
} return ;
}

若是输出map中所有的值

会发现实际上在头尾之间的端点的覆盖次数会出错

当连续往同一个方向移动时

若指令为      则会出现

—1——2

2 1         位置  0  1  2  3  4

R 1               +1     -1

R 2                   +1         -1

最后 map内的值为

0  1

1  2

2  1

3  1

4  0

虽然这对本题没有什么影响

因为中间端点的覆盖次数虽不准确但绝对会大于头尾端点

且在下一点便会被减去多余的部分 而答案是加右-1-左 所以不会有影响

如上样例若是k=2 那么ans=2-1-1=0

但题目若是要求查看某点的被覆盖次数则会WA

所以我们可以判断一下当前指令与上轮指令是否相同

#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <cstring>
#include <stdio.h>
#include <map>
#define INF 0x3f3f3f3f
using namespace std;
int main()
{
//freopen("23695all.in","r",stdin);
int n,w;
while(~scanf("%d%d",&n,&w))
{
map <double,int> m; m.clear();
int key=,st=INF,ed=; m[key]=;
int a,cnt=; char op,ops='A';
while(n--)
{
scanf("%d %c",&a,&op);
if(op=='R')
{
if(op!=ops)
{ m[key]++; key+=a;
m[key+0.5]--;}
else
{ m[key+0.5]++; key+=a;
m[key+0.5]--;}
ed=max(ed,key);
}
else
{
if(op!=ops)
{ m[key+0.5]--; key-=a;
m[key]++;}
else
{ m[key]--; key-=a;
m[key]++;}
st=min(st,key);
}
ops=op;
} map <double,int>::iterator it;
int sum=;
for(it=m.begin();it!=m.end();it++)
{
sum+=it->second;
m[it->first]=sum;
//printf("%.1f %d\n",it->first,it->second);
}
double le,rig,ans=;
for(it=m.begin();it!=m.end();it++)
{
while(it!=m.end()&&it->second<w) it++;
le=it->first;
if(it==m.end()) break;
while(it!=m.end()&&it->second>=w) it++;
rig=it->first;
if(it==m.end()) rig=ed+0.5;
if(rig==(int)rig) ans+=rig--le;
else ans+=rig-0.5-le;
//printf("%.1f %.1f %.1f %d\n",rig,le,ans,ed);
if(it==m.end()) break;
}
printf("%.0f\n",ans);
} return ;
} ///

此时 当连续往同一个方向移动时

若指令为      则会出现

—1——2

2 1         位置  0  1   2  3  4

R 1               +1      -1

R 2                        +1     -1

最后 map内的值为

0  1

1  1

2  1

3  1

4  0

这样的话每个端点的覆盖次数就都是准确的了

USACO 2013 January Silver Painting the Fence /// oj23695的更多相关文章

  1. USACO翻译:USACO 2013 NOV Silver三题

    USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...

  2. USACO翻译:USACO 2013 DEC Silver三题

    USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...

  3. USACO 2013 Nov Silver Pogo-Cow

    最近因为闲的蛋疼(停课了),所以开始做一些 USACO 的银组题.被完虐啊 TAT 貌似 Pogo-Cow 这题是 2013 Nov Silver 唯一一道可说的题目? Pogo-Cow Descri ...

  4. USACO 2008 January Silver Telephone Lines /// 二分最短路 邻接表dijkstra oj22924

    题目大意: 一共有N (1 ≤ N ≤ 1,000)个电线杆,有P P (1 ≤ P ≤ 10,000)对电线杆是可以连接的, 用几条线连接在一起的电线杆之间都可相互通信,现在想要使得电线杆1和电线杆 ...

  5. [luogu P2205] [USACO13JAN]画栅栏Painting the Fence

    [luogu P2205] [USACO13JAN]画栅栏Painting the Fence 题目描述 Farmer John has devised a brilliant method to p ...

  6. USACO翻译:USACO 2013 JAN三题(1)

    USACO 2013 JAN 一.题目概览 中文题目名称 镜子 栅栏油漆 奶牛排队 英文题目名称 mirrors paint lineup 可执行文件名 mirrors paint lineup 输入 ...

  7. USACO翻译:USACO 2014 DEC Silver三题

    USACO 2014 DEC SILVER 一.题目概览 中文题目名称 回程 马拉松 奶牛慢跑 英文题目名称 piggyback marathon cowjog 可执行文件名 piggyback ma ...

  8. USACO翻译:USACO 2012 FEB Silver三题

    USACO 2012 FEB SILVER 一.题目概览 中文题目名称 矩形草地 奶牛IDs 搬家 英文题目名称 planting cowids relocate 可执行文件名 planting co ...

  9. USACO翻译:USACO 2014 FEB SILVER 三题

    USACO 2014 FEB SILVER 一.题目概览 中文题目名称 自动打字 路障 神秘代码 英文题目名称 auto rblock scode 可执行文件名 auto rblock scode 输 ...

随机推荐

  1. html图片导入画布

    首先定义一个画布 canvas id="myCanvas"></canvas> var canvas = document.getElementById('myC ...

  2. thinkphp session驱动

    默认的session驱动的命名空间是Think\Session\Driver,并实现下面的驱动接口:大理石构件哪家好 方法说明 接口方法 打开Session open($savePath, $sess ...

  3. How to SSH Into Your iPhone

    First, I will explain what SSH is and why we do it. SSH (Secure Shell) allows you to exchange data b ...

  4. Spring源码由浅入深系列四 创建BeanFactory

    继上一章refresh之后,上图描述了obtainFreshBeanFactory过程.

  5. LA 3263 /// 欧拉定理 oj21860

    题目大意: n个端点的一笔画 第n个和第1个重合 即一笔画必定是闭合曲线 输出平面被分成的区域数 欧拉定理 V+F-E=2 即 点数+面数-边数=2 (这里的面数包括了外部) #include < ...

  6. 更改idea启动内存信息

    1.到idea的安装目录的bin下,找idea64.exe.vmoptions 文件 2.更改参数 对应的参数解释: -Xms1024m    设置IDEA初时的内存大小,提高Java程序的启动速度. ...

  7. 今天真的很SB

    在公司Review系统网页上,写了一篇几百字的作文, 然后突然手一抖,竟然没有保存就切换页面了, 赶快退回来...没了,啥都没了... 怎么办... 还好洒家N久之前看了一本什么什么杂七杂八的书, 里 ...

  8. python 的打开、读、写、追加操作

    读f = open('D:\最新全栈python第2期视频教程 全套完整版\day08-python 全栈开发-基础篇\新建文本文档.txt','r') # data = f.read(10000)# ...

  9. linq语句,常用的查询,模糊查询,实体查询

    查询: //List是要查询的实体列表的集合 List.FindAll(n => n.NAME == NAME),    //NAME变量是要查询的条件 模糊查询 List.FindAll(s ...

  10. drupal7 代码生成用户,并自动登录

    直接上代码 1. 生成用户(注册) $edit = [ "name" => "name", "pass" => "pa ...