贪心 UVALive 6834 Shopping
/*
题意:有n个商店排成一条直线,有一些商店有先后顺序,问从0出发走到n+1最少的步数
贪心:对于区间被覆盖的点只进行一次计算,还有那些要往回走的区间步数*2,再加上原来最少要走n+1步就是答案了
详细解释:http://blog.csdn.net/u013625492/article/details/45640735
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std; const int MAXN = 1e3 + ;
const int INF = 0x3f3f3f3f;
struct A
{
int l, r;
}a[MAXN];
struct B
{
int l, r;
}b[MAXN]; bool cmp(A x, A y) {return x.l < y.l;} int main(void) //UVALive 6834 Shopping
{
// freopen ("C.in", "r", stdin); int n, m;
while (scanf ("%d%d", &n, &m) == )
{
int cnt = ;
for (int i=; i<=m; ++i)
{
int u, v; scanf ("%d%d", &u, &v);
if (u > v) continue;
a[++cnt].l = u; a[cnt].r = v;
}
sort (a+, a++cnt, cmp); int l = a[].l; int r = a[].r; int tot = ;
for (int i=; i<=cnt; ++i) //区间覆盖
{
if (r < a[i].l)
{
b[++tot].l = l; b[tot].r = r;
l = a[i].l; r = a[i].r;
}
else r = max (r, a[i].r);
if (i == cnt) {b[++tot].l = l; b[tot].r = r;}
} int ans = n + ;
for (int i=; i<=tot; ++i) ans += * (b[i].r - b[i].l);
printf ("%d\n", ans);
} return ;
}
贪心 UVALive 6834 Shopping的更多相关文章
- UVaLive 6834 Shopping (贪心)
题意:给定 n 个商店,然后有 m个限制,去 c 之前必须先去d,问你从0到n+1,最短路程是多少. 析:我们我们要到c,必须要先到d,那么举个例子,2 5, 3 7,如果我们先到5再到2,再到7再到 ...
- 贪心 UVALive 6832 Bit String Reordering
题目传送门 /* 贪心:按照0或1开头,若不符合,选择后面最近的进行交换.然后选取最少的交换次数 */ #include <cstdio> #include <algorithm&g ...
- UVALive - 6572 Shopping Malls floyd
题目链接: http://acm.hust.edu.cn/vjudge/problem/48416 Shopping Malls Time Limit: 3000MS 问题描述 We want to ...
- LA 6834 Shopping
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- Codeforces Gym 100803C Shopping 贪心
Shopping 题目连接: http://codeforces.com/gym/100803/attachments Description Your friend will enjoy shopp ...
- Gym 101194D / UVALive 7900 - Ice Cream Tower - [二分+贪心][2016 EC-Final Problem D]
题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...
- UVALive 7147 World Cup(数学+贪心)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- UVALive 7146 Defeat the Enemy(贪心+STL)(2014 Asia Shanghai Regional Contest)
Long long ago there is a strong tribe living on the earth. They always have wars and eonquer others. ...
- UVaLive 6609 Meeting Room Arrangement (贪心,区间不相交)
题意:给定 n 个区间,让你选出最多的区间,使得每个区间不相交. 析:贪心题,贪心策略是按右端点排序,然后按着选即可. 代码如下: #pragma comment(linker, "/STA ...
随机推荐
- Ubuntu系统U盘安装以及降内核
由于项目需要,要用U盘制作一个Linux系统,支持EFI启动,并且内核版本要求是2.6.35.6.所以在选系统的时候,就必须要选安装文件里面带有EF I目录,并且该目录下面有BOOTx64.EFI和g ...
- O2O助汪峰成功逆袭,汪峰最终上头条了
8月2日七夕情人节,汪峰<峰暴来临>演唱会在鸟巢10万人体育场唱响,各大报纸.站点娱乐板块并没有等来汪峰向国际章求婚的"头条",只是,与乐视合作现场演出+付费直播的O2 ...
- MySQL学习系列之触发器
触发器简介 触发器作用: 监控某种事件并触发某种动作 触发语法: CREATE TRIGGER trigger_name trigger_event ON tbl_name FOR EACH ROW ...
- java SE基础(Collection接口概述)
Collection接口相关集成关系例如以下图 1. 关于可迭代接口(Iterable) 可迭代接口仅包括一个方法,返回一个在一组T类型元素上进行迭代的迭代器: public ...
- 一个关于MYSQL IFNULL的用法
select a.receiveID,(a.num - IFNULL(b.num,0)) as num from (SELECT num,receiveID from dog_giftnumrecor ...
- 24Web前端架构
近来都是接触前端.所以学多点这方面的东西,虽说有实战到项目里面去了,但可能还没走到所谓正确的道路上去.欢迎交流. 转载请说明来着:http://blog.csdn.net/wowkk -------- ...
- 2016/3/27 分页 共X条数据 本页x条 本页从x-y条 x/y页 首页 上一页 123456 下一页 末页 pagego echo $page->fpage(7,6,5,4,3,2,1,0);
显示效果: fpage.class.php <?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; / ...
- PyTorch 60 分钟入门教程
PyTorch 60 分钟入门教程:PyTorch 深度学习官方入门中文教程 http://pytorchchina.com/2018/06/25/what-is-pytorch/ PyTorch 6 ...
- 正则表达式ab?c匹配的字符串是?(B)
A:abcd B:abc C:abcbc D:aEbc
- MVC框架显示层——Velocity技术
Velocity,名称字面翻译为:速度.速率.迅速,用在Web开发里,用过的人可能不多,大都基本知道和在使用Struts,到底Velocity和Struts(Taglib和Tiles)是如何联系?在技 ...