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. Solaris系统管理(二)资源管理与网络配置

    上一篇主要总结了Solaris安装后需要进行的一些设置,如ssh,pkgutil管理依赖,vim安装. 这一篇将会对Solaris资源管理与网络配置进行总结. 四 Solaris 系统管理 1,查询总 ...

  2. strcpy()的实现

    看到有一个博客讲的比平时理解的更深入,mark一下:strcpy函数的实现 这里只写平时理解的,三个要点: //strcpy自己实现 char *strcpy(char *dest, const ch ...

  3. 【加解密】关于DES加密算法的JAVA加密代码及C#解密代码

    JAVA加密: package webdomain; import java.security.Key; import java.security.spec.AlgorithmParameterSpe ...

  4. signal()函数

    转自:http://blog.csdn.net/sddzycnqjn/article/details/7285760 1. 信号概念 信号是进程在运行过程中,由自身产生或由进程外部发过来的消息(事件) ...

  5. AudioManager --- generateAudioSessionId

    AudioManager中的generateAudioSessionId方法介绍: 1.方法声明 pubilc void generateAudioSessionId(); 2.API描述 返回一个不 ...

  6. Zookeeper Hello World

    1.Zookeeper的安装使用 在官网上下载zk的安装包(http://labs.renren.com/apache-mirror/zookeeper/),解压后cd到zk的目录下. 单机版安装方法 ...

  7. Yii 1.1 DAO绑定参数实例

    <?php $sql = "SELECT * FROM admin_user WHERE user_name=:uname AND password LIKE :c"; $c ...

  8. S3

    S3是Amazon EMR的一部分,它提供了一些Wikipedia的浏览统计数据,这些浏览数据的格式便于Spark测试.

  9. 全文索引之nutch与hadoop(转)

    原文:http://blog.csdn.net/chaofanwei/article/details/39476535 全文索引-lucene,solr,nutch,hadoop之lucene 全文索 ...

  10. Fast-paced Multiplayer

    http://www.gabrielgambetta.com/fpm1.html —————————————————————————————————————————————————————— Fast ...