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. linux 常用端口列表

    常见端口表汇总 1 tcpmux TCP Port Service Multiplexer 传输控制协议端口服务多路开关选择器 2 compressnet Management Utility com ...

  2. JS方式调用本地的可执行文件

    看到一个方法,有些用,先存下来,有用的时候再用. 前几天,在IE,FIREFOX中实现了用JS方式调用本地的可执行文件.地址:www.yihaomen.com/article/js/211.htm , ...

  3. CSS框架分析与网站的CSS架构

    框架(framework)是一个基本概念上的结构,用于去解决或者处理复杂的问题,是一种可复用的构架. 我们对CSS框架这个词比较陌生,但对于JavaScript框架就比较熟悉了,比如jQuery 但为 ...

  4. VC远控(三)磁盘显示

    服务端: 发送与接收命令 DWORD WINAPI SLisen(LPVOID lparam) { SOCKET client = (SOCKET)lparam; COMMAND command; w ...

  5. Python在centos下的安装

    1.wget http://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz默认下载到主目录下 2.tar xzf Python-2.6.6.tgz 3 ...

  6. jsoup 获取指定页面的所有链接(需后续完善)

    java代码如下: import java.io.IOException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; impor ...

  7. 数据结构 -- 简单图的实现与遍历 (Java)

    ---恢复内容开始--- 作者版权所有,转载请注明出处,多谢. http://www.cnblogs.com/Henvealf/p/5534071.html 前些天因为某些原因,就试着回想一下图(gr ...

  8. 读取proc信息的可扩展实现

    需求 1. 将内存.线程数等信息注册到zk上进行监控 2. 统计信息,为下一步做负载均衡做准备. 实现 本文只解决问题1. 从网上查询了下,这些信息可以从proc文件系统中获取,如果不知道proc的, ...

  9. Struts – Wildcards example

    Struts wildcards can helps to reduce the repetition in your struts-config.xml file, as long as your ...

  10. python 应用xml.dom.minidom读xml

    xml文件 <?xml version="1.0" encoding="utf-8"?> <city> <name>上海&l ...