USACO 2013 January Silver Painting the Fenceoj23695

mac2022-06-30  30

题目大意:

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

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

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

Sample Input

6 22 R6 L1 R8 L1 R2 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=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; } View Code

若是输出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=0,st=INF,ed=0; m[key]=0; int a,cnt=0; 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=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; } /// 23695 View Code

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

        若指令为      则会出现  

                                —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

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

转载于:https://www.cnblogs.com/zquzjx/p/8891847.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)