AtCoder Regular Contest 082 F-UPC 6604 Sandglass

题目描述

We have a sandglass consisting of two bulbs, bulb A and bulb B. These bulbs contain some amount of sand. When we put the sandglass, either bulb A or B lies on top of the other and becomes the upper bulb. The other bulb becomes the lower bulb.
The sand drops from the upper bulb to the lower bulb at a rate of 1 gram per second. When the upper bulb no longer contains any sand, nothing happens.
Initially at time 0, bulb A is the upper bulb and contains a grams of sand; bulb B contains X−a grams of sand (for a total of X grams).
We will turn over the sandglass at time r1,r2,..,rK. Assume that this is an instantaneous action and takes no time. Here, time t refer to the time t seconds after time 0.
You are given Q queries. Each query is in the form of (ti,ai). For each query, assume that a=ai and find the amount of sand that would be contained in bulb A at time ti.

Constraints
1≤X≤109
1≤K≤105
1≤r1<r2<..<rK≤109
1≤Q≤105
0≤t1<t2<..<tQ≤109
0≤ai≤X(1≤i≤Q)
All input values are integers.

样例输入

180
3
60 120 180
3
30 90
61 1
180 180

样例输出

60
1
120

思路:假设开始初始值为0那么在任意时间点  其他的初始值都是大于等于以0为初始点的。

同理,初始值为X,其他初始值都是小于等于他的。

直接记录从开始到查询时间点的变化总值加上每次查询的初始值,然后与0,X为初始值到此时间点的值比对,如果超过范围,说明已经0和X跑的线路一样了,直接赋值,然后计算。(黑猫的思路,比赛的时候想不到,感觉有那么点道理,还需再想想)

 

代码:

 

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long LL;
const int maxn = 1e5+5;
int x;
int n;
int r[maxn];
int main()
{
    scanf("%d%d",&x,&n);
    r[0]=0;
    for (int i=1;i<=n;i++) scanf("%d",r+i);
    int L = 0;
    int R = x;
    int tmp = 0;
    int pos = 1;
    int Q;
    scanf("%d",&Q);
    int t,a;
    while(Q--)
    {
        scanf("%d%d",&t,&a);
        while(pos<=n && r[pos]<=t)
        {
            tmp+=(r[pos]-r[pos-1])*(pos&1?-1:1);
            L+=(r[pos]-r[pos-1])*(pos&1?-1:1);
            R+=(r[pos]-r[pos-1])*(pos&1?-1:1);
            if(L<0) L=0;
            if(L>x) L=x;
            if(R<0) R=0;
            if(R>x) R=x;
            pos++;
        }
        //        printf("%d %d %d ",pos,L,R);
        int ans = a+tmp;
        if(ans<L)   ans = L;
        if(ans>R)   ans = R;
        ans += (t-r[pos-1])*((pos)&1?-1:1);
        if(ans<0)   ans = 0;
        if(ans>x)   ans = x;
        printf("%d\n",ans);
    }
    return 0;
}


(照搬 :) )

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值