ural 2064. Caterpillars
2064. Caterpillars
Time limit: 3.0 second
Memory limit: 64 MB
Memory limit: 64 MB
Young gardener didn’t visit his garden for a long time, and now it’s not very pleasant there: ncaterpillars 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 ti cm i-th caterpillar needs to rest for ti 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 ≤ 106).
Second line contains n integers ti — characteristics of caterpillars (1 ≤ ti ≤ 109).
In the third line there is a number q — number of moments in time, which Kirill finds interesting (1 ≤ q ≤ 106).
Remaining q lines contain one query from Kirill each. A query is described by xi — number of minutes since the start of the competition (1 ≤ xi ≤ 106).
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.
Sample
input | output |
---|---|
4 1 3 2 1 12 1 2 3 4 5 6 7 8 9 10 11 12 |
1 2 3 2 1 2 1 2 3 2 1 0 |
Problem Author: Nikita Sivukhin (prepared by Alexey Danilyuk, Nikita Sivukhin)
Problem Source: Ural Regional School Programming Contest 2015
Problem Source: Ural Regional School Programming Contest 2015
Tags: none
Difficulty: 559
题意:有n只蜗牛,对于第i只蜗牛,有性质ti,这只蜗牛往上爬ti秒,有下降ti秒,上升下降速度都是1cm/s,问第x秒爬得最高的蜗牛多高。
分析:首先画出蜗牛们的高度的函数,就可以看到它们是周期的类似三角形的函数。
如果我们只关注最高点,那么第x秒的答案是
max{a[i] - abs(x - i)}
其中a[i]表示在第i秒这个点的最高点最大是多少。
把那个abs拆开,分成x<i,x>i讨论
发现当x>i时
a[i]+i - x
当x<i时
a[i]-i + x
所以用是不是有点像单调队列?
其实我们只用记录最大值即可。
1 /** 2 Create By yzx - stupidboy 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 #include <cmath> 8 #include <deque> 9 #include <vector> 10 #include <queue> 11 #include <iostream> 12 #include <algorithm> 13 #include <map> 14 #include <set> 15 #include <ctime> 16 #include <iomanip> 17 using namespace std; 18 typedef long long LL; 19 typedef double DB; 20 #define MIT (2147483647) 21 #define INF (1000000001) 22 #define MLL (1000000000000000001LL) 23 #define sz(x) ((int) (x).size()) 24 #define clr(x, y) memset(x, y, sizeof(x)) 25 #define puf push_front 26 #define pub push_back 27 #define pof pop_front 28 #define pob pop_back 29 #define mk make_pair 30 31 inline int getInt() 32 { 33 int ret = 0; 34 char ch = ' '; 35 bool flag = 0; 36 while(!(ch >= '0' && ch <= '9')) 37 { 38 if(ch == '-') flag ^= 1; 39 ch = getchar(); 40 } 41 while(ch >= '0' && ch <= '9') 42 { 43 ret = ret * 10 + ch - '0'; 44 ch = getchar(); 45 } 46 return flag ? -ret : ret; 47 } 48 49 const int N = 1000010; 50 int n, m, cnt[N]; 51 int ans[N]; 52 53 inline void input() 54 { 55 for(cin >> n; n--; ) 56 { 57 int x, t; 58 cin >> x; 59 for(t = x; t < N; t += 2 * x) 60 cnt[t] = max(cnt[t], x); 61 cnt[N - 1] = max(cnt[N - 1], x - (t - (N - 1))); 62 } 63 cin >> m; 64 } 65 66 inline void solve() 67 { 68 for(int i = 1, now = -INF; i < N; i++) 69 { 70 now = max(now, cnt[i] + i); 71 ans[i] = max(ans[i], now - i); 72 } 73 for(int i = N - 1, now = -INF; i > 0; i--) 74 { 75 now = max(now, cnt[i] - i); 76 ans[i] = max(ans[i], now + i); 77 } 78 79 while(m--) 80 { 81 int x; 82 cin >> x; 83 cout << ans[x] << "\n"; 84 } 85 } 86 87 int main() 88 { 89 ios::sync_with_stdio(0); 90 input(); 91 solve(); 92 return 0; 93 }