CodeForces - 101B Buses (二分+DP)

题目链接:http://codeforces.com/problemset/problem/101/B

#include<bits/stdc++.h>
using namespace std;

#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long

#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt

#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define fi first
#define se second
#define mp(x,y) make_pair(x,y)

const int  maxn =2e5+5;
const int mod=1e9+7;
const int ub=1e6;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
/*
题目大意:给定n+1个点在直线上,
家在0处,学校在n处,一个bus 的路线
是s和t,指定在s到t-1的站可以到t处,
问到学校有多少种方法。

很明显的dp状态,
问题是n开到了九次方,用普通的前缀和查询肯定不行数组开不下,
那么考虑对线段进行操作,动态维护一个前缀和,
对于当前位置需要累加的权值,需要用二分找位置来计算,
详情见代码,,,注意while循环里面程序的位置。
先更新当前数值,再累计权值,最后更新前缀和数组。

、时间复杂度:O(mlogm),wa了一次。
这题告诉我有些容器迭代器不支持相减运算。
*/

int n,m,x,y;
int st[maxn],cnt=0;

struct qy{
    int x,y;
    bool operator<(const qy& t) const{
        return y<t.y;
    }
}q[maxn];
ll dp[maxn],sum[maxn];
int main(){
    scanf("%d%d",&n,&m);
    st[cnt++]=0;
    for(int i=0;i<m;i++){
        scanf("%d%d",&x,&y);
        q[i]=qy{x,y};
    }
    sort(q,q+m);
    sum[0]=1,cnt=0;
    int nw=0,i=0;
    ll add=0;///初始化数据

    while(i<m){
        if(nw!=q[i].y){
            st[cnt++]=nw;
            nw=q[i].y;
        }
        add=0;
        while(i<m&&q[i].y==nw){
            int pos=lower_bound(st,st+cnt,q[i].x)-st;
            if(pos>0) add=(add+(sum[cnt-1]-sum[pos-1]+mod)%mod)%mod;
            else add=(add+sum[cnt-1])%mod;
            i++;
        }
        sum[cnt]=(sum[cnt-1]+add)%mod;
    }
    if(nw==n) printf("%lld\n",(mod+sum[cnt]-sum[cnt-1])%mod);
    else puts("0");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37451344/article/details/83930539