题目大意:
输入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=0,ed=0; m[key]=0; 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=0; 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=0; 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-1-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 0; }