2064. Caterpillars

Time limit: 3.0 second
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
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
所以用是不是有点像单调队列?
其实我们只用记录最大值即可。
 
 /**
Create By yzx - stupidboy
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define mk make_pair inline int getInt()
{
int ret = ;
char ch = ' ';
bool flag = ;
while(!(ch >= '' && ch <= ''))
{
if(ch == '-') flag ^= ;
ch = getchar();
}
while(ch >= '' && ch <= '')
{
ret = ret * + ch - '';
ch = getchar();
}
return flag ? -ret : ret;
} const int N = ;
int n, m, cnt[N];
int ans[N]; inline void input()
{
for(cin >> n; n--; )
{
int x, t;
cin >> x;
for(t = x; t < N; t += * x)
cnt[t] = max(cnt[t], x);
cnt[N - ] = max(cnt[N - ], x - (t - (N - )));
}
cin >> m;
} inline void solve()
{
for(int i = , now = -INF; i < N; i++)
{
now = max(now, cnt[i] + i);
ans[i] = max(ans[i], now - i);
}
for(int i = N - , now = -INF; i > ; i--)
{
now = max(now, cnt[i] - i);
ans[i] = max(ans[i], now + i);
} while(m--)
{
int x;
cin >> x;
cout << ans[x] << "\n";
}
} int main()
{
ios::sync_with_stdio();
input();
solve();
return ;
}

ural 2064. Caterpillars的更多相关文章

  1. Ural 2064:Caterpillars(思维暴力)

    http://acm.timus.ru/problem.aspx?space=1&num=2064 题意:有n只虫子在爬树,每个虫子往上爬ti距离后会往下掉落ti距离,每爬一个单位距离耗费一个 ...

  2. 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome

    题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...

  3. ural 2071. Juice Cocktails

    2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...

  4. ural 2073. Log Files

    2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...

  5. ural 2070. Interesting Numbers

    2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...

  6. ural 2069. Hard Rock

    2069. Hard Rock Time limit: 1.0 secondMemory limit: 64 MB Ilya is a frontman of the most famous rock ...

  7. ural 2068. Game of Nuts

    2068. Game of Nuts Time limit: 1.0 secondMemory limit: 64 MB The war for Westeros is still in proces ...

  8. ural 2067. Friends and Berries

    2067. Friends and Berries Time limit: 2.0 secondMemory limit: 64 MB There is a group of n children. ...

  9. ural 2066. Simple Expression

    2066. Simple Expression Time limit: 1.0 secondMemory limit: 64 MB You probably know that Alex is a v ...

随机推荐

  1. Linux(Ubuntu)之设定开机自启动

    分两种:对自建脚本,对已安装服务 对已安装服务, 只需在/etc/rc.local中添加相应的启动命令,如: 重启后,即可自启动服务. 对自建脚本自启动: 创建脚本test,修改其权限为755,如: ...

  2. 设计模式之构建者模式(Builder):初步理解

    构建者(Builder)设计模式(又叫生成器设计模式): 当一个类的内部数据过于复杂的时候(通常是负责持有数据的类,比如Config.VO.PO.Entity...),要创建的话可能就需要了解这个类的 ...

  3. Android 反编译 -- apktool、dex2jar、jd-gui

    原文:http://blog.csdn.net/vipzjyno1/article/details/21039349/ apktool 最新官网: http://ibotpeaches.github. ...

  4. Linux下pipe使用注意事项

    转自:http://blog.yufeng.info/archives/1485 Linux下的pipe使用非常广泛, shell本身就大量用pipe来粘合生产者和消费者的. 我们的服务器程序通常会用 ...

  5. Bootstrap简介

    接下来的一段时间,想研究一下现有的网页框架,第一个不容错过的就是Bootstrap,Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架.Bootstrap 是基于 HTML.CS ...

  6. 算法系列:XXX

    转载自http://www.cnblogs.com/skynet/p/3372855.html 这次分享的宗旨是——让大家学会创建与使用静态库.动态库,知道静态库与动态库的区别,知道使用的时候如何选择 ...

  7. GMap.Net开发之在地图上添加多边形

    上一篇介绍了在GMap上添加自定义标签(GMapMarker),这篇介绍在GMap上添加多边形(GMapPolyogn),并且介绍如何在地图上画任意的多边形. 如果已经知道了多边形的各个点的位置,就可 ...

  8. 使用.NET Framework的配置文件app.config

    在一般的项目中,为了使你的代码更加灵活,更方便调整,减少不必要的hard code,我们都在config中添加许多配置信息,一般可以选择.NET自带的配置文件形式app.config或者web项目中的 ...

  9. 用CocoaPods做iOS程序的依赖管理(转摘)

    转摘自:http://blog.devtang.com/blog/2014/05/25/use-cocoapod-to-manage-ios-lib-dependency/ 文档更新说明 2012-1 ...

  10. js 上传文件后缀名的判断 var flag=false;应用

    js 上传文件后缀名的判断  var flag=false;应用 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional// ...