upc 6604: Sandglass (沙漏定时翻转询问沙量-思维!)

6604: Sandglass

时间限制: 1 Sec  内存限制: 128 MB
提交: 223  解决: 33
[提交] [状态] [讨论版] [命题人: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

【小结】

真特么讨厌这题,因为我菜。

【题意】

有个沙漏,有AB两端,里面有总沙量X,初始时A在上B在下。每一秒会从上向下流一个单位的沙子。

然后规定了k个时间点,每个时间点瞬间把沙漏AB颠倒。

然后q次询问,每次询问输入两个数:t,a,表示询问若初始0秒A端有沙量a,那么 t 秒的A端有多少沙量?

【分析】

只考虑A端,当A端在上时,沙量递减,A端在下时,沙量递增。

A端的沙量最少为0,最多为X,受这个限制,沙子流完时,若没有翻转,则停留。

设一个变量maxsand初始为X模拟过程。

设一个变量minsand初始为0模拟过程。

那么当初值为a时,每个时刻的沙量值会在minsand~maxsand之间

设一个变量val初始为0,模拟过程,但不受限制,也就是可以为负,可以>X。

对于输入的t,a,求出<=t时刻的那个翻转时的val值,若超出minsand~maxsand,则回归一下。

再把val加上a,若超出0~X,则回归一下。

即为答案。。。

【代码】

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX=2e5+5;
const int INF=0x3f3f3f3f;
template<class T>bool gmax(T &a,T b){return a<b?a=b,1:0;}
template<class T>bool gmin(T &a,T b){return a>b?a=b,1:0;}

ll n,q,t,a,r[MAX],X;
int main()
{
    scanf("%lld%lld",&X,&n);
    for(int i=1;i<=n;i++)scanf("%lld",&r[i]);
    scanf("%lld",&q);
    ll minsand=0,maxsand=X,val=0,k=1,sign=-1;
    while(q--)
    {
        scanf("%lld%lld",&t,&a);
        while(k<=n&&r[k]<=t)
        {
            ll v=sign*(r[k]-r[k-1]);
            minsand+=v;
            maxsand+=v;
            val+=v;
            gmax(minsand,0ll);
            gmin(minsand,(ll)X);
            gmax(maxsand,0ll);
            gmin(maxsand,(ll)X);
            sign*=-1;
            k++;
        }
        ll now=val+a;
        gmax(now,minsand);
        gmin(now,maxsand);
        now+=sign*(t-r[k-1]);
        gmax(now,0ll);
        gmin(now,(ll)X);
        printf("%lld\n",now);
    }
}

猜你喜欢

转载自blog.csdn.net/winter2121/article/details/81276106
UPC