题意:有n个城市并排着,每个城市有些珠宝,有两个人站在第s个城市准备收集珠宝,两人可以各自行动,但两人之间的距离不能超过dis,而且每经过一个城市就需要消耗1天,他们仅有t天时间收集珠宝,问最多能收集多少珠宝?

思路:

  其实就是类似一个滑动窗口在收集一个序列上的权值。首先两个人可以同时往两边散开,直到极限距离dis(这样省时),然后他们可能往左/右走,也可能往左走一会儿再往右走,也可能往右走一会儿再往左走。可以看出其实这些考虑都是对称的,那么我们主要考虑1边就行了。可以尝试枚举往左边走k天,其他t-k天往右走(利用前缀和)。注意有可能dis是个奇数的,那么在t充足的情况下,应该两边扩展到dis-1就停下呢,还是往左/右一点呢?还得靠枚举,左边试一下,右边试一下。dis也可能比t还大,那么最多也就是同时往两边扩展t天。

  情况不算多,但是写起来还是得很小心的。还有,数据必须用longlong了。对称情况只需要反转一下序列,改变一下起点s。时间复杂度O(n)。

 //#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <algorithm>
#include <vector>
#include <iostream>
#define pii pair<int,int>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int N=;
const double PI = acos(-1.0);
LL w[N], sum[N];
int n, dis, s;
LL get_ans(int p1,int p2,int t)
{
LL big=sum[p2]-sum[p1-];
LL ans=big;
for(int k=; k<p1&&t>=k; k++) //枚举给左边k天,其他给右边
{
LL left=sum[p1-]-sum[p1-k-], right=;
int r=t-*k; //若大于0,则可继续往右走
if( r> ) right=sum[min(n,p2+r)]-sum[p2]; //右边还能获得珠宝
ans=max(ans, big+left+right);
}
return ans;
} LL cal(int t) //初始定点p1,p2必须拉到最长
{
int p1=max(, s-min(dis/,t));
int p2=min(n, s+min(dis/,t));
LL ans=;
t-=dis/;
if(dis%==) //奇数
{
if( t< || p1==&&p2==n ) return get_ans(p1,p2, t); //t已无剩
if( p1> ) ans=max(ans, get_ans(p1-,p2, t-)); //左坑
if( p2<n ) ans=max(ans, get_ans(p1,p2+, t-)); //右坑
}
else //偶数,不需要占坑
{
ans=max(ans, get_ans(p1,p2, t));
}
return ans;
} int main()
{
freopen("input.txt", "r", stdin);
int t;LL ans;
while(~scanf("%d%d",&n,&s))
{
memset(sum, , sizeof(sum));
for(int i=; i<=n; i++)
{
scanf("%d",&w[i]);
sum[i]+=sum[i-]+w[i];
}
scanf("%d%d",&dis,&t); //dis是两人的距离上限,t是所有的时间
ans=cal(t); reverse(w+,w+n+); //反过来继续求。
memset(sum, , sizeof(sum));
for(int i=; i<=n; i++) sum[i]=sum[i-]+w[i];
s=n+-s;
ans=max(ans, cal(t));
cout<<ans<<endl;
}
return ;
}

AC代码

ZOJ 3627 Treasure Hunt II (贪心,模拟)的更多相关文章

  1. zoj 3627 Treasure Hunt II (贪心)

    本文出自   http://blog.csdn.net/shuangde800 题目链接:zoj-3627 题意 直线上有n个城市, 第i个城市和i+1个城市是相邻的.  每个城市都有vi的金币.   ...

  2. zoj 3629 Treasure Hunt IV 打表找规律

    H - Treasure Hunt IV Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu ...

  3. ZOJ 3626 Treasure Hunt I 树上DP

    E - Treasure Hunt I Time Limit:2000MS Memory Limit:65536KB Description Akiba is a dangerous country ...

  4. ZOJ 3626 Treasure Hunt I(树形dp)

    Treasure Hunt I Time Limit: 2 Seconds      Memory Limit: 65536 KB Akiba is a dangerous country since ...

  5. 【ZOJ3627】Treasure Hunt II

    题目大意:给定一个长度为 N 的序列,现有两个人从 P 点出发,每个单位时间每个人最多可以移动一个单位,两人之间的最大距离不能超过 M,一共有 T 单位的时间,求在合法情况下,两人可以获得的序列点权和 ...

  6. ZOJ 3626 Treasure Hunt I (树形DP,常规)

    题意:给一棵树,一个人站在节点s,他有m天时间去获取各个节点上的权值,并且最后需要回到起点s,经过每条边需要消耗v天,问最少能收获多少权值? 思路: 常规的,注意还得跑回原地s. //#include ...

  7. zoj 3627#模拟#枚举

    Treasure Hunt II Time Limit: 2 Seconds                                     Memory Limit: 65536 KB    ...

  8. 贪心+模拟 ZOJ 3829 Known Notation

    题目传送门 /* 题意:一串字符串,问要最少操作数使得成为合法的后缀表达式 贪心+模拟:数字个数 >= *个数+1 所以若数字少了先补上在前面,然后把不合法的*和最后的数字交换,记录次数 岛娘的 ...

  9. zoj Treasure Hunt IV

    Treasure Hunt IV Time Limit: 2 Seconds      Memory Limit: 65536 KB Alice is exploring the wonderland ...

随机推荐

  1. HUD-1548

    A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  2. 3、css边框以及其他常用样式

    一.边框 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  3. maven构建java项目、web项目

    maven构建java项目.web项目 一.mvn构建web项目 1安装mvn(包括path) 2命令:mvn archetype:create -DgroupId=cn.edu.sdau.neat ...

  4. 微信小程序开发之页面wxml里面实现循环 wx:for

    js代码: Page({ data:{ upploadimagelist:{},    //上报图片列表 js数组 }}) 后台数据库保存的格式:{"imageList":[{&q ...

  5. 【WIP_S9】图论算法

    创建: 2018/06/01 图的概念 有向边 有向图 无向边 无向图 点的次数: 点连接的边的数量 闭路: 起点和重点一样 连接图: 任意两点之间都可到达 无闭路有向图: 没有闭路的有向图 森林: ...

  6. STL——pair

    功能:pair将一对值组合成一个值,这一对值可以具有不同的数据类型(T1和T2),两个值可以分别用pair的两个公有函数first和second访问. #include <bits/stdc++ ...

  7. TP5之验证码

    处于安全的角度,我们一般都会使用tp5自带的验证码 直接上图,干就完了

  8. Linux下更改mysql版本

    想要更改linux的mysql版本,并不需要重装系统重新选择mysql版本,只需要删除掉原来的mysql然后在安装新的就可以啦 (谨记:一定要做好数据库备份) 详情请参照:大牛博客 over!over ...

  9. 658. Find K Closest Elements

    Given a sorted array, two integers k and x, find the k closest elements to x in the array. The resul ...

  10. HDU1059 【DP·二进制数优化】

    题意: 有6种不同价值的物品,然后问你能不能分成两半使得两堆价值相等: 思路: 一共有20000*6=120000 多的价值, 总共背包有20000个,价值最大是120000,看看能不能DP到valu ...