USACO 2013 January Silver Painting the Fence /// oj23695
题目大意:
输入n,k ;n次操作 找到覆盖次数在k及以上的段的总长
一开始位置在0 左右活动范围为1-1000000000
接下来n行描述每次操作的步数和方向
6 2
2 R
6 L
1 R
8 L
1 R
2 R
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的更多相关文章
- USACO翻译:USACO 2013 NOV Silver三题
USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...
- USACO翻译:USACO 2013 DEC Silver三题
USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...
- USACO 2013 Nov Silver Pogo-Cow
最近因为闲的蛋疼(停课了),所以开始做一些 USACO 的银组题.被完虐啊 TAT 貌似 Pogo-Cow 这题是 2013 Nov Silver 唯一一道可说的题目? Pogo-Cow Descri ...
- USACO 2008 January Silver Telephone Lines /// 二分最短路 邻接表dijkstra oj22924
题目大意: 一共有N (1 ≤ N ≤ 1,000)个电线杆,有P P (1 ≤ P ≤ 10,000)对电线杆是可以连接的, 用几条线连接在一起的电线杆之间都可相互通信,现在想要使得电线杆1和电线杆 ...
- [luogu P2205] [USACO13JAN]画栅栏Painting the Fence
[luogu P2205] [USACO13JAN]画栅栏Painting the Fence 题目描述 Farmer John has devised a brilliant method to p ...
- USACO翻译:USACO 2013 JAN三题(1)
USACO 2013 JAN 一.题目概览 中文题目名称 镜子 栅栏油漆 奶牛排队 英文题目名称 mirrors paint lineup 可执行文件名 mirrors paint lineup 输入 ...
- USACO翻译:USACO 2014 DEC Silver三题
USACO 2014 DEC SILVER 一.题目概览 中文题目名称 回程 马拉松 奶牛慢跑 英文题目名称 piggyback marathon cowjog 可执行文件名 piggyback ma ...
- USACO翻译:USACO 2012 FEB Silver三题
USACO 2012 FEB SILVER 一.题目概览 中文题目名称 矩形草地 奶牛IDs 搬家 英文题目名称 planting cowids relocate 可执行文件名 planting co ...
- USACO翻译:USACO 2014 FEB SILVER 三题
USACO 2014 FEB SILVER 一.题目概览 中文题目名称 自动打字 路障 神秘代码 英文题目名称 auto rblock scode 可执行文件名 auto rblock scode 输 ...
随机推荐
- Jmeter-【JSON Extractor】-响应结果中三级key取值
一.请求返回样式 二.取第三个option 三.查看结果
- 暴力模拟——cf988E
很简单的题,就是模拟一下 #include<bits/stdc++.h> using namespace std; #define ll long long ll n,a[],len; i ...
- delphi 获取文件图标
{根据文件的名字得到此文件在系统中对应大小的图标large=true(64*64) false(32*32)}procedure GetFileIcon(TypeName: Widestring; I ...
- 移动端多选插件-jquery
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- params拦截器
1. params拦截器首先给action中的相关参数赋值,如id 2. prepare拦截器执行prepare方法,prepare方法中会根据参数,如id,去调用业务逻辑,设置model对象 ...
- python virtual env 使用 jupyter ipython notebook,舒服了, 工作效率翻倍
话不多说,尊重原作者 知乎链接
- 数据结构C++版-图
一.概念及分类 二.图的存储结构 1.邻接矩阵 顶点: 弧: 边: 表达式语句: 2.邻接表 逆邻接表: 3.十字链表 4.邻接多重表 三.图的权值概念及遍历 权值: 图的遍历: 1.深度优先搜索 2 ...
- Devstack单节点环境实战配置
本文为minxihou的翻译文章,转载请注明出处Bob Hou: http://blog.csdn.net/minxihou JmilkFan:minxihou的技术博文方向是 算法&Open ...
- code rain???
Everybody loves the visual effect of the falling binary code known as ” Rain ” in The Matrix. In thi ...
- jquery操作html元素之(添加元素)
添加新的 HTML 内容 我们将学习用于添加新内容的四个 jQuery 方法: append() - 在被选元素的结尾插入内容 prepend() - 在被选元素的开头插入内容 after() - 在 ...