这题WA了四次,后来发现不能用所谓的桶排来写

虽然空间上是可以的,但是存在这样一个问题

比如两组数据[15,20]和[21,30]

在20 和 21这两个时刻之间没有milking,但是用桶排的方法写的话只能判断离散的量

不能判断连续的量。

所以这题应该要用【区间合并】的思想来写

不错的题目~

Souce code:

/*
ID: wushuai2
PROG: milk2
LANG: C++
*/
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#define ll long long
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x))) using namespace std;
const int INF = 0x3f3f3f3f; struct sc{
int s,e;
}a[]; bool cmp(struct sc a, struct sc b){
if(a.s != b.s){
return a.s < b.s;
} else{
return a.e > b.e;
}
} int main() {
ofstream fout ("milk2.out");
ifstream fin ("milk2.in");
int i, j, k, t, n, m;
int ans1 = , ans2 = ;
fin >> n;
for(i = ; i < n; ++i){
fin >> a[i].s >> a[i].e;
}
sort(a, a + n, cmp);
for(i = ; i < n - ; ++i){
if(a[i].s == a[i + ].s){
for(j = i + ; j < n - ; ++j){
a[j] = a[j + ];
}
--n;
}
}
for(i = ; i < n - ; ++i){
if(a[i].e >= a[i + ].s){
a[i].e = max(a[i].e, a[i + ].e);
for(j = i + ; j < n - ; ++j){
a[j] = a[j + ];
}
--n;
--i;
}
}
for(i = ; i < n; ++i){
if(a[i].e - a[i].s > ans1){
ans1 = a[i].e - a[i].s;
}
if(i < n - && a[i + ].s - a[i].e > ans2){
ans2 = a[i + ].s - a[i].e;
}
}
fout << ans1 << ' ' << ans2 << endl;
return ;
}

USACO Milk2 区间合并的更多相关文章

  1. POJ 3667 Hotel(线段树 区间合并)

    Hotel 转载自:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html [题目链接]Hotel [题目类型]线段树 ...

  2. HDU 3911 线段树区间合并、异或取反操作

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...

  3. HDU 1540 Tunnel Warfare 平衡树 / 线段树:单点更新,区间合并

    Tunnel Warfare                                  Time Limit: 4000/2000 MS (Java/Others)    Memory Lim ...

  4. HDU 3911 Black And White(线段树区间合并+lazy操作)

    开始以为是水题,结果...... 给你一些只有两种颜色的石头,0为白色,1为黑色. 然后两个操作: 1 l r 将[ l , r ]内的颜色取反 0 l r 计算[ l , r ]内最长连续黑色石头的 ...

  5. POJ 2750 Potted Flower (线段树区间合并)

    开始懵逼找不到解法,看了网上大牛们的题解才发现是区间合并...  给你n个数形成一个数列环,然后每次进行一个点的修改,并输出这个数列的最大区间和(注意是环,并且区间最大只有n-1个数) 其实只需要维护 ...

  6. ACM: Hotel 解题报告 - 线段树-区间合并

    Hotel Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description The ...

  7. HDU 1540 Tunnel Warfare(线段树+区间合并)

    http://acm.hdu.edu.cn/showproblem.php?pid=1540 题目大意:抗日战争期间进行地道战,存在n个村庄用地道连接,输入D表示破坏某个村庄(摧毁与其相连的地道, 包 ...

  8. POJ 2892 Tunnel Warfare(线段树单点更新区间合并)

    Tunnel Warfare Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7876   Accepted: 3259 D ...

  9. [HDOJ3308]LCIS(线段树,区间合并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 题意:给定n个数,两个操作: U A B:将位置A的数值改成B Q A B:查询[A,B]内最长 ...

随机推荐

  1. php 不能同时提交form

    注意:提交form到相应的页面时,不能在form中嵌套一个form,否则,不能提交

  2. java 简单的词法分析

    package com.seakt.example; import java.io.*; import java.lang.String; public class J_Scanner { publi ...

  3. inlay检验标准

    Inlay 检验标准 检验条件及要求 正常的 40W 日光灯下距离被检物 50cm,眼睛距离被检物 30cm,与被检物呈 45 度角,目视检 使用强光灯箱透视其内部结构 适用范围 Inlay 中料 检 ...

  4. android 大小写转换

    private void toUpperCase(byte[] data, int start, int len) { int end = start + len; int dist = 'A' - ...

  5. ORACLE中date类型字段的处理

    (1)在英文版本的ORACLE中默认日期格式为'DD-MON-YY',例如'01-JAN-98' 在汉化的中文版本中ORACLE默认日期格式为'日-月-年',例如'21-8月-2003'或'21-8月 ...

  6. 《UNIX环境高级编程》笔记--UNIX标准化及实现

    1.UNIX标准化 1.1.ISO C 1989 年后期,C程序设计语言的ANSI(American National Standards Institute) 标准X3. 15 9-1989得到批准 ...

  7. 【从0開始Tornado建站】群聊

    群聊的前台主要代码: {%block content%} <!--<p class='text-success h3'>測试版本号,每天凌晨4:00清水,enjoy it~~:-)& ...

  8. Orchard 添加搜索栏

    Orchard 提供索引和搜索的功能. 索引功能需要开启 Indexing 模块, 同时我们要开启Lucene 模块(做实际检索工作的东西). 然后还要开启Search模块(调用Lucene 查询然后 ...

  9. MVC+ADO模式

    MVC+DAO设计模式 博客分类: Java Java WEB开发   MVC+DAO设计模式 本文摘自:http://www.paper.edu.cn    基于MVC+DAO设计模式的Struts ...

  10. 【转载】Google Analytics 使用图文全攻略

    转载自:Google Analytics 使用图文全攻略 最近一段时间,因为工作的需要,小励使用GA(GA是Google Analytics的简称)比较频繁,所以花时间研究了一下,从不太了解到会使用( ...