Painting the Fence(离散化)

Painting the Fence(离散化)

Main Title

题目描述
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.

输入
* Line 1: Two space-separated integers: N and K.
* Lines 2..1+N: Each line describes one of Bessie’s N moves (e.g., “15L”).
输出
* Line 1: The total area covered by at least K coats of paint.
样例输入 Copy
6 2
2 R
6 L
1 R
8 L
1 R
2 R
样例输出 Copy
6
提示
Bessie starts at position 0 and moves 2 units to the right, then 6 to the left, 1 to the right, 8 to the left, and finally 3 to the right.  FJ wants to know the area covered by at least 2 coats of paint.6 units of area are covered by at least 2 coats of paint.  This includes the intervals [-11,-8], [-4,-3], and [0,2].

思路:具体来说就是,每输入一条线,插入起点和终点。小的点,一个值为1,一个为-1。再按照坐标排序。

具体看代码和图片。

 

#pragma GCC optimize(3 , "Ofast" , "inline")

#include <bits/stdc++.h>

#define rep(i , a , b) for(register int i=(a);i<=(b);i++)
#define rop(i , a , b) for(register ll i=(a);i<(b);i++)
#define per(i , a , b) for(register ll i=(a);i>=(b);i--)
#define por(i , a , b) for(register ll i=(a);i>(b);i--)

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll , ll> pi;
template<class T>
inline void read (T &x) {
    x = 0;
    int sign = 1;
    char c = getchar ();
    while (c < '0' || c > '9') {
        if ( c == '-' ) sign = - 1;
        c = getchar ();
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar ();
    }
    x = x * sign;
}
const int maxn = 2e5 + 10;
const int inf = int (1e9);
const ll INF = ll(1e18);

struct node{
    int x;
    int y;
    node(){}
    node(int _x,int _y) : x(_x), y(_y){}
}e[maxn];


bool cmp(node A,node B) {
    return A.x<B.x;
}
int cnt = 0;
int n,k,now;

int main () {
    read(n);read(k);
    rep(i,1,n) {
        int p;
        char dir;
        scanf("%d %c",&p,&dir);
        if(dir=='R') {
            e[++cnt]= node(now,1);
            e[++cnt]= node(now+=p,-1);
        }
        else {
            e[++cnt]= node(now,-1);
            e[++cnt]= node(now-=p,1);
        }
    }
    sort(e+1,e+1+cnt,cmp);
//    rep(i,1,cnt) {
//        cout<<e[i].x<<' '<<e[i].y<<endl;
//    }
    now = e[1].y;
    ll ans = 0;
    rep(i,2,cnt) {
        if(now>=k) ans=ans+e[i].x-e[i-1].x;
        now+=e[i].y;
    }
    cout<<ans<<endl;
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值