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 ...
随机推荐
- caffe-----silence layer 作用
最近看到prototxt里面有silence这个层,好奇是干什么用的,而且看源码也出奇的简单: #include <vector> #include "caffe/layers/ ...
- 奉献pytorch 搭建 CNN 卷积神经网络训练图像识别的模型,配合numpy 和matplotlib 一起使用调用 cuda GPU进行加速训练
1.Torch构建简单的模型 # coding:utf-8 import torch class Net(torch.nn.Module): def __init__(self,img_rgb=3,i ...
- C++ 优先队列 priority_queue
平时定义的时候,直接上就完事了: priority_queue<int>Q; 默认大根堆. 之前很菜的时候不知道小根堆怎么写,还在考场上干过加个负号甩到大根堆里面去的蠢事. 它的完整形式呢 ...
- Mac022-brew安装tool
1.Mac上查看目录的树结构 Step1:安装tree命令 brew install tree Step2:某一目录下执行tree,会将该目录下的所有目录以树形结构显示 $ tree $ tree - ...
- MySQL 常用命令和基础语法
-- mysql 命令 SHOW DATABASES; #查看目前系统中存在的数据库 use database_name; #切换数据库 SHOW TABLES; #显示当前数据库下面的所有可用的表 ...
- Akka系列(九):Akka分布式之Akka Remote
前言.... Akka作为一个天生用于构建分布式应用的工具,当然提供了用于分布式组件即Akka Remote,那么我们就来看看如何用Akka Remote以及Akka Serialization来构建 ...
- Linux c实现一个tcp文件服务器和客户端
总体需求:编写tcp文件服务器和客户端.客户端可以上传和下载文件. ================================================ 分解需求 客户端功能描述: 1)要 ...
- excel常用公式--关联匹配类
index: 根据位置返回单元格的值. match: 根据单元格的值返回位置. index+match替代vlookup rank:返回一列数字的数字排位.数字的排位是其相对于列表中其他值的大小.
- 2019CSP-S游记(真)
本来是考完了的,但是由于江西省的负责人员的不小心(?),江西oier的大部分代码都被删掉了, 所以我们需要重考,想看我之前CSP的游记可以看这个点我.下面是我江西重考的游记: Day0 又集训了一个星 ...
- 干货 | 深入分析 string.intern() 方法
首先我们来看一段代码: public class InternTest { public static void main(String[] args) { String str1 ...