Codeforces Gym 100803C Shopping 贪心
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 贪心的更多相关文章
- Codeforces Gym 100203E bits-Equalizer 贪心
原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 考虑到交换可以减少一次操作,那么可以 ...
- codeforces Gym 100338E Numbers (贪心,实现)
题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...
- Codeforces GYM 100876 J - Buying roads 题解
Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...
- codeforces 704B - Ant Man 贪心
codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...
- Codeforces Gym 101252D&&floyd判圈算法学习笔记
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
- Codeforces Gym 101190M Mole Tunnels - 费用流
题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...
- Codeforces Gym 101623A - 动态规划
题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...
- 【Codeforces Gym 100725K】Key Insertion
Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...
- 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 ...
随机推荐
- 一个强大的LogParser的UI工具--logparserlizard简介
日志分析,特别是IIS日志,一般人都会想到LogParser工具,的确很强.但是命令行的操作界面令很多非专业的管理人员望而生畏,现在好了,有一个可视化的LogParser的UI工具可以使用了! Log ...
- Chapter14:重载运算符
对于一个运算符函数来说,它或者是类的成员,或者至少含有一个类类型的参数. int operator+(int, int);//错误,不能为int重定义内置运算符 对于一个重载的运算符来说,其优先级和结 ...
- win7中protel99添加元件库
protel打开时默认添加Miscellaneous Devices.lib,点击ADD/REMOVE按钮添加所需要的库时,弹出“文件无法识别”. 点击面板右下方“FIND”按钮,弹出如下对话框时不要 ...
- win7启动出现蓝屏STOP: 0X0000007B
解决方法:开机进BIOS,更改Interface Combination,即硬盘的接口种类,由默认的RAID改成了AHCI,保存,重启,一切正常. 事件过程: 今天开机进入win7,在start wi ...
- C#获取ftp文件最后修改时间
public static DateTime GetFileModifyDateTime(string ftpServerIP,string ftpFolder,string ftpUserID,st ...
- Git 提交后开始自动构建
设定Git仓库的钩子 一般路径为 xxx.git/hooks 参考文档 https://git-scm.com/docs/githooks 修改 post-receive #!/bin/bash wh ...
- Chapter 6 Windows下编译pycaffe
pycaffe 是 Caffe 提供的Python接口,我们知道 Caffe 的整个framework是基于C Plus Plus 来开发的,所以pycaffe 的根本就是提供Python调用Caff ...
- 《学习OpenCV》练习题第四章第二题
#include <highgui.h> #include <cv.h> #pragma comment (lib,"opencv_calib3d231d.lib&q ...
- 主机找不到vmnet1和vmnet8
今天跑程序时,突然发现虚拟机ping不通主机了,返过来可行,防火墙什么的都设置好了,仍然不行,后来发现,在网络和共享中心已经看不到vmnet1和vmnet8了,更改适配器设置也只有本地连接和宽带连接, ...
- java、android 对比两个目录或文件是否是同一个目录或文件的方法
由于软链接及android的外部卡mount方式存在,导致一个文件夹可能同时有两个路径,如: /mnt/sdcard1 /storage/ext_sdcard ,如果通过某种方式(如moun ...