Caterpillars URAL
来源:互联网 发布:mysql服务无法启动1067 编辑:程序博客网 时间:2024/06/04 01:03
Young gardener didn’t visit his garden for a long time, and now it’s not very pleasant there: n caterpillars have appeared on the ground.
Kirill decided to use this opportunity to have some fun and organized a competition — “caterpillar crawl-race.”
At Kirill’s command all caterpillars start crawling from the ground to the top of a tree. But they get tired pretty fast. After crawling t i cm i-th caterpillar needs to rest for t i minutes. During that time it slides down a bit. Crawling speed of a caterpillar is 1 cm/minute, sliding speed — also 1 cm/minute.
Kirill is very much interested to find out how high on the tree is the leading caterpillar at different moments in time.
Input
First line contains one integer n — the number of caterpillars (1 ≤ n ≤ 10 6).
Second line contains n integers t i — characteristics of caterpillars (1 ≤ t i ≤ 10 9).
In the third line there is a number q — number of moments in time, which Kirill finds interesting (1 ≤ q ≤ 10 6).
Remaining q lines contain one query from Kirill each. A query is described by x i — number of minutes since the start of the competition (1 ≤ x i ≤ 10 6).
Output
For every query print in a separate line one integer, that describes how high is the highest caterpillar at the given moment of time.
Example
input output
4
1 3 2 1
12
1
2
3
4
5
6
7
8
9
10
11
12
output
1
2
3
2
1
2
1
2
3
2
1
0
//看了好久才看懂,对于某个点处的最高点的话,如果最高为 a[i] ,那么对于对于 //i 的奇数倍时,也为 a[i], 周期性 //对于某个 a[i] , 则它周围 某点横坐标等于 x 时, a[x] = ( a[i] - abs( //x - i) ) //即当 x > i 时 ,a[x] = a[i] + i - x; 当 x < i 时,a[x] = a[i] //- i +x; //这样的话,就可以从前往后,从后往前各遍历一遍,对于某个点y,比较a[y] 和 //a[i] + i - y,(i 是 y 前面的某个点)//从后往前也是一样,只不过比较的是 a[i] - i + y 和 y//这样的话就得到了最后的结果数组了 //因为查询最大的不会超过10^6,所以如果输入大于10^6的话就可以标记下,直接输出时间就好了// 还有就是如果没有 ios::sync_with_stdio(0); 这句话也会超时,跑到了 3046ms。// 这段代码是关闭cin 与 stdin 的同步,使cin 快了很多#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>#include<algorithm>#include<iostream>#define inf 1000000001using namespace std;const int maxn = 1000010;int tmp[maxn];int ans[maxn];int main(){ ios::sync_with_stdio(0); // 没有就会超时 int n; int h; int flag = 0; cin >> n; for(int i = 0; i < n; i ++) { cin >> h; if( flag == 1) continue; if( h >= 1000000) { flag = 1; continue; } int j; for( j = h; j < maxn; j += 2*h) { tmp[j] = max(tmp[j],h); } tmp[maxn - 1] = max( tmp[maxn - 1], h - ( j - ( maxn - 1))); } if(flag == 0) { int temp = -inf; for(int i = 1; i < maxn; i ++) { temp = max( tmp[i] + i,temp); ans[i] = max( ans[i], temp - i); } temp = -inf; for(int i = maxn - 1; i > 0; i --) { temp = max( temp, tmp[i] - i); ans[i] = max( ans[i], temp + i); } } int m,time; cin >> m; for(int i = 0; i < m; i ++) { cin >> time; if(flag == 1) cout << time << endl; else cout << ans[time] << endl; } return 0;}
- Caterpillars URAL
- URAL 2064|Caterpillars|堆
- Ural 2064 Caterpillars
- URAL
- 【ural】
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- 【测试环境】 测试TCPCOPY+MYSQL
- 抽象工厂模式(创建型)
- bootstrap中jquery插件——collapse折叠效果-手风琴效果
- node.js环境配置
- Android学习记录0324-Intent详解及StartActivityForResult用法
- Caterpillars URAL
- Docker学习笔记 — Swarm搭建Docker集群
- window 远程登录linux mysql失败 错误2003
- 数据库SQL Server与C#中数据类型的对应关系
- java关键字
- 优达学城优惠码
- 设计模式-状态模式(State)
- 将eclipse项目提交到GitHub(新)
- 带分数