Treasure Hunt II - ZOJ 3627 想法题
来源:互联网 发布:小清新调色思路 知乎 编辑:程序博客网 时间:2024/05/16 08:36
There are n cities(1, 2, ... ,n) forming a line on the wonderland. city i and city i+1 are adjacent and their distance is 1. Each city has many gold coins. Now, Alice and her friend Bob make a team to go treasure hunting. They starts at city p, and they want to get as many gold coins as possible in T days. Each day Alice and Bob can move to adjacent city or just stay at the place, and their action is independent. While as a team, their max distance can't exceed M.
Input
The input contains multiple cases.
The first line of each case are two integers n, p as above.
The following line contain n interger,"v1 v2 ... vn" indicate the gold coins in city i.
The next line is M, T.
(1<=n<=100000, 1<=p<=n, 0<=vi<=100000, 0<=M<=100000, 0<=T<=100000)
Output
Output the how many gold coins they can collect at most.
Sample Input
6 31 2 3 3 5 42 1
Sample Output
8
Hint
At day 1: Alice move to city 2, Bob move to city 4.
They can always get the gold coins of the starting city, even if T=0
题意:有n堆金币,两个人一开始在p位置,每个人每天可以移动一个位置,然后两个人的距离不能超过M,问在T天内最多得到的金币是多少。
思路:首先先把两个人张开,能张多大张多大,然后在这种情况下,考虑遍历两边的情况,假设左边走了a步,右边走了b步,那么需要的天数就是2*(a+b)-max(a,b)。然后考虑特殊情况。
AC代码如下:
#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;long long num[100010],sum[100010];int n;long long solve(int l,int r){ if(l<=0) l=1; if(r>=n) r=n; return sum[r]-sum[l-1];}long long solve2(int l,int r,int t){ long long ans; int i; if(t==0) { return solve(l,r); } if(l<=1) { r+=t; return solve(1,r); } if(r>=n) { l-=t; return solve(l,n); } else { ans=0; for(i=0;i<=t/2;i++) ans=max(ans,solve(l-i,r+t-i*2)); for(i=0;i<=t/2;i++) ans=max(ans,solve(l-t+i*2,r+i)); return ans; }}int main(){ int i,j,k,l,r,m,t,a,b; long long ans; sum[0]=0; while(~scanf("%d%d",&n,&k)) { l=k;r=k; for(i=1;i<=n;i++) { scanf("%lld",&num[i]); sum[i]=sum[i-1]+num[i]; } scanf("%d%d",&m,&t); if(t<=m/2) { l-=t; r+=t; printf("%lld\n",solve(l,r)); continue; } l-=m/2; r+=m/2; t-=m/2; if(t==0) { printf("%lld\n",solve(l,r)); continue; } if(m%2==0) ans=solve2(l,r,t); else ans=max(solve2(l-1,r,t-1),solve2(l,r+1,t-1)); printf("%lld\n",ans); }}
- Treasure Hunt II - ZOJ 3627 想法题
- ZOJ 3627 Treasure Hunt II
- ZOJ 3627 Treasure Hunt II
- ZOJ 3627 Treasure Hunt II
- ZOJ - 3627 Treasure Hunt II
- zoj 3627 Treasure Hunt II (贪心)
- ZOJ 3627 Treasure Hunt II (贪心)
- zoj 1158 Treasure Hunt
- ZOJ Treasure Hunt IV
- zoj 3629 Treasure Hunt IV
- zoj 3626 Treasure Hunt I
- ZOJ 3629 Treasure Hunt IV
- ZOJ 3626 Treasure Hunt I
- Zoj 3629 Treasure Hunt IV
- ZOJ 3626 Treasure Hunt I
- zoj 1158 || poj 1066 Treasure Hunt
- ZOJ 3626 Treasure Hunt I(树形DP)
- ZOJ 3626 Treasure Hunt I 树形背包
- 数据库设计过程
- JAVA8 十大新特性详解
- 求数组的最大值和次大值
- UI 基础 初级数据持久化 沙盒
- Bridge - 桥接模式
- Treasure Hunt II - ZOJ 3627 想法题
- 字符串查找算法-KMP
- 《C专家编程》学习笔记(对内存的思考)
- Java Word Break(单词拆解)
- 阻塞队列BlockQueue的一些方法
- Writing your first Django app--Django 第一步
- 反射(reflect)--使用某个类的字节码来获取该类对象
- LeetCode——Regular Expression Matching
- HDU 3790 (最短路)