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 输 ...
随机推荐
- JAVA C 数据类型对应
{ Java—C和操作系统数据类型的对应表 Java Type C Type Native Representation boolean int 32-bit integer (customizabl ...
- c++ GetAsyncState() 函数
函数原型 SHORT GetAsyncKeyState(int vKey); 例:若判断 回车键 if(GetAsyncKeyState(VK_RETURN)&0x8000) ( return ...
- bzoj1010题解
[解题思路] 设s[i]=i+∑c[j](j∈[1,n]∩N) 易得转移方程f[i]=min{f[j]+(s[i]-s[j]-L-1)2},朴素算法复杂度O(n2). 考虑斜率优化:记T[i]=s[i ...
- Python查看对象属性的方法
帮助https://docs.python.org/2/library/functions.html dir() 函数 D:\pythontest>python Python (v3. , :: ...
- NX二次开发-UFUN拉伸函数UF_MODL_create_extruded
NX9+VS2012 //NX二次开发中常用拉伸函数为UF_MODL_create_extruded2,但是此函数不能拉伸片体, //想要拉伸片体用函数UF_MODL_create_extruded. ...
- [JZOJ 5860] 荒诞
思路: 头皮发麻的操作... 理解一下题意会发现:排名为\(i\)的前缀正好是第\(i\)个前缀. 所以问题就变成了求\(1->len\)的平方和,注意取模即可. #include <bi ...
- (转)Java安全通信:HTTPS与SSL
转:http://www.cnblogs.com/devinzhang/archive/2012/02/28/2371631.html 1. HTTPS概念 1)简介 HTTPS(全称:Hyperte ...
- Qt5编译使用QFtp
使用 QNetworkAccessManager 可以实现 Ftp 的上传/下载功能(参考:Qt之FTP上传/下载),但有些原本 QFtp 有的功能 QNetworkAccessManager 却没有 ...
- json、pickle和base64
json.dumps() 用于将dict类型的数据转成str,因为如果直接将dict类型的数据写入json文件中会发生报错,因此在将数据写入时需要用到该函数. json.dump() 用于将dict类 ...
- __typeof与typeof
其实之前在stackoverflow就看过一篇讲的比较详细的, https://stackoverflow.com/questions/14877415/difference-between-type ...