[AtCoder][ARC082]Sandglass 题解

Sandglass

时间限制: 1 Sec 内存限制: 128 MB 
原题链接 https://arc082.contest.atcoder.jp/tasks/arc082_d

题目描述

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 

60 120 180 

30 90 
61 1 
180 180

样例输出

60 

120

题解

我的做法是进行带优化的模拟,因为题目输入是确保后一个时间一定大于前一个时间的,所以对于翻转的模拟总共只需要按顺序进行一次便可,最后使用数学结论根据时间和初始质量推出答案。 
需要特别注意的是,这道题会卡输入输出,如果不使用scanf/printf的话,必须要关闭同步锁且不能使用endl,不然会TLE。(可能是华东OJ的原因)

心疼那将近一半被输入输出卡掉的提交QAQ

代码

42 ms/1664 KB(AtCoder) 
80 ms/2080 KB(华东 OJ)

#include <iostream>
#include <algorithm>

using namespace std;
int x, k, q, t, a, low, up, add, delta, now, ans, r[100007];
bool flag;

int cal(int tt, int lower = 0, int upper = x) {
    if (tt < lower) return lower;
    if (tt > upper) return upper;
    return tt;
}

int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);

    cin >> x >> k;
    for (int i = 1; i <= k; i++) cin >> r[i];
    cin >> q;

    low = x, now = 1;
    while (q--) {
        cin >> t >> a;
        while (t >= r[now] && now <= k) {
            delta = (flag ? 1 : -1) * (r[now] - r[now - 1]), add += delta;
            up = cal(delta + up), low = cal(delta + low);
            flag = !flag, now++;
        }
        ans = cal((flag ? 1 : -1) * (t - r[now - 1]) + cal(a + add, up, low));
        cout << ans << ‘\n‘;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/xfl03/p/9385769.html

时间: 2024-11-09 11:32:10

[AtCoder][ARC082]Sandglass 题解的相关文章

【AtCoder】ARC100 题解

C - Linear Approximation 找出\(A_i - i\)的中位数作为\(b\)即可 题解 #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #define enter putchar('\n') #define space putchar(' ') #define fi first #define se second #define

AtCoder AGC038 C-LCMs 题解

题目链接:https://agc038.contest.atcoder.jp/tasks/agc038_c?lang=en 题意:给定一个数组,求这个数组中所有数对的LCM之和. 分析:网上看到了很多反演的解法,但是本题也可以通过埃氏筛在\(O(nlnlnn)\)的复杂度下解决.大致做法就是根据\(a_i \leq 1000000\),我们得到\(gcd(a_i, a_j) \leq 1000000\),于是可以通过枚举gcd来解决本题.实现参考代码,埃氏筛的思路就是求出gcd的值在\([1,

【AtCoder】ARC082 F - Sandglass

[链接]F - Sandglass [题意]给定沙漏A和B,分别装着a和X-a的沙子,开始时A在上B在下,每秒漏1,漏完不再漏.给定n,有n个时刻ai沙漏倒转.给定m个询问,每次询问给定初值a和时刻t,求A中沙子量. [算法]数学(函数) [题解] 先不考虑时刻,令ft(a)表示沙子初值a时,当前A中的沙子数.(x轴是初值a,y轴是沙子数num) 时刻为0时,显然是一条从0出发斜率为1的直线. 若A在上,则每过1s,整段函数都下移一个单位,碰到y=0则变成平的. 若A在下,则每过1s,整段函数都

AtCoder Beginner Contest 115 题解

题目链接:https://abc115.contest.atcoder.jp/ A Christmas Eve Eve Eve 题目: Time limit : 2sec / Memory limit : 1024MB Score : 100 points Problem Statement In some other world, today is December D-th. Write a program that prints Christmas if D=25, Christmas E

AtCoder Beginner Contest 154 题解

人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We have A balls with the string S written on each of them and B balls with the string T written on each of them. From these balls, Takahashi chooses one

【ATcoder】AtCoder Beginner Contest 161 题解

题目链接:AtCoder Beginner Contest 161 原版题解链接:传送门 A - ABC Swap 这题太水,直接模拟即可. 1 #include <iostream> 2 using namespace std; 3 int main() { 4 int a, b, c; 5 cin >> a >> b >> c; 6 swap(a, b); 7 swap(a, c); 8 cout << a << " &

题解 [Atcoder ABC 161] A,B,C

题解 [Atcoder ABC 161] A,B,C A: 水题,按题意模拟即可. code: #include<bits/stdc++.h> #define ft(i,l,r) for(register int i=l;i<=r;i++) #define fd(i,r,l) for(register int i=r;i>=l;i--) using namespace std; int a,b,c; int main() { cin>>a>>b>>

【AtCoder】ARC096 C-F题解

听说日本题思维都很棒,去涨涨智商qwq C - Half and Half 题解 枚举买多少个AB披萨也行 但是关于买x个AB披萨最后的总花费是个单峰函数,可以三分 这题有点像六省联考2017D1T1送分题期末考试 代码 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #define MAXN 100005 #define PLI pair<lo

【AtCoder】AGC023 A-F题解

可以说是第一场AGC了,做了三道题之后还有30min,杠了一下D题发现杠不出来,三题滚粗了 rating起步1300+,感觉还是很菜... 只有三题水平显然以后还会疯狂--啊(CF的惨痛经历) 改题的感觉似乎还不错因为思维都非常的妙(我根本想不到) A - Zero-Sum Ranges 开场娱乐大家的小水题,区间和为0的情况存在于sum[R] == sum[L - 1],只要记录一下某一个值的sum出现了多少次就行,懒得离散化直接用map就OK啊 代码 #include <iostream>