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 ...
随机推荐
- 将a、b的值进行交换,并且不使用任何中间变量
方法1:用异或语句 a = a^b; b = a^b; a = a^b; 注:按位异或运算符^是双目运算符,其功能是参与运算的两数各对应的二进制位相异或,当对应的二进制相异时,结果为1.参与运算数仍以 ...
- 使用clipboard.js复制页面内容到剪切板
最近在做一个的智能客服Web端浏览器应用,其中有一项需求是客户在获取系统返回的 答案后点击“复制答案”按钮将答案复制到系统剪切板.本以为这是一个小case,但是发现如果 要对各种主流浏览器都有良好的兼 ...
- ChromePHP - Chrome浏览器下的PHP debug工具
一款 Chrome 下用来配合调试 PHP 的工具,看官方介绍应该和 FirePHP 有异曲同工的.喜欢用Chrome 的PHPer 可以尝试一下. 官方网站:http://www.chromephp ...
- iframe根据子页面自动调整大小
//iframe高度自适应 function IFrameReSize(iframename) { var pTar = document.getElementById(iframename); if ...
- C语言实现strlen
strlen: #ifndef STRLEN_H #define STRLEN_H #include <stdio.h> // 参考微软的写法 int cat_strlen(const c ...
- Java Web高性能开发(二)
今日要闻: 性价比是个骗局: 对某个产品学上三五天个把月,然后就要花最少的钱买最多最好的东西占最大的便宜. 感谢万能的互联网,他顺利得手,顺便享受了智商上的无上满足以及居高临下的优越感--你们一千块买 ...
- iOS打开手机QQ与指定用户聊天界面
开发中遇到一个联系客服qq的需求,找到这么一个实现方法,先记录下来.大概的原理就是,iOS启动第三方应用是采用schema模式的,这有点像url,打开不同的界面使用不同的地址.但这个url怎么得来的还 ...
- [POJ] #1002# 487-3279 : 桶排序/字典树(Trie树)/快速排序
一. 题目 487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 274040 Accepted: 48891 ...
- 转】用Maven构建Hadoop项目
原博文出自于: http://blog.fens.me/hadoop-maven-eclipse/ 感谢! 用Maven构建Hadoop项目 Hadoop家族系列文章,主要介绍Hadoop家族产品 ...
- 使用DNSPod来处理网站的均衡负载(转)
add by zhj:配置倒是蛮简单的,其实就是把域名与多个IP进行关联,在数据库中实现这个应该也是蛮简单的. 原文:http://kb.cnblogs.com/page/75571/ 首先介绍下DN ...