洛谷——P2205 [USACO13JAN]画栅栏Painting the Fence

题目描述

Farmer John has devised a brilliant method to paint the long fence next to his barn (think of the fence as a one-dimensional number line). He simply attaches a paint brush to his favorite cow Bessie, and then retires to drink a cold glass of water as Bessie walks back and forth across the fence, applying paint to any segment of the fence that she walks past.

Bessie starts at position 0 on the fence and follows a sequence of N moves (1 <= N <= 100,000). Example moves might be "10 L", meaning Bessie moves 10 units to the left, or "15 R", meaning Bessie moves 15 units to the right. Given a list of all of Bessie's moves, FJ would like to know what area of the fence gets painted with at least K coats of paint. Bessie will move at most 1,000,000,000 units away from the origin during her walk.

Farmer John 想出了一个给牛棚旁的长围墙涂色的好方法。(为了简单起见,我们把围墙看做一维的数轴,每一个单位长度代表一块栅栏)他只是简单的把刷子蘸满颜料,系在他最喜欢的奶牛Bessie上,然后让Bessie来回地经过围墙,自己则在一旁喝一杯冰镇的凉水。(……-_-|||) Bessie 经过的所有围墙都会被涂上一层颜料。Bessie从围墙上的位置0出发,并将会进行N次移动(1 <= N <= 100,000)。比如说,“10 L”的意思就是Bessie向左移动了10个单位。再比如说“15 R”的意思就是Bessie向右移动了15个单位。给出一系列Bessie移动的清单。FJ 想知道有多少块栅栏涂上了至少K层涂料。注意:Bessie最多会移动到离原点1,000,000,000单位远的地方。

输入输出格式

输入格式:

 

  • 第1行: 两个整数: N K

  • 第2...N+1 行: 每一行都描述了Bessie的一次移动。 (比如说 “15 L")

 

输出格式:

 

  • 一个整数:被至少涂上K层涂料的栅栏数

(注意:输出的最后一定要输出换行符!否则会WA)

 

输入输出样例

输入样例#1:
6 2 
2 R 
6 L 
1 R 
8 L 
1 R 
2 R 
输出样例#1:
6

说明

PS1:来源:usaco jan silver P01 想看原题的请戳http://www.usaco.org/index.php?page=viewproblem2&cpid=226)

PS2:测试数据也可以在在http://www.usaco.org/index.php?page=jan13problems上下载,还可以看到题解(不过是英文的:-D)

PS3:如果有翻译的问题或题目的不理解,可以在问答后面留言的说。

 

 

类似于线段覆盖的一道题目,我们看到数据范围以后发现我们如果要for循环累计,每一个位置染色的层数的话,显然是不现实的,因为n的个数为10^5,每一次覆盖的位置还可能扩展到10^9.如果我们要枚举的话,10^14,甭想,准T,保你T成狗。

所以我们要对这个进行一下优化,我们对于一个线段为一个整体,然后我们将线段的信息储存下来,对于l我们用1记录,r用0记录,当遇到左节点的时候,我们的s++,即为在这段区间里面的线段数++,遇到右节点的时候线段数--,当s>=k的时候,ans++。然后ans累加的为线段的长度

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 100010
using namespace std;
char ch;
int n,k,x,l,s,ans; 
struct A
{
    int x,l;
}a[N<<1];
int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int cmp(A a,A b){return a.x<b.x;}
int main()
{
    n=read(),k=read(),x=0;
    for(int i=1;i<=n;i++)
    {
        l=read(),cin>>ch;
        a[i*2-1].x=x;
        if(ch=='L') x-=l;
        else x+=l;
        a[i*2].x=x;
        if(a[i*2-1].x<a[i*2].x) 
         a[i*2-1].l=1,a[i*2].l=0;
        else a[i*2].l=1,a[i*2-1].l=0;
    }
    sort(a+1,a+1+2*n,cmp);
    for(int i=1;i<=2*n;i++)
    {
        if(s>=k) ans+=a[i].x-a[i-1].x;
        if(a[i].l==1) s++;else s--;
    }
    printf("%d\n",ans);
    return 0;
}

 

转载于:https://www.cnblogs.com/z360/p/7695010.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值