中国石油大学OJ 第六场个人训练赛 Sandglass

6604: Sandglass

时间限制: 1 Sec  内存限制: 128 MB
提交: 347  解决: 79
[提交] [状态] [讨论版] [命题人:admin]

题目描述

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

来源/分类

ABC072&ARC082 

题意:给你一个沙漏,总容量是x,上面是a,下面是b,在k个时间点会将沙漏翻转,q次询问,每次在t的时间沙漏a初始为ai,最终沙漏a的沙子量。

题解:可以直接进行模拟处理,因为给定的时间一定是递增的,所以可以保证复杂度是k+q,每次求出两个翻转点之间沙子的变化量,根据变化量来限定能达到的最大上界,和能达到的最小下界,最终结果一定在这个上下界内。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+7;

int x,q,r[maxn],k;
int cal(int tt,int lower=0,int upper=x)
{
    if(tt<lower) return lower;
    if(tt>upper) return upper;
    return tt;
}

int main()
{
    scanf("%d%d",&x,&k);

    for(int i=1;i<=k;i++)
        scanf("%d",&r[i]);

    int q;
    int delta=0,flag=0;
    int now=1,low=x,up=0,a,add=0,ans,t;
    scanf("%d",&q);
    while(q--)
    {
        scanf("%d%d",&t,&a);
        while(t>=r[now] && now<=k)
        {
            delta=(flag?1:-1)*(r[now]-r[now-1]),add+=delta;
            low=cal(low+delta),up=cal(up+delta);
            flag=!flag;now++;
        }
        ans=cal((flag?1:-1)*(t-r[now-1])+cal(a+add,up,low));
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sudu6666/article/details/81280731