51nod 1428 && bzoj 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
来源:互联网 发布:榕基软件 编辑:程序博客网 时间:2024/06/06 18:43
1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 951 Solved: 550
[Submit][Status][Discuss]
Description
有N头牛,每头牛有个喝水时间,这段时间它将专用一个Stall 现在给出每头牛的喝水时间段,问至少要多少个Stall才能满足它们的要求
Input
* Line 1: A single integer, N
* Lines 2..N+1: Line i+1 describes cow i's milking interval with two space-separated integers.
Output
* Line 1: The minimum number of stalls the barn must have.
* Lines 2..N+1: Line i+1 describes the stall to which cow i will be assigned for her milking period.
Sample Input
5
1 10
2 4
3 6
5 8
4 7
Sample Output
4
经典题了
把线段转化成一个入点和一个出点,这样存下2*n个点,之后按坐标从小到大给这2*n个点排序,之后O(n)遍历一遍就好,遇到入点sum++, 遇到出点sum--,遍历时最大的sum便是答案,算上排序复杂度稳定nlogn(注意:排序时坐标相同的点出点优先,出点不在区间内)
#include<stdio.h>#include<algorithm>using namespace std;typedef struct{int x;int flag;}Point;Point s[100005];bool comp(Point a, Point b){if(a.x<b.x || a.x==b.x && a.flag<b.flag)return 1;return 0;}int main(void){int n, i, a, b, sum, ans;scanf("%d", &n);for(i=1;i<=n;i++){scanf("%d%d", &a, &b);s[i*2-1].x = a, s[i*2-1].flag = 1;s[i*2].x = b+1, s[i*2].flag = -1;}n *= 2;sort(s+1, s+n+1, comp);sum = ans = 0;for(i=1;i<=n;i++){sum += s[i].flag;ans = max(ans, sum);}printf("%d\n", ans);return 0;}
阅读全文
1 0
- 51nod 1428 && bzoj 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
- BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
- BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
- 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
- 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
- bzoj1651[Usaco2006 Feb]Stall Reservations 专用牛棚
- [BZOJ1651] [Usaco2006 Feb]Stall Reservations 专用牛棚
- bzoj1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
- bzoj1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
- BZOJ1651--[Usaco2006 Feb]Stall Reservations 专用牛棚
- [Usaco2006 Feb]Stall Reservations 专用牛棚
- [bzoj1651][Usaco2006 Feb]Stall Reservations 专用牛棚
- bzoj 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 (线段树)
- |BZOJ 1651|差分序列|[Usaco2006 Feb]Stall Reservations 专用牛棚
- [BZOJ1651][Usaco2006 Feb]Stall Reservations 专用牛棚(线段树)
- 1651: [Usaco2006 Feb]专用牛棚[经典] 多个区间不相交
- BZOJ1651Stall Reservations 专用牛棚
- Stall Reservations
- Fileupload组件parseRequest方法问题
- 对象的序列化,ObjectOutputStream
- 运算符和分支语句
- JVM的内存区域划分
- 排序算法-选择排序之堆排序
- 51nod 1428 && bzoj 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
- 通过MySQL自动同步刷新Redis
- 合并两个有序的单链表
- CodeForces
- Dubbo分布式服务框架简介
- java学习初探6之继承 方法覆盖 多态
- 初识微信小程序第一弹
- 应用程序打包和发布
- Maven中的依赖传递