【题目链接】

http://poj.org/problem?id=3190

【算法】

将这些牛按开始吃草的时间排序

维护一个数组S,Si表示畜栏i进去的最后一头牛结束吃草的时间,对于每头牛,找任意一个畜栏使得 Si < 这头牛开始吃草时间,将这头牛加入这个畜栏,如果不存在这样的畜栏,则新建一个

这个过程可以用堆来加速

这个算法的正确性可以通过数学归纳法来证明 :

第1头牛选畜栏 : 新建一个畜栏,显然最优

假设前k头牛选完了且是最优方案,那么,轮到第(k+1)头牛选时,找任意一个Si < 第(k+1)头牛开始吃草时间的畜栏都是最优的,不会对后一头牛产生任何影响,如果不存在这样的畜栏,则新建一个,将第(k+1)头牛放入,也是最优的

因此,最终求出的必然是最优解

【代码】

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 50010 int i,n,tot,x,id,pos;
int belong[MAXN];
priority_queue< pair<int,int> > q; struct info
{
int id,l,r;
} a[MAXN]; inline bool cmp(info a,info b) { return a.l < b.l; } int main()
{ scanf("%d",&n);
for (i = ; i <= n; i++)
{
a[i].id = i;
scanf("%d%d",&a[i].l,&a[i].r);
}
sort(a+,a+n+,cmp);
belong[a[].id] = ;
q.push(make_pair(-a[].r,tot = ));
for (i = ; i <= n; i++)
{
x = -q.top().first;
pos = q.top().second;
if (a[i].l > x)
{
q.pop();
q.push(make_pair(-a[i].r,pos));
belong[a[i].id] = pos;
} else
{
q.push(make_pair(-a[i].r,++tot));
belong[a[i].id] = tot;
}
}
printf("%d\n",tot);
for (i = ; i <= n; i++) printf("%d\n",belong[i]); return ; }

【POJ 3190】 Stall Reservations的更多相关文章

  1. 【POJ - 3190 】Stall Reservations(贪心+优先队列)

    Stall Reservations 原文是English,这里直接上中文吧 Descriptions: 这里有N只 (1 <= N <= 50,000) 挑剔的奶牛! 他们如此挑剔以致于 ...

  2. bzoj 2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...

  3. 【链表】BZOJ 2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 382  Solved: 111[Submit][S ...

  4. BZOJ2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 284  Solved: 82[Submit][St ...

  5. BZOJ2293: 【POJ Challenge】吉他英雄

    2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 80  Solved: 59[Submit][Stat ...

  6. BZOJ2287: 【POJ Challenge】消失之物

    2287: [POJ Challenge]消失之物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 254  Solved: 140[Submit][S ...

  7. BZOJ2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 126  Solved: 90[Submit][Sta ...

  8. BZOJ2296: 【POJ Challenge】随机种子

    2296: [POJ Challenge]随机种子 Time Limit: 1 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 114  Solv ...

  9. BZOJ2292: 【POJ Challenge 】永远挑战

    2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 513  Solved: 201[Submit][ ...

随机推荐

  1. 怎么让Eclipse对html和js代码自动提示

    使用eclipse自带的插件,无需另外安装插件,具体步骤如下1.打开eclipse→Windows→Preferences→Java→Editor→Content Assist修改Auto Activ ...

  2. TWaver矢量小试——Android演进路线图

    还有半个多月就到春节了,年底相信很多公司都会进行年度总结以及公司发展状况总结,在这过程中难免会用到RoadMap,在这我们也使用TWaver的矢量部分绘制一个Android系统的发展历程.先看效果:什 ...

  3. 使用JavaScript制作一个好看的轮播图

    目录 使用JavaScript制作出好看的轮播图效果 准备材料 1.图片若干张(包括轮播图和按钮的图片) 2.将按钮的图片应用到按钮上的CSS样式文件 3.实现轮播和点击跳转的JavaScript代码 ...

  4. C++ 实现Golang里的defer

    不多说了,直接贴代码.就一个hpp文件. 1 #include <functional> 2 3 #define CONCAT_(a, b) a##b 4 #define CONCAT(a ...

  5. UVA - 11175 From D to E and Back(思路)

    题目: 思路: 如图E:图中a.b.c.d是有向图D中的顶点,如果ac.bc都指向cd,而ac又指向ce,那bc同样应该有一条指向ce的边不然就不能从图D转换来.所以直接枚举顶点就可以了. 代码: # ...

  6. 60.通过应用层join实现用户与博客的关联

    在构造数据模型的时候,将有关联关系的数据分割为不同的实体,类似于关系型数据库中的模型. 案例背景:博客网站,一个网站可能有多个用户,一个用户会发多篇博客,此时最好的方式是建立users和blogs两个 ...

  7. oldboy python 3.5 week 1

    #!/usr/bin/env python # -*- coding:utf-8 -*- ------------------------------------------- name = inpu ...

  8. Extract local angle of attack on wind turbine blades

    Extract local angle of attack on wind turbine blades Table of Contents 1. Extract local angle of att ...

  9. Codeforces 934D/933B - A Determined Cleanup

    传送门:http://codeforces.com/contest/934/problem/D 给定两个正整数p(p≥1).k(k>1).多项式f(x)的系数的取值集合为{0,1,2,...,k ...

  10. Spring 使用注解注入 学习(四)

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...