i.sandglass

Posted lwsh123k

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了i.sandglass相关的知识,希望对你有一定的参考价值。


i.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


1.维护0和x这两个最小值和最大值,对于Now不维护(可以看做增减量),所给的设a = 所给初始值+Now,再有,a = max(0,a) a = min(x,a); 即代表a不能小于0,大于所给的沙子总和x。
2.题目输入是确保后一个时间一定大于前一个时间的,那么对于翻转的模拟总共只需要按顺序进行一次便可。

#include<bits/stdc++.h>
using namespace std;
const int m = 1e5 + 5;
pair<int,int> t[m];   
int r[m];    //记录倒置时的时间 
typedef long long ll;
int main()
{
    int x,k;
    cin>>x>>k;
    for(int i = 1; i <= k; i++)
    cin>>r[i];    r[0] = 0;
    int q;
    cin>>q;
    for(int i = 0; i < q; i++)
    cin>>t[i].first>>t[i].second;
    ll Max = x, Min = 0, Now = 0;  //now是不加维护的 
    int sng = -1,ki = 1;
    for(int i = 0; i < q; i++)
    {
        while(ki<=k && r[ki]<=t[i].first)
        {
            ll dat = (r[ki] - r[ki-1]) * sng;
            Min += dat, Max += dat, Now += dat;
            Min = max((ll)0,Min),    Min = min((ll)x,Min);
            Max = max((ll)0,Max),    Max = min((ll)x,Max);
            ki++;
            sng *= -1;
        }
        ll remain = (t[i].first - r[ki-1]) * sng;
        ll ans = t[i].second + Now;
        if(ans > Max)    ans = Max;
        if(ans < Min)    ans = Min;
        ans += remain;
        ans = max((ll)0,ans);
        ans = min(ans,(ll)x);
        cout<<ans<<endl;
    }
    return 0;
}

 























以上是关于i.sandglass的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器

VSCode自定义代码片段——声明函数