UVALive 6834Shopping (贪心)
来源:互联网 发布:js高级程序设计最新 编辑:程序博客网 时间:2024/05/23 13:27
题意:
N个点,给定M个限定先后访问关系,求从0出发全部访问完毕到N+1的最短时间
分析:
画画图发现可以贪心,对于交叉的区间我们总是可以合并成一个大区间一起访问,最后得到一些不交叉的区间
总时间为n+1+每个区间的长度∗2
代码:
//// Created by TaoSama on 2015-12-04// Copyright (c) 2015 TaoSama. All rights reserved.////#pragma comment(linker, "/STACK:1024000000,1024000000")#include <algorithm>#include <cctype>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iomanip>#include <iostream>#include <map>#include <queue>#include <string>#include <set>#include <vector>using namespace std;#define pr(x) cout << #x << " = " << x << " "#define prln(x) cout << #x << " = " << x << endlconst int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;typedef pair<int, int> P;int n, m;P a[1005];int main() {#ifdef LOCAL freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);// freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);#endif ios_base::sync_with_stdio(0); while(scanf("%d%d", &n, &m) == 2) { for(int i = 1; i <= m; ++i) scanf("%d%d", &a[i].first, &a[i].second); sort(a + 1, a + 1 + m); int l = 0, r = 0, ans = n + 1; for(int i = 1; i <= m; ++i) { if(a[i].first <= r) r = max(r, a[i].second); else { ans += r - l << 1; l = a[i].first, r = a[i].second; } } ans += r - l << 1; printf("%d\n", ans); } return 0;}
0 0
- UVALive 6834Shopping (贪心)
- UVALive 6834Shopping(开箱拿钥匙题型)
- UVALive 2757Supermarket(贪心)
- acmicpc-asia-tokyo-regional-contestC-Shopping(贪心)
- uvaLive 4475 - Dinner Hall(贪心)
- UVAlive 2322 Wooden Sticks(贪心)
- UVALive 2088 Entropy (贪心 + 哈夫曼编码)
- uvalive 2322 Wooden Sticks(贪心)
- uvalive 3971 - Assemble(二分搜索 + 贪心)
- UVALive 2322 Wooden Sticks(贪心)
- uvalive 3971 - Assemble(二分搜索 + 贪心)
- UVALIVE 5000 Underwater Snipers(二分+贪心)
- UVALive 3664 Guess(贪心+精度)
- Uvalive 6424 - Russian Dolls (贪心)
- UVALive 7146 Defeat the Enemy (贪心)
- UVALive 3971 Assemble(二分 + 贪心)
- UVALive 6901String Transformation(贪心)
- UVALive 3610 Log Jumping(贪心)
- Activity两种启动方式
- mybaits学习总结
- Use Avahi as a library for Discovering mDNS Service
- css3学习笔记(一)
- [LeetCode306] Additive Number
- UVALive 6834Shopping (贪心)
- UVALive 6832Bit String Reordering (模拟)
- UVALive 6833Miscalculation (模拟)
- UVALive 6837There is No Alternative (MST + LCA)
- UVALive 6838Flipping Parentheses (线段树)
- UVALive 6840Sweet War (博弈dp)
- Angular JS 学习笔记四
- 电子或通信领域当前的主流技术及其社会需求调查
- Angular JS 学习笔记五