问题 I: Sandglass
时间限制: 1 Sec 内存限制: 128 MB提交: 407 解决: 105
题目描述
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.
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,B两面,开始时,A面在上。每秒钟会往下流一单位的沙子,直到上边的沙子流光。每隔一段时间,沙漏被翻转一次。给出沙子总量和初始时A面沙子的量,和反转的时刻,求指定时间t后,A面沙子的量。对于每组数据,反转的时间是固定的,每次询问给出初始时A面沙子的量和时间t。
考虑两个极端情况:初始时,所有的沙子都在A面或者B面,求出两个边界情况,设他们的答案分别为UP(t)和LOW(t)。设t秒后,A面沙子的量为F(t)。不考虑沙子漏完,求出F_prime(t)=-r1+r2-r3+r4-r5+...±r(k-1)∓r(k)±rr, 其中,r(k)是最大的小于t的数,rr的绝对值为t-r(k)。
答案为F(t)=将F_prime(t)限制在区间[UP(t), LOW(t)]后的结果。
If F_prime(t) > UP(t), F(t)=UP(t); If F_prime(t) < LOW(t), F(t)=LOW(t); else F(t)=F_prime(t).
借助图像,可以说明其正确性。
下面是1371767389的博客。