HDU-6669-Game(模拟,贪心)
链接:
https://vjudge.net/problem/HDU-6669
题意:
度度熊在玩一个好玩的游戏。
游戏的主人公站在一根数轴上,他可以在数轴上任意移动,对于每次移动,他可以选择往左或往右走一格或两格。
现在他要依次完成 n 个任务,对于任务 i,只要他处于区间 [ai,bi] 上,就算完成了任务。
度度熊想知道,为了完成所有的任务,最少需要移动多少次?
度度熊可以任意选择初始位置。
思路:
刚开始以为是贪心,按y拍了个序,疯狂wa,找了个题解发现是要按顺序.....
维护当前所在区间,然后找从最近的跑来跑去就行.
代码:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e3+10;
struct Node
{
int x, y;
}node[MAXN];
int n;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--)
{
cin >> n;
for (int i = 1;i <= n;i++)
cin >> node[i].x >> node[i].y;
int res = 0;
int l = node[1].x, r = node[1].y;
for (int i = 2;i <= n;i++)
{
if (node[i].x >= l && node[i].y <= r)
l = node[i].x, r = node[i].y;
else if (node[i].x >= l && node[i].x <= r && node[i].y > r)
l = node[i].x;
else if (node[i].x < l && node[i].y >= l && node[i].y <= r)
r = node[i].y;
else if (node[i].x > r)
{
res += (node[i].x-r+1)/2;
l = node[i].x, r += ((node[i].x-r+1)/2)*2;
r = min(r, node[i].y);
}
else if (node[i].y < l)
{
res += (l-node[i].y+1)/2;
r = node[i].y, l -= ((l-node[i].y+1)/2)*2;
l = max(l, node[i].x);
}
}
cout << res << endl;
}
return 0;
}
HDU-6669-Game(模拟,贪心)的更多相关文章
- HDU 4903 (模拟+贪心)
Fighting the Landlords Problem Description Fighting the Landlords is a card game which has been a he ...
- 2019 年百度之星·程序设计大赛 - 初赛一Game HDU 6669 (实现,贪心)
Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...
- HDU 4442 Physical Examination(贪心)
HDU 4442 Physical Examination(贪心) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=4442 Descripti ...
- hdu 模拟 贪心 4550
卡片游戏 Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): Accepted Su ...
- HDU 5835 Danganronpa (贪心)
Danganronpa 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5835 Description Chisa Yukizome works as ...
- BZOJ1029: [JSOI2007]建筑抢修[模拟 贪心 优先队列]
1029: [JSOI2007]建筑抢修 Time Limit: 4 Sec Memory Limit: 162 MBSubmit: 3785 Solved: 1747[Submit][Statu ...
- Codeforces Round #375 (Div. 2) A B C 水 模拟 贪心
A. The New Year: Meeting Friends time limit per test 1 second memory limit per test 256 megabytes in ...
- Codeforces 452D [模拟][贪心]
题意: 给你k件衣服处理,告诉你洗衣机烘干机折叠机的数量,和它们处理一件衣服的时间,要求一件衣服在洗完之后必须立刻烘干,烘干之后必须立刻折叠,问所需的最小时间. 思路: 1.按照时间模拟 2.若洗完的 ...
- HDU 5821 Ball (贪心)
Ball 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5821 Description ZZX has a sequence of boxes nu ...
- HDU 4121 Xiangqi 模拟题
Xiangqi Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4121 ...
随机推荐
- nginx启动用户和nginx工作用户要一致
[root@bogon default]# ps aux | grep "nginx: worker process" | awk '{print $1}'rootrootroot ...
- Python示例-TCP Port Scan
import socket import threading # target host address host = "127.0.0.1" # thread list thre ...
- nodejs+koa2 实现一个get请求
html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- spring boot-14.集成MyBatis
1.如何使用注解版Mybatis? (1)引入mybatis ,druid,Mysql 的依赖,环境搭建可以参考第13篇的内容 <dependency> <groupId>or ...
- centos7下安装composer和git
一.安装composer composer 属于php的包依赖管理工具. 1.进入Composer国内镜像网站文档页查看安装方法: https://docs.phpcomposer.com/00-in ...
- [LeetCode] 完全二叉树的节点个数
题目链接: https://leetcode-cn.com/problems/count-complete-tree-nodes 难度:中等 通过率:57.4% 题目描述: 给出一个 完全二叉树 ,求 ...
- Python 入门之 内置模块 -- hashlib模块
Python 入门之 内置模块 -- hashlib模块 1.hashlib 摘要算法,加密算法 (1)主要用途: <1> 加密 : md5 sha1 sha256 sha512 md5, ...
- 优雅的用两种方式爬网络 txt 文件【雾
TXT 文件?? (笑 这里爬的是 74xsw (咱好像也不怎么逛的网站)的英雄再临 ... 请注意这并不是教程,只是贴个代码仅供参考而已[雾 这里 用的 getTXT 的方式有两种,一种是每个章节分 ...
- 【Linux】C字节对齐
原文地址:https://www.jianshu.com/p/e8fcc01041a7 什么是对齐,以及为什么要对齐: 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问 ...
- allure 这么高大上的测试报告环境,5 分钟搞定
allure 的测试报告是老板喜欢的样子.如果能用上 allure,干嘛还选择其他的测试报告类型呢?python 的 pytest 单元测试框架有 allure 的插件,可以很方便的在 python ...