zoukankan      html  css  js  c++  java
  • 【题解】售票系统--一道毒瘤题

    • 售票系统

    输入文件:railway.in 输出文件:railway.out
    时间限制:1 s 内存限制:128 MB

    【问题描述】

    某次列车途经C个城市,城市编号依次为1到C,列车上共有S个座位,铁路局规定售出的车票只能是坐票, 即车上所有的旅客都有座。售票系统是由计算机执行的,每一个售票申请包含三个参数,分别用O、D、N表示,O为起始站,D为目的地站,N为车票张数。售票 系统对该售票申请作出受理或不受理的决定,只有在从O到D的区段内作者注:这句话是大毒瘤啊!!!)列车上都有N个或N个以上的空座位时该售票申请才被受理。请你写一个程序,实现这个自动 售票系统。

    【输入格式】

    第一行包含三个用空格隔开的整数C、S和R,其中1≤C≤60000, l≤S≤60000,1≤R≤60000。C为城市个数,S为列车上的座位数,R为所有售票申请总数。接下来的R行每行为一个售票申请,用三个由空格隔开的整数O,D和N表示,O为起始站,D 为目的地站,N为车票张数,其中1≤D≤C,1≤O≤C,所有的售票申请按申请的时间从早到晚给出。

    【输出格式】

    输出共有R行,每行输出一个“YES”或“NO”,表示当前的售票申请被受理或不被受理。

    【输入输出样例】

    输入:

    4 6 4

    1 4 2

    1 3 2

    2 4 3

    1 2 3

    输出:

    YES

    YES

    NO

    NO

    • 分析

    题目很容易,就是一个线段树维护一个动态区间最大值,样例基本上能一遍过,然后你就交上去就只有......33分。这就是这道题的毒瘤所在,目的地站是不算座位的!!!但是题目描述中却用了区段这一个模糊的概念实在是气人,我在WA了之后还是自己在纸上模拟过程才发现这毒瘤之处。

    • 代码

    如果不会区间动态查询最大值就看一下吧,其实只要熟练掌握线段树的思想应该是挺好写的:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    const int maxn=60010;
    int a[maxn],add[maxn<<2],maxx[maxn<<2];
    int c,s,r;
    int n;
    int L,R;
    int MAX=0;
    void pushup(int now)
    {
    	maxx[now]=max(maxx[now<<1],maxx[now<<1|1]);
    	return ;
    }
    void build(int now,int l,int r){
    	if(l==r){
    		maxx[now]=0;
    		return;
    	}
    	int mid=(l+r)>>1;
    	build(now<<1,l,mid);
    	build(now<<1|1,mid+1,r);
    	return ;
    }
    void pushdown(int now,int ln,int rn)
    {
    	if(add[now])
    	{
    		add[now<<1]+=add[now];
    		maxx[now<<1]+=add[now];	
    		add[now<<1|1]+=add[now];
    		maxx[now<<1|1]+=add[now];
    		add[now]=0;
    
    	}
    }
    void update(int now,int l,int r)
    {
    	if(L<=l&&r<=R){
    	    add[now]+=n;
    		maxx[now]+=n;
    		return;
    	}
    	int mid=(l+r)>>1;
    	pushdown(now,mid-l+1,r-mid);
    	if(L<=mid)update(now<<1,l,mid);
    	if(mid<R)update(now<<1|1,mid+1,r);
    	pushup(now);
    	return ; 
    }
    int query(int now,int l,int r)
    {
    	if(L<=l&&r<=R){
    		return maxx[now];
    	}
    	int mid=(l+r)>>1;
    	pushdown(now,mid-l+1,r-mid);
    	if(L<=mid)MAX=max(MAX,query(now<<1,l,mid));
    	if(mid<R)MAX=max(MAX,query(now<<1|1,mid+1,r));
    	return MAX;
    }
    int main()
    {
    	freopen("railway.in","r",stdin);
        freopen("railway.out","w",stdout);
    	cin>>c>>s>>r;
    	for(register int i=1;i<=r;i++)
    	{
    		MAX=0;
    	    scanf("%d %d %d",&L,&R,&n);
    	    R=R-1;   //注意
    	    update(1,1,c);
    	    if(query(1,1,c)>s){
    	    	puts("NO");
    	    	n=-n;
    	    	update(1,1,c);
    	    }
    	    else {
    	    	puts("YES");
    	    }
    	}
    	fclose(stdin);
    	fclose(stdout);
    	return 0;
    }
    
  • 相关阅读:
    安卓5.1/7.1/8.1+wifi有叉问题解决
    历史市盈率查询
    IE9 Windows7 x64
    Error while loading shared libraries: libpq.so.5: cannot open shared object file: No such file or directory
    ubuntu kylin 13.10 无法安装ia32-libs解决方案
    ajax报错问题的解决
    关于 java swing 使用按钮关闭窗口
    关于在filter中获取WebApplicationContext的实践
    spring security
    毕设项目,系统搭建笔记文档
  • 原文地址:https://www.cnblogs.com/Rye-Catcher/p/8467082.html
Copyright © 2011-2022 走看看