贪心 UVALive 6834 Shopping
2024-09-07 00:25:36
/*
题意:有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 ;
}
最新文章
- 详解Javascript的继承实现
- Spring学习系列(二) 自动化装配Bean
- Java 基础【07】线程同步锁的选择
- swift uiview弹出动画
- form表单类标签汇总
- ionic 嵌套view 的方法
- codeforces 100548F (西安现场赛F题):容斥原理
- juce中的BailOutChecker
- 添加zabbix自动发现(监控多tomcat实例)
- 在线教育平台搭建 预览和models
- Javaoop 遇到的问题
- [UE4]显示落地箭头
- Web前端学习笔记之离线安装npm
- animation3 背景小动画笔记
- Maven项目META-INF文件夹不存在的问题
- JQuery漂浮广告代码
- 有关string stringbuff stringbuild 的区别
- 004_ssh连接慢的问题的解决?
- Luogu 2831 [NOIP2016] 愤怒的小鸟
- 线程池ThreadPool实现异步多线程
热门文章
- 【网络】TCP的流量控制
- unix时间戳(unix timestamp)与北京时间的互转方法
- 1 TypeScript 简介与安装
- 配置server禁止全部非法域名 訪问自己的server
- 【iOS系列】-oc中特有的语法
- 【网站支付PHP篇】thinkPHP集成支付宝支付(担保交易)
- Eclipse 变量点击高亮显示以及自己定义高亮显示颜色
- Datatables 1.10.x在命名上与1.9.x
- (21) java web的struts2框架的使用
- 【Android进度条】三种方式实现自定义圆形进度条ProgressBar