UVaLive 6834 Shopping (贪心)
题意:给定 n 个商店,然后有 m个限制,去 c 之前必须先去d,问你从0到n+1,最短路程是多少。
析:我们我们要到c,必须要先到d,那么举个例子,2 5, 3 7,如果我们先到5再到2,再到7再到3,那么3-5这个区间我们走了4次,如果我们先到7再到2,
那么就只走了3次,这很明显是最优的,所以我们把能合并的区间都合并起来,然后再一块计算。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define debug puts("+++++")
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 2e5 + 5;
const LL mod = 1e9 + 7;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
inline int gcd(int a, int b){ return b == 0 ? a : gcd(b, a%b); }
inline int lcm(int a, int b){ return a * b / gcd(a, b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
struct Node{
int l, r;
bool operator < (const Node &p) const{
return l < p.l || (l == p.l && r < p.r);
}
};
Node a[505]; int main(){
while(scanf("%d %d", &n, &m) == 2){
for(int i = 0; i < m; ++i) scanf("%d %d", &a[i].l, &a[i].r);
if(!m){ printf("%d\n", n+1); continue; }
sort(a, a+m);
int l = a[0].l, r = a[0].r;
int ans = l;
for(int i = 1; i < m; ++i){
if(a[i].l <= r) r = Max(r, a[i].r);
else{ ans += 3 * (r-l) + a[i].l - r; l = a[i].l, r = a[i].r; }
}
ans += 3 * (r-l) + n - r + 1;
printf("%d\n", ans);
}
return 0;
}
UVaLive 6834 Shopping (贪心)的更多相关文章
- 贪心 UVALive 6834 Shopping
题目传送门 /* 题意:有n个商店排成一条直线,有一些商店有先后顺序,问从0出发走到n+1最少的步数 贪心:对于区间被覆盖的点只进行一次计算,还有那些要往回走的区间步数*2,再加上原来最少要走n+1步 ...
- Codeforces Gym 100803C Shopping 贪心
Shopping 题目连接: http://codeforces.com/gym/100803/attachments Description Your friend will enjoy shopp ...
- UVALive - 6572 Shopping Malls floyd
题目链接: http://acm.hust.edu.cn/vjudge/problem/48416 Shopping Malls Time Limit: 3000MS 问题描述 We want to ...
- UVAlive 2911 Maximum(贪心)
Let x1, x2,..., xm be real numbers satisfying the following conditions: a) -xi ; b) x1 + x2 +...+ xm ...
- uvalive 2911 Maximum(贪心)
题目连接:2911 - Maximum 题目大意:给出m, p, a, b,然后xi满足题目中的两个公式, 要求求的 xp1 + xp2 +...+ xpm 的最大值. 解题思路:可以将x1 + x2 ...
- UVALive - 4225(贪心)
题目链接:https://vjudge.net/contest/244167#problem/F 题目: Given any integer base b ≥ 2, it is well known ...
- UVALive 4850 Installations 贪心
题目链接 题意 工程师要安装n个服务,其中服务Ji需要si单位的安装时间,截止时间为di.超时会有惩罚值,若实际完成时间为ci,则惩罚值为max{0,ci-di}.从0时刻开始执行任务,问惩罚值最大 ...
- UVALive 4863 Balloons 贪心/费用流
There will be several test cases in the input. Each test case will begin with a line with three inte ...
- csu - 1538: Shopping (贪心)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1538 很奇妙的一个题,开始没有思路.问了别人才知道. 题目的意思可以理解成上图中,从0点开始向右走 ...
随机推荐
- 【03】AJAX 向服务器发送请求
AJAX 向服务器发送请求 创建 XMLHttpRequest 对象后,就可以向服务器发送请求了. XMLHttpRequest 对象的 open() 方法和 send() 方法用来向服务器发送请 ...
- Flask(2):登陆验证
装饰器补充: import functools def auth(func): @functools.wraps(func) # 作用:把原函数的原信息封装到 inner 中 def inner(*a ...
- [thrift] thrift基本原理及使用
参考文章RPC 基本原理与 Apach Thrift 初体验 RPC基本原理 RPC(Remote Procedure Call),远程过程调用,大部分的RPC框架都遵循如下三个开发步骤: 1. 定义 ...
- eclipse断点有个斜杠 skip all breakpoints
切换到debug,单击下 skip all breakpoints 即可
- Python循环定时服务功能(相似contrab)
Python实现的循环定时服务功能.类似于Linux下的contrab功能.主要通过定时器实现. 注:Python中的threading.timer是基于线程实现的.每次定时事件产生时.回调完响应函数 ...
- 基于Office 365 无代码工作流分析-数据源的建立!
标准操作步骤 下面整个步骤我们是以嘉昊信息的招聘过程的整个流程为一个场景,整个的流程场景的步骤例如以下: 整个的过程,我们通过Infopath 进行对应的表单流转,然后利用Sharepoint ...
- cocos2d-x 3.6版连连看版本号控制
为了以后的开发和管理.源代码开发必须要使用版本号控制.我们当然选择git来做版本号控制了. 假设你在终端输入git,提示不是一个命令的话.那就说明你的机器没有安装git工具.那就安装一个,百度之有非常 ...
- 【面试】iOS 开发面试题(一)
1. #import 跟#include 又什么差别,@class呢, #import<> 跟 #import""又什么差别? 答:#import是Objectiv ...
- js的调用函数前先执行某语句问题
js的调用函数前先执行某语句问题 标签: web前端面试 2015-09-29 17:48 1455人阅读 评论(0) 收藏 举报 分类: js(5) 版权声明:本文为博主原创文章,未经博主允许不 ...
- Android实现多个倒计时优化与源代码分析
由于之前有个项目需求是须要时时刻去更新UI倒计时,之前想到的,这简单嘛,用计时或者Handler就能够搞定,并且性能也不错,可是需求要ListView,什么,?大量的View都须要,那Handle处理 ...