UPC 6604 Sandglass

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Seeyouer/article/details/81295786

题目描述
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

180
3
60 120 180
3
30 90
61 1
180 180

样例输出

60
1
120

为什么会一直超时呢?
请问你有没有看的ti是递增的(对自己无语。。),这样时间复杂度就是O(k+x),超时?不存在的。。。。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll r[101010],t,a;//loxz[101010],upxz[101010],lo[101010];
int main()
{
    int x,k,q,i=1;
    scanf("%d%d",&x,&k);
    int flag=-1,y=-1;
    ll upxz=x,loxz=0,lo=0;
    r[0]=0;
    for(int i=1;i<=k;i++){
        scanf("%lld",&r[i]);
    }
    scanf("%d",&q);
    while(q--){
        scanf("%d%d",&t,&a);
        int flag=0;
        int ans;
        ll sum;
        for(;i<=k&&r[i]<=t;i++){
            sum=y*(r[i]-r[i-1]);
            upxz+=sum;
            loxz+=sum;
            lo+=sum;
            if(upxz>x) upxz=x;
            else if(upxz<0) upxz=0;
            if(loxz>x) loxz=x;
            else if(loxz<0) loxz=0;
            y*=-1;
        }
        ans=lo+a;
        if(ans>upxz) ans=upxz;
        else if(ans<loxz) ans=loxz;

        ans+=y*(t-r[i-1]);
        if(ans<0) ans=0;
        else if(ans>x) ans=x;


        printf("%d\n",ans);
        //else printf("%d\n",lo);
    }
}

猜你喜欢

转载自blog.csdn.net/Seeyouer/article/details/81295786
UPC