贪心 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 ...
随机推荐
- elasticsarch5.4集群安装
越来越多的企业已经采用ELK解决方案来对其公司产生的日志进行分析,笔者最近着手在生产环境部署自己的ELK stack,本文介绍ELK中elasticsearch5.2集群的实现. 一.环境准备 1.系 ...
- 微信小程序实战之 pay(支付页面)
项目目录: 逻辑层: pay.js // pages/pay/pay.js Page({ /** * 页面的初始数据 */ data: { resultType: "", resu ...
- Windows——cmd findstr 字符串查找增强使用说明
在文件中寻找字符串. 复制代码代码如下: FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file] [ ...
- 在云服务器 ECS Linux CentOS 7 下重启服务不再通过 service 操作,而是通过 systemctl 操作
在云服务器 ECS Linux CentOS 7 下重启服务不再通过 service 操作,而是通过 systemctl 操作. 操作说明如下: 1. 查看 sshd 服务是否启动: 看到上述信息就 ...
- thinkphp3.2.3 数据库写入add 方法的一些问题。
最近在做项目中遇到的一个数据操作add()方法,在不开启debug的模式下会漏掉一些字段没写入数据库. 当时并不知道是这个原因,明明在开发的时候都是没问题的,怎么突然出现这个问题,找了好久都没有头绪, ...
- 2016/05/05 smarty ① 登录 ②主页面 ③删除 ④让缩略信息显示完整 (补:增加 修改 )
共 八个页面 ①login.php <?php include("init.inc.php"); $smarty->display("login.html& ...
- iOS开发——高级篇——多线程dispatch_apply
我们知道遍历数组是一个相对耗时的操作,而同时手机的核是越来越多,所以我们需要充分利用iOS多核的作用. 特别是在遍历操作中还有其他耗时操作.像我们平时直接遍历数组的操作 ,i< ,i++){ / ...
- lsblk df
df(1) - Linux manual page http://man7.org/linux/man-pages/man1/df.1.html report file system disk spa ...
- Virtual IP address
https://en.wikipedia.org/wiki/Virtual_IP_address Virtual IP address From Wikipedia, the free encyclo ...
- YTU 2980: 几点了
2980: 几点了 时间限制: 1 Sec 内存限制: 128 MB 提交: 37 解决: 9 题目描述 现有一个Time类可以用来记录时间,请输出Time记录的时间加上s秒后的时间. 只需提交补 ...