ZOJ 3627 Treasure Hunt II (贪心,模拟)
题意:有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 (贪心,模拟)的更多相关文章
- zoj 3627 Treasure Hunt II (贪心)
本文出自 http://blog.csdn.net/shuangde800 题目链接:zoj-3627 题意 直线上有n个城市, 第i个城市和i+1个城市是相邻的. 每个城市都有vi的金币. ...
- zoj 3629 Treasure Hunt IV 打表找规律
H - Treasure Hunt IV Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- ZOJ 3626 Treasure Hunt I 树上DP
E - Treasure Hunt I Time Limit:2000MS Memory Limit:65536KB Description Akiba is a dangerous country ...
- ZOJ 3626 Treasure Hunt I(树形dp)
Treasure Hunt I Time Limit: 2 Seconds Memory Limit: 65536 KB Akiba is a dangerous country since ...
- 【ZOJ3627】Treasure Hunt II
题目大意:给定一个长度为 N 的序列,现有两个人从 P 点出发,每个单位时间每个人最多可以移动一个单位,两人之间的最大距离不能超过 M,一共有 T 单位的时间,求在合法情况下,两人可以获得的序列点权和 ...
- ZOJ 3626 Treasure Hunt I (树形DP,常规)
题意:给一棵树,一个人站在节点s,他有m天时间去获取各个节点上的权值,并且最后需要回到起点s,经过每条边需要消耗v天,问最少能收获多少权值? 思路: 常规的,注意还得跑回原地s. //#include ...
- zoj 3627#模拟#枚举
Treasure Hunt II Time Limit: 2 Seconds Memory Limit: 65536 KB ...
- 贪心+模拟 ZOJ 3829 Known Notation
题目传送门 /* 题意:一串字符串,问要最少操作数使得成为合法的后缀表达式 贪心+模拟:数字个数 >= *个数+1 所以若数字少了先补上在前面,然后把不合法的*和最后的数字交换,记录次数 岛娘的 ...
- zoj Treasure Hunt IV
Treasure Hunt IV Time Limit: 2 Seconds Memory Limit: 65536 KB Alice is exploring the wonderland ...
随机推荐
- 3.sql中的向上递归和向下递归
1.向下递归 select * from table_name where 条件 connect by prior bmbm(本级关联条件)=sjbmbm(上级关联条件) start with bmb ...
- Codeforces Round #408( Div2)
Bank Hacking 阅读题,读完之后手算一下可以发现每一个bank被hack所需要的strength无非分为三种情况. 1. $a_i$,当且仅当i为第一个选择的点. 2. $a_i+1$,当且 ...
- sublime text3配置node.js环境,以及出现的问题
1) 下载sublime并安装,下载列表:https://www.sublimetext.com/3 2) 下载sublime的nodejs插件,需是集成Nodejs插件到sublime中,下载地址: ...
- Linux中如何开启8080端口供外界访问 和开启允许对外访问的端口8000
举例: 开放10000端口的解决步骤如下: 1.修改/etc/sysconfig/iptables文件,增加如下一行: -A INPUT -m state --state NEW -m tcp -p ...
- [python]MS17-010自动化扫描脚本
一种是3gstudent分享的调用Nsa泄露的smbtouch-1.1.1.exe实现验证,另一种是参考巡风的poc.这里整合学习了下两种不同的方法. import os import fileinp ...
- JavaScript数组及相关方法
数组 1.创建数组 var array = new Array(); var array = new Array(size);//指定数组的长度 var array = new Array(item1 ...
- Untiy PoolManager随手记
用法,1是获取,2是清除, 问题是这个池到底能做什么用 首先用这个池生成的对象是在池节点下使用,而不是取出来用(可以取出来用,直接transform.parent赋值就可以) 疑问,池里面的节点时什么 ...
- VR相关网站
VR87870 http://www.87870.com/ VR玩家网 http://www.vrwanjia.cn/ VR之家 http://www.vr.cn/ http://gad.qq.com ...
- Pycharm 配置autopep8到菜单
Pycharm 可以自动检测PEP8规范. 我们可以安装autopep8来自动修改文件实现PEP8规范. 1.通过Pycharm安装autopep8 2.File->Setting->Ex ...
- hoj2665 Factory of XiaoE
Factory of XiaoE My Tags (Edit) Source : zhouguyue & lilu0355 & xiaoE Time limit : 1 s ...