Shopping

题目连接:

http://codeforces.com/gym/100803/attachments

Description

Your friend will enjoy shopping. She will walk through a mall along a straight street, where N

individual shops (numbered from 1 to N) are aligned at regular intervals. Each shop has one

door and is located at the one side of the street. The distances between the doors of the adjacent

shops are the same length, i.e. a unit length. Starting shopping at the entrance of the mall, she

visits shops in order to purchase goods. She has to go to the exit of the mall after shopping.

She requires some restrictions on visiting order of shops. Each of the restrictions indicates that

she shall visit a shop before visiting another shop. For example, when she wants to buy a nice

dress before choosing heels, she shall visit a boutique before visiting a shoe store. When the

boutique is farther than the shoe store, she must pass the shoe store before visiting the boutique,

and go back to the shoe store after visiting the boutique.

If only the order of the visiting shops satisfies all the restrictions, she can visit other shops in

any order she likes.

Write a program to determine the minimum required walking length for her to move from the

entrance to the exit.

Assume that the position of the door of the shop numbered k is k units far from the entrance,

where the position of the exit is N + 1 units far from the entrance.

Input

The input consists of a single test case.

N m

c1 d1

.

.

.

cm dm

The first line contains two integers N and m, where N (1 ≤ N ≤ 1000) is the number of shops,

and m (0 ≤ m ≤ 500) is the number of restrictions. Each of the next m lines contains two

integers ci and di (1 ≤ ci < di ≤ N) indicating the i-th restriction on the visiting order, where

she must visit the shop numbered ci after she visits the shop numbered di (i = 1, . . . , m).

There are no pair of j and k that satisfy cj = ck and dj = dk.

Output

Output the minimum required walking length for her to move from the entrance to the exit.

You should omit the length of her walk in the insides of shops.

Sample Input

10 3

3 7

8 9

2 5

Sample Output

23

Hint

题意

在一条街上有1-n个店,你一开始在0这个位置,你需要访问每个店,并且最后到n+1这个点

然后有m个限制,就给你ci,di

表示你去ci这个店之前,你必须先到bi这个点才行

保证di>ci

问你最小距离走多少

题解:

贪心,我们走的话,就走闭环就好了

闭环是什么?这个区间的[l,r]中,l点是被限制的,并且这个区间的最大的限制为r。

那么我们就往回走一次,再走过去就好了

实力贪一波

代码

#include<bits/stdc++.h>
using namespace std; const int maxn = 1200;
int fa[maxn]; int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n+1;i++)
fa[i]=i;
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
fa[x]=max(fa[x],y);
}
long long ans = n+1;
int l=1,r=1;
for(int i=1;i<=n;)
{
r=fa[i];
int now = i;
while(now<=r)
{
r=max(r,fa[now]);
now++;
}
ans+=2*(r-i);
i=now;
}
cout<<ans<<endl;
}

Codeforces Gym 100803C Shopping 贪心的更多相关文章

  1. Codeforces Gym 100203E bits-Equalizer 贪心

    原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 考虑到交换可以减少一次操作,那么可以 ...

  2. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  3. Codeforces GYM 100876 J - Buying roads 题解

    Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...

  4. codeforces 704B - Ant Man 贪心

    codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...

  5. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

  6. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

  7. Codeforces Gym 101623A - 动态规划

    题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...

  8. 【Codeforces Gym 100725K】Key Insertion

    Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...

  9. Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】

     2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...

随机推荐

  1. 【LeetCode 239】Sliding Window Maximum

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  2. 匿名函数自执行原理和instanceof运算符执行原理

    今天收到RSS订阅中有一篇<Javascript – Arraylike的7种实现>,看第一种实现方式是,瞬间被!function(){}()这种匿名函数自执行方式给亮瞎了眼睛.这种写法绝 ...

  3. Java之--Java语言基础组成(关键字、标识符、注释、常量和变量、运算符)

    Java语言基础组成-关键字.标识符.注释.常量和变量.运算符 Java语言由8个模块构成,分别为:1.关键字:2.标识符(包名.类名.接口名.常量名.变量名等):3.注释:4.常量和变量:5.运算符 ...

  4. DzzOffice共享文件夹、共享目录设置

    dzzoffice中共享目录的设置,是通过机构部门建立的. 首先打开机构用户管理.建立需要的机构和部门.这里机构和部门可以理解为共享目录的名称.也可以根据自己需要起名,并不一定是机构和部门的名字. 而 ...

  5. IOS设备启动图像命名规范

  6. openGl从零开始之添加颜色

    OpenGL 支持两种颜色模式:一种是 RGBA模式,一种是 颜色索引模式.无论哪种颜色模式,计算机都必须为每一个像素保存一些数据,即通过每一个像素的颜色,来改变整体图形的颜色.不同的是, RGBA ...

  7. bzoj3064 CPU监控

    今天终于写了一道正常的题 思路是这样的: 1.普通线段树add,set不变,并改为下放标记版本 2.past_addv 记录一个区间内可能的addv值的最大值 3.past_setv 记录一个区间被s ...

  8. git 操作大全

    Git 以下内容整理自廖雪峰的git教程,主要用于个人方便使用git命令 git忽略已经被纳入版本库的文件 使用 git update-index –-skip-worktree [file] 可以实 ...

  9. Chapter13:拷贝控制

    拷贝控制操作:拷贝构造函数.拷贝赋值运算符.移动构造函数.移动赋值运算符.析构函数. 实现拷贝控制操作的最困难的地方是首先认识到什么时候需要定义这些操作. 拷贝构造函数: 如果一个构造函数的第一个参数 ...

  10. PHP相关图书推荐

    PHP和MySQL Web开发(原书第4版) 作      者 [澳] Luke Welling,[澳] Luke Welling 著:武欣 等 译 出 版 社 机械工业出版社 出版时间 2009-0 ...