ZOJ Candies
来源:互联网 发布:佳能cp1200安装软件 编辑:程序博客网 时间:2024/05/18 15:23
As we know, the majority of students in the world-class university like candy and game so much. With some candies, the students are playing a guessing game with you.
These students are standing in a line. Every student has some candies in the hand. (Of course the quantity of the candies will not be negative.) Some kindhearted students will tell you the exactly quantity of candies they have in hand, but some of them won't do that. You may think that THE GAME CAN'T PLAY, but the fact is, every student in line will tell you the exact total quantities of previous one (if exists), himself, and next one (if exists).
You should guess the maximum possible quantity the queried student might have.
Input
The input will consist of multiple testcases.
The first line of each test case is an integer n, indicating the amount of students, 3 ≤n ≤100000.
The second line contains n integers, the ith numberai represent the exact quantity of candies the ith student has, it will be a nonnegative number not more than 10000, ifai equals to -1, it means that the corresponding student haven't tell you the candies' quantity.
The third line also contains n integers, the ith number represents the sum ofai-1, ai and ai+1 (the first and last student's number contain only two guys' summation).
The forth line contains an integer m, indicating the amount of queries, 1 ≤m ≤100.Following m integers in a line indicate the 0-base number we queried.
Output
For each test case, you should output exactly m lines, each line contains an integer which is the answer of the corresponding query. If the queried quantity had been told in the input, just output that number.
Sample Input
5-1 -1 -1 -1 -1 2 3 3 3 220 3
Sample Output
22
Hint
The quantities they have might be "2 0 1 2 0", "0 2 1 0 2", "1 1 1 1 1" and so on.
任何一个变量都可以由a1 表示,维护a1的左端点和右端点,[l,r], 刚开始进入循环的时候两端点没有处理好,错了很多次。
#include <iostream>#include <cstring>#include <cstdio>#include <cmath>#define N 100010#define INF 0x7ffffffusing namespace std;struct num{ int x,y;}a[N];int b[N],c[N];int main(){ //freopen("data.in","r",stdin); int n,m; while(scanf("%d",&n)!=EOF) { for(int i=1;i<=n;i++) { scanf("%d",&b[i]); } for(int i=1;i<=n;i++) { scanf("%d",&c[i]); } int l=0,r=INF; a[1].x = 0; a[1].y = 1; if(b[1]!=-1) { l = r = b[1]; } a[2].x = c[1]; a[2].y = -1; if(b[2]!=-1) { l = r = (c[1]-b[2]); }else { r = min(r,c[1]); } for(int i=3;i<=n;i++) { int x1 = a[i-2].x; int y1 = a[i-2].y; int x2 = a[i-1].x; int y2 = a[i-1].y; int x = x1+x2; int y = y1+y2; a[i].x = c[i-1]-x; a[i].y = -y; x = c[i-1]-x; y = -y; if(b[i]!=-1&&y!=0) { l = r = (b[i]-x)/y; }else if(y!=0) { if(y>0) { int ll = ceil((double)(-x)/y); l = max(l,ll); }else { int rr = floor((double)(-x)/y); r = min(r,rr); } } } int x1 = a[n-1].x; int y1 = a[n-1].y; int x2 = a[n].x; int y2 = a[n].y; int x = x1+x2; int y = y1+y2; if(y!=0) { l = r = (c[n]-x)/y; } scanf("%d",&m); while(m--) { int k; scanf("%d",&k); k+=1; if(a[k].y==0) { printf("%d\n",a[k].x); continue; } int res1 = a[k].x + l * a[k].y; int res2 = a[k].x + r * a[k].y; int res = max(res1,res2); printf("%d\n",res); } } return 0;}
- zoj Candies
- ZOJ Candies
- ZOJ 1227 Free Candies
- ZOJ Candies 长沙
- zoj Candies 贪心
- zoj 3859——Candies
- ZOJ 1227 Free Candies(记忆化搜索)
- Candies
- Candies
- ZOJ Candies 2013年长沙赛区网络赛
- 2013 ACM/ICPC 长沙网络赛 J Candies (ZOJ)
- ZOJ XXXX Candies(13年长沙网络赛-J题-数学)
- Candies(HackerRank candies)
- InterviewStreet --Candies
- poj3159 Candies
- Another Candies
- poj3159 Candies
- Free Candies
- Uninstall Adobe Photoshop with WindowsUninstaller.Org Removal Tips
- java导出pdf
- 电脑ip绑定,vm虚拟机上网u
- 如何设计一个LRU Cache?
- android 获取外置卡的方法
- ZOJ Candies
- oracle 数据类型详解---日期型
- [每日一题] 11gOCP 1z0-052 :2013-09-25 Lock ――for update.................................C23
- 学习贴:看360公关如何把控搜狗投资案舆论
- 测试QQ邮箱开放平台
- Android service 学习
- 新炬网络Oracle 11g rac 删除节点
- 以跑马拉松心态分批买基金
- DEDE中如何过滤掉Html标签,并且截取字符串长度