离散-洛谷P2205 [USACO13JAN]画栅栏Painting the Fence

https://daniu.luogu.org/problem/show?pid=2205
本来要抄题解的 ,结果发现搜不到;
….
其实就是一个离散化;
假如现在的点是now;
要向右移动x个单位;
我们直接在now的地方+1;
在now+x的地方-1;
就好了;
然后排序判重乱搞;
要注意它是一个单位长度是一个栏栅;
所以2个点组成一个栏栅;

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<map>
#define Ll long long
using namespace std;
map<int,int>F;
int q[300000],top;
int n,m,now,x,ans;
char c;
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&x);
        cin>>c;
        if(c=='R'){
            q[++top]=now;
            F[q[top]]++;
            q[++top]=now+x;
            F[q[top]]--;
            now+=x;
        }else{
            q[++top]=now-x;
            F[q[top]]++;
            q[++top]=now;
            F[q[top]]--;
            now-=x;
        }
    }
    sort(q+1,q+top+1);
    now=F[q[1]];
    for(int i=2;i<=top;i++)
        if(q[i]!=q[i-1]){
            if(now>=m)ans+=q[i]-q[i-1];
            now+=F[q[i]];
        }
    printf("%d",ans);
}
posted @ 2017-04-10 10:32  largecube233  阅读(143)  评论(0编辑  收藏  举报