Atocder ARC082 F-Sandglass 【思维题】*
Atocder ARC082 F-Sandglass
Problem Statement
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

All input values are integers.
Input
The input is given from Standard Input in the following format:

Output
For each query, print the answer in its own line.
Sample Input 1
180
3
60 120 180
3
30 90
61 1
180 180
Sample Output 1
60
1
120
In the first query, 30 out of the initial 90 grams of sand will drop from bulb A, resulting in 60 grams. In the second query, the initial 1 gram of sand will drop from bulb A, and nothing will happen for the next 59 seconds. Then, we will turn over the sandglass, and 1 second after this, bulb A contains 1 gram of sand at the time in question.
Sample Input 2
100
1
100000
4
0 100
90 100
100 100
101 100
Sample Output 2
100
10
0
0
In every query, the upper bulb initially contains 100 grams, and the question in time comes before we turn over the sandglass.
Sample Input 3
100
5
48 141 231 314 425
7
0 19
50 98
143 30
231 55
342 0
365 100
600 10
Sample Output 3
19
52
91
10
58
42
100
题目大意:有一个沙漏分成AB两面,里面共有X个点位的沙子,每一单位时间向下漏1个单位的沙子。
然后有n次操作,每次把沙漏翻转(不消耗时间)
询问当初始A有ai的沙子,B有X-ai的沙子,在t时刻A中有多少沙子
考场上平方暴力
然后想想正解,挺神奇的一道题
首先我们可以用ai=0和X各按照规则跑一遍,跑出每个操作的A中沙子的上下界限
然后我们考虑预处理偏量,不考虑规则,预处理每个时刻的偏量
然后我们对于起始的量ai,直接加上偏量,如果在0和X处理出的区间内就合法,否则变成最靠近的一个
考虑如果直接处理偏量和0或X的折线相交会发生什么
首先明确ai只有可能在0或X触及界限的时候才会相交
那么在相交点右侧一定会存在一个转折点,那么在这个转折点位置ai一定比0低或比X高,所以在这之后ai一定不可能再回到0和X之间
然后就很简单了,是可以做到线性,但我懒,就多挂了一个log
#include<bits/stdc++.h>
using namespace std;
#define N 100010
#define LL long long
int n,q;
LL X,r[N];
struct Node{LL a,t;}p[N];
LL l_line[N],r_line[N];
LL cnt[N];
int main(){
scanf("%lld%d",&X,&n);
r[0]=0;for(int i=1;i<=n;i++)scanf("%lld",&r[i]);
scanf("%d",&q);
for(int i=1;i<=q;i++)scanf("%lld%lld",&p[i].t,&p[i].a);
LL na=0,nb=X;
l_line[0]=0;r_line[0]=X;
for(int tmp=1;tmp<=n;tmp++){
if(tmp&1){
LL tip=min(na,r[tmp]-r[tmp-1]);
na-=tip;
nb+=tip;
}else{
LL tip=min(nb,r[tmp]-r[tmp-1]);
na+=tip;
nb-=tip;
}
l_line[tmp]=na;
}
na=X,nb=0;
for(int tmp=1;tmp<=n;tmp++){
if(tmp&1){
LL tip=min(na,r[tmp]-r[tmp-1]);
na-=tip;
nb+=tip;
}else{
LL tip=min(nb,r[tmp]-r[tmp-1]);
na+=tip;
nb-=tip;
}
r_line[tmp]=na;
}
for(int i=1;i<=n;i++)
if(i&1)cnt[i]=cnt[i-1]-(r[i]-r[i-1]);
else cnt[i]=cnt[i-1]+(r[i]-r[i-1]);
for(int i=1;i<=q;i++){
int ll=1,rr=n,res=0;
while(ll<=rr){
int mid=(ll+rr)>>1;
if(r[mid]<=p[i].t)res=mid,ll=mid+1;
else rr=mid-1;
}
LL pic=p[i].a+cnt[res];
if(l_line[res]>pic)pic=l_line[res];
if(r_line[res]<pic)pic=r_line[res];
if(res&1)pic+=min(X-pic,p[i].t-r[res]);
else pic-=min(pic,p[i].t-r[res]);
printf("%lld\n",pic);
}
return 0;
}
Atocder ARC082 F-Sandglass 【思维题】*的更多相关文章
- 【AtCoder】ARC082 F - Sandglass
[链接]F - Sandglass [题意]给定沙漏A和B,分别装着a和X-a的沙子,开始时A在上B在下,每秒漏1,漏完不再漏.给定n,有n个时刻ai沙漏倒转.给定m个询问,每次询问给定初值a和时刻t ...
- AtCoder Beginner Contest 188 F - +1-1x2 思维题
题目描述 给你两个数 \(x\),\(y\) 可以对 \(x\) 进行 \(+1,-1\) 或 \(\times 2\) 的操作 问最少操作多少次后变为 \(y\) \(x,y \leq 10^{18 ...
- UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)
UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...
- 思维题--code forces round# 551 div.2
思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...
- UVA.11384 Help is needed for Dexter (思维题)
UVA.11384 Help is needed for Dexter (思维题) 题意分析 同样水题一道,这回思路对了. 给出数字n,面对一个1,2,3,4--n的数字序列,你可以对他们的部分或者全 ...
- BZOJ 2734 洛谷 3226 [HNOI2012]集合选数【状压DP】【思维题】
[题解] 思维题,看了别人的博客才会写. 写出这样的矩阵: 1,3,9,... 2,6,18,... 4,12.36,... 8,24,72,... 我们要做的就是从矩阵中选出一些数字,但是不能选相邻 ...
- 【2019.7.15 NOIP模拟赛 T1】夹缝(mirror)(思维题)
思维题 此题应该是比较偏思维的. 假设一次反射后前进的距离是\(2^x(2y+1)\),则显然,它可以看做是前进距离为\(2^x\)的光线经过了\((2y+1)\)次反射,两者是等价的,甚至后者可能还 ...
- [Hdu-5155] Harry And Magic Box[思维题+容斥,计数Dp]
Online Judge:Hdu5155 Label:思维题+容斥,计数Dp 题面: 题目描述 给定一个大小为\(N*M\)的神奇盒子,里面每行每列都至少有一个钻石,问可行的排列方案数.由于答案较大, ...
- CF--思维练习-- CodeForces - 215C - Crosses(思维题)
ACM思维题训练集合 There is a board with a grid consisting of n rows and m columns, the rows are numbered fr ...
随机推荐
- ZooKeeper的API操作(二)(通俗易懂)
所需要6个jar包,都是解压zookeeper的tar包后里面的. zookeeper-3.4.10.jar jline-0.094.jar log4j-1.2.16.jar netty- ...
- jquery tmpl模板
之前用模板渲染都是用angular,无意间发现了jquery tmpl这种轻量级,其文档在这里 官方解释对该插件的说明:将匹配的第一个元素作为模板,render指定的数据,签名如下: .tmpl([d ...
- http协议报头详解
目录: 1. http协议简介 2. http报头举例 3. http报头详解 4. 几个字段的说明 5. 总结 6. 参考文章 1. http协议简介 HTTP是Hyper Text Transfe ...
- Android-----代码实现打开手机第三方应用APP
最近做一个项目,有一个需要启动第三方应用,和微信的地图查看差不多,需要启动高德,百度或腾讯地图来查看:特来分享,希望有所帮助. 案例效果如图: 要想启动第三方:首先要知道他的包名 一:高德 高德:co ...
- tensorflow入门(三)
三种代价函数 1,二次代价函数 式子代表预测值与样本值的差得平方和 由于使用的是梯度下降法,我们对变量w,b分别求偏导: 这种函数对于处理线性的关系比较好,但是如果遇到s型函数(如下图所示),效率 ...
- IOS - 前台时的推送弹窗效果
作者:Pikacode 原文链接:http://www.jianshu.com/p/67864e1c2085 本文获作者授权转载 或许很多童鞋还不知道,在 iOS 中收到推送通知时,如果 App 处于 ...
- 通过一道面试题了解Condition线程通信
Condition Condition接口描述了可能会与锁有关联的条件变量.这些变量在用法与使用Object.wait访问的隐式监视器类似,但提供了更强大的功能.需要特别指出的是,单个Lock可能与多 ...
- Spark安装和简单示例
spark的安装 先到官网下载安装包 注意第二项要选择和自己hadoop版本相匹配的spark版本,然后在第4项点击下载.若无图形界面,可用windows系统下载完成后传送到centos中. 本例中安 ...
- String类运算符重载,自己实现
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- 解决xadmin下设置“use_bootswatch = True”无效的问题
环境:python 2.7django 1.9xadmin采用源代码的方式引入到项目中QQ群交流:697028234 1.安装requests pip install requests 2./xadm ...