D. Treasure Hunting ( 思维题 , 贪心)
题意: 在一个 n * m 的地图里,有 k 个宝藏,你的起点在 (1, 1), 每次你能 向下向右向左移动(只要在地图里);
现在,有 q 个安全的列, 你只有在这些列上面,你才能向下走。 问你收集所有宝藏,需要走的最少步数
解: 首先,我们对每一行 维护数组 L, R,分别代表 这一行的所有宝藏中,最左的那个和最右的那个的 纵坐标
然后 维护两个值 ansl, ansr 分别代表你处理完前 i 行所有的宝藏都拿完了之后;
停在 L[ i ], 和停在 R[ i ]处的最小步数;
那么你更新 ansl, ansr 各有四种情况; ( 代码有提到,详见代码)
然后处理完N行后, 去 ansl, ansr 较小的那个,就是所有横向需要走的最小步数。
然后纵向的话,你走的步数就是 所有宝藏中最大的 x - 1 步;
然后相加就是答案了。
- #include <iostream>
- #include <cstdio>
- #include <fstream>
- #include <algorithm>
- #include <cmath>
- #include <deque>
- #include <vector>
- #include <queue>
- #include <string>
- #include <cstring>
- #include <map>
- #include <stack>
- #include <set>
- #define LL long long
- #define ULL unsigned long long
- #define rep(i,j,k) for(int i=j;i<=k;i++)
- #define dep(i,j,k) for(int i=k;i>=j;i--)
- #define INF 0x3f3f3f3f3f3f3f3f
- #define mem(i,j) memset(i,j,sizeof(i))
- #define make(i,j) make_pair(i,j)
- #define pb push_back
- #define Pi acos(-1.0)
- using namespace std;
- const int N = 2e5 + ;
- LL l[N], r[N], s[N];
- int main() {
- int n, m, k, q;
- while(~scanf("%d %d %d %d", &n, &m, &k, &q)) {
- rep(i, , n) l[i] = INF, r[i] = ;
- LL Y = ;
- rep(i, , k) {
- LL x, y;
- scanf("%lld %lld", &x, &y);
- Y = max(Y, x);
- l[x] = min(l[x], y);
- r[x] = max(r[x], y);
- }
- rep(i, , q) scanf("%lld", &s[i]);
- sort(s + , s + + q);
- LL ansl, ansr;
- LL L = l[], R = r[];
- if(r[] == ) ansl = , ansr = , L = R = ;
- else ansl = r[] - + r[] - l[], ansr = r[] - ;
- rep(i, , n) if(r[i]) {
- int indexl = lower_bound(s + , s + + q, L) - s;
- int indexr = lower_bound(s + , s + + q, R) - s;
- int Ll, Lr, Rl, Rr; Ll = Lr = Rl = Rr = INF;
- if(indexl <= q) Lr = s[indexl]; if(indexl > ) Ll = s[indexl - ];
- if(indexr <= q) Rr = s[indexr]; if(indexr > ) Rl = s[indexr - ];
- LL nowL = INF, nowR = INF;
- nowL = min(nowL, ansl + abs(L - Ll) + abs(Ll - r[i]) + abs(r[i] - l[i])); ///L -> Ll -> r[i] -> l[i]
- nowL = min(nowL, ansl + abs(L - Lr) + abs(Lr - r[i]) + abs(r[i] - l[i])); ///L -> Lr -> r[i] -> l[i]
- nowL = min(nowL, ansr + abs(R - Rl) + abs(Rl - r[i]) + abs(r[i] - l[i])); ///R -> Rl -> r[i] -> l[i]
- nowL = min(nowL, ansr + abs(R - Rr) + abs(Rr - r[i]) + abs(r[i] - l[i])); ///R -> Rr -> r[i] -> l[i]
- nowR = min(nowR, ansl + abs(L - Ll) + abs(Ll - l[i]) + abs(l[i] - r[i])); ///L -> Ll -> l[i] -> r[i]
- nowR = min(nowR, ansl + abs(L - Lr) + abs(Lr - l[i]) + abs(l[i] - r[i])); ///L -> Lr -> l[i] -> r[i]
- nowR = min(nowR, ansr + abs(R - Rl) + abs(Rl - l[i]) + abs(l[i] - r[i])); ///R -> Rl -> l[i] -> r[i]
- nowR = min(nowR, ansr + abs(R - Rr) + abs(Rr - l[i]) + abs(l[i] - r[i])); ///R -> Rr -> l[i] -> r[i]
- ansl = nowL, ansr = nowR; L = l[i], R = r[i];
- }
- printf("%lld\n", min(ansl, ansr) + Y - );
- }
- return ;
- }
D. Treasure Hunting ( 思维题 , 贪心)的更多相关文章
- 洛谷P4643 [国家集训队]阿狸和桃子的游戏(思维题+贪心)
思维题,好题 把每条边的边权平分到这条边的两个顶点上,之后就是个sb贪心了 正确性证明: 如果一条边的两个顶点被一个人选了,一整条边的贡献就凑齐了 如果分别被两个人选了,一作差就抵消了,相当于谁都没有 ...
- 思维题+贪心——牛客多校第一场C
/* 给定一组n维向量 A=(a1/m,a2/m,a3/m ... an/m), 求另一个n维向量 P=(p1,p2,p3...pn),满足sum{pi}=1,使得ans=sum{(ai/m-pi)^ ...
- B. Heaters 思维题 贪心 区间覆盖
B. Heaters 这个题目虽然只有1500的分数,但是我还是感觉挺思维的,我今天没有写出来,然后看了一下题解 很少做这种区间覆盖的题目,也不是很擅长,接下来讲讲我看完题解后的思路. 题目大意是:给 ...
- hdu 4091 数学思维题贪心
/* 参看博客地址:http://blog.csdn.net/oceanlight/article/details/7857713 重点是取完最优的后剩余的rest=n%lcm+lcm;中性价比小的数 ...
- PAT 甲级 1038 Recover the Smallest Number (30 分)(思维题,贪心)
1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to ...
- [Codeforces 1201D]Treasure Hunting(DP)
[Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列 ...
- 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas
题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...
- 贪心/思维题 UVA 11292 The Dragon of Loowater
题目传送门 /* 题意:n个头,m个士兵,问能否砍掉n个头 贪心/思维题:两个数组升序排序,用最弱的士兵砍掉当前的头 */ #include <cstdio> #include <c ...
- ZOJ 3829 贪心 思维题
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...
- 2017广东工业大学程序设计竞赛决赛 题解&源码(A,数学解方程,B,贪心博弈,C,递归,D,水,E,贪心,面试题,F,贪心,枚举,LCA,G,dp,记忆化搜索,H,思维题)
心得: 这比赛真的是不要不要的,pending了一下午,也不知道对错,直接做过去就是了,也没有管太多! Problem A: 两只老虎 Description 来,我们先来放松下,听听儿歌,一起“唱” ...
随机推荐
- [xpath] text()和string()区别
质区别 text()是一个node test,而string()是一个函数,data()是一个函数且可以保留数据类型.此外,还有点号(.)表示当前节点. 使用要点 XML例子: <book> ...
- Asteroid Collision
We are given an array asteroids of integers representing asteroids in a row. For each asteroid, the ...
- 2-SAT问题介绍求解 + 模板题P4782
(点击此处查看原题) 什么是2-SAT问题 sat 即 Satisfiability,意思为可满足,那么2-SAT表示一些布尔变量只能取true或者false,而某两个变量之间的值存在一定的关系(如: ...
- FFmpeg4.0笔记:封装ffmpeg的音频重采样功能类CSwr
Github https://github.com/gongluck/FFmpeg4.0-study/tree/master/Cff CSwr.h /************************* ...
- java限流工具类
代码 import com.google.common.util.concurrent.RateLimiter; import java.util.concurrent.ConcurrentHashM ...
- Elasticsearch入门教程(五):Elasticsearch查询(一)
原文:Elasticsearch入门教程(五):Elasticsearch查询(一) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:h ...
- nginx(五)- linux下安装nginx与配置
linux系统为Centos 64位 准备目录 [root@instance-3lm099to ~]# mkdir /usr/local/nginx [root@instance-3lm099to ~ ...
- Slimvoice能代替JavaScript?
对于Slimvoice(https://slimvoice.co/),我想反对JavaScript的炒作,并对整个应用程序进行服务器端渲染.您可能会说:“用户必须在使用应用程序时重新加载每个页面,这必 ...
- chrome79开发者工具代码高亮失效的解决办法
升级chrome最新版本后,存在开发者工具Sources内代码高亮失效的情况 解决办法: 1. 开发者工具面板右上角菜单->Setting->Preferences 2. 将Theme切换 ...
- redis性能指标
1.当内存使用达到设置的最大阀值时,需要选择一种key的回收策略,可在Redis.conf配置文件中修改“maxmemory-policy”属性值. 若是Redis数据集中的key都设置了过期时间,那 ...