Sandglass(数学推导)

问题 I: Sandglass

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

分析:数学公式题,比赛的时候其实已经做出来了,该求的都求了,就是没搞清楚结果最后让求什么,本以为只要处于上方的球一定是A,很可惜。其实就是一个简单的数学题,始终维护一下,对于当前时刻t的答案,让他在t能取到的最大值与最小值之间。超了就按边界处理。可以看成是一个函数,若答案y比maxn大那就取maxn,如果y比minn小,就取minn,处于中间的可以线性计算。

所以维护一下翻转时刻的,那个函数就ok了,还有翻转时刻一定是递增着给的,这里读题也没读出来。。。。

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
#define gcd(a,b) __gcd(a,b)
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );

ll x,p=1,q,k;
ll r[MAX],flag=-1;
struct node
{
    ll a,t;
}e[MAX];

int main()
{
    scanf("%lld",&x);
    scanf("%lld",&k);
    for(int i=1;i<=k;i++)
        scanf("%lld",&r[i]);

    scanf("%lld",&q);
    for(int i=1;i<=q;i++)
        scanf("%lld%lld",&e[i].t,&e[i].a);

    ll maxn=x,minn=0;
    ll temp=0;

    for(int i=1;i<=q;i++)
    {
        while(p<=k && r[p]<=e[i].t)
        {
            ll tt=(r[p]-r[p-1])*flag;
            flag*=-1;
            p++;

            temp+=tt;
            maxn+=tt;
            minn+=tt;

            maxn=min(maxn,x);
            maxn=max(maxn,(ll)0);

            minn=min(minn,x);
            minn=max(minn,(ll)0);
        }

        ll ans=e[i].a+temp;

        if(ans>maxn)
            ans=maxn;
        if(ans<minn)
            ans=minn;

        ans+=(e[i].t-r[p-1])*flag;

        ans=min(ans,x);
        ans=max(ans,(ll)0);
        printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ToBeYours/article/details/81280222