milk2解题报告 —— icedream61 博客园(转载请注明出处)
------------------------------------------------------------------------------------------------------------------------------------------------
【题目】
  N个农民,每个农民从L[i]到R[i]时间给奶牛挤奶。问最长的一直有人挤奶的时间,和最长的没有人挤奶的时间。
【数据范围】
  1<=N<=5000
  0<=L[i],R[i]<=1,000,000
【输入样例】
  3
  300 1000
  700 1200
  1500 2100
【输出样例】
  900 300
------------------------------------------------------------------------------------------------------------------------------------------------
【分析】
  先对N个农民按照L[i]排序,然后顺序扫。
  我用Sum[0]记录最长一直没人挤奶的时间,用Sum[1]记录最长一直有人挤奶的时间。
  用Lt记录当前所考虑的开始时间,Rt记录当前所考虑的结束时间。
  “a←b”表示用b更新a,即 if(b>a) a=b;
  开始 Lt=L[1]; Rt=R[1];
  如果 R[i]<L[i+1],说明i和i+1时间分开了,那么 Sum[0]←L[i+1]-Rt; Sum[1]←Rt-Lt;
  否则,说明i+1可以和i的挤奶时间连上,那么 Rt←R[i+1];
  算法描述完毕,注意别写错就好。
------------------------------------------------------------------------------------------------------------------------------------------------
【总结】
  一遍AC。
  复习了下快排~
  还有由于直接上手写的代码,代码不漂亮:
    1.bool变量p没用上
    2.L和R如果定义成Time类型,和T[]保持一致就更好看了

------------------------------------------------------------------------------------------------------------------------------------------------

【代码】

 /*
ID: icedrea1
PROB: milk2
LANG: C++
*/ #include <iostream>
#include <fstream>
using namespace std; int N;
struct Time { int b,g; } T[+]; void qsort(Time T[],int l,int r)
{
if(l>=r) return; int i=l,j=r,x=T[(l+r)>>].b;
while(true)
{
while(T[i].b<x) ++i;
while(T[j].b>x) --j;
if(i>j) break;
Time t=T[i]; T[i]=T[j]; T[j]=t;
++i; --j;
}
qsort(T,l,j); qsort(T,i,r);
} void change(int &a,int b)
{
if(b>a) a=b;
} int main()
{
ifstream in("milk2.in");
ofstream out("milk2.out"); in>>N;
for(int i=;i<=N;++i) in>>T[i].b>>T[i].g; qsort(T,,N); bool p; // Ture挤奶,F空闲
int L,R; // 当前时间段
int Sum[]={,}; // 最大值——0空闲时间,1挤奶时间 L=T[].b; R=T[].g;
for(int i=;i<=N;++i)
{
if(R<T[i+].b) // 挤奶时间完结
{
change(Sum[],T[i+].b-R);
change(Sum[],R-L);
L=T[i+].b; R=T[i+].g;
}
else // 更新挤奶时间
{
change(R,T[i+].g);
}
}
change(Sum[],R-L); out<<Sum[]<<" "<<Sum[]<<endl; in.close();
out.close();
return ;
}

USACO Section1.2 Milking Cows 解题报告的更多相关文章

  1. USACO Section1.5 Prime Palindromes 解题报告

    pprime解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...

  2. USACO Section1.5 Superprime Rib 解题报告

    sprime解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...

  3. USACO Section1.5 Number Triangles 解题报告

    numtri解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...

  4. USACO Section1.4 Arithmetic Progressions 解题报告

    ariprog解题报告 —— icedream61 博客园(转载请注明出处)-------------------------------------------------------------- ...

  5. USACO Section1.3 Combination Lock 解题报告

    combo解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...

  6. USACO Section1.3 Prime Cryptarithm 解题报告

    crypt1解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...

  7. USACO Section1.3 Barn Repair 解题报告

    barn1解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...

  8. USACO Section1.3 Mixing Milk 解题报告

    milk解题报告 —— icedream61 博客园(转载请注明出处)----------------------------------------------------------------- ...

  9. USACO Section1.2 Palindromic Squares 解题报告

    palsquare解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------ ...

随机推荐

  1. IOS 单例模式(非ARC)

    singleton_h :连接字符串和参数 // ## : 连接字符串和参数 #define singleton_h(name) + (instancetype)shared##name; #defi ...

  2. IOS NSThread 线程间通信

    @interface HMViewController () @property (weak, nonatomic) IBOutlet UIImageView *imageView; @end @im ...

  3. 四大CPU体系结构ARM、X86/Atom、MIPS、PowerPC

    http://blog.csdn.net/wangjianno2/article/details/52140936 RISC(reduced instruction set computer,精简指令 ...

  4. iterm2配置项

    1. 启动终端Terminal2. 进入当前用户的home目录    输入cd ~3. 创建.bash_profile    输入touch .bash_profile 在导入并应用完颜色方案之后,通 ...

  5. .net core自定义特性操作

    最近移植之前写的几个类,发现特性操作发生了一些改变. 直接看代码,建立表和字段特性类,添加一个用户表,设置好特性. using System; namespace TestDemo { /// < ...

  6. orale 10g和11g中的自动统计任务

    orale 10g和11g中的自动统计任务 博客分类:  数据库相关/oracle   1)  先来看下oracle 10g中的自动统计任务的问题. 从Oracle Database 10g开始,Or ...

  7. react的 react-router使用

    官方API:https://reacttraining.com/react-router/web/api/BrowserRouter; React Router 安装命令如下. 使用时,路由器Rout ...

  8. 牛客小白月赛2 H 武 【Dijkstra】

    链接:https://www.nowcoder.com/acm/contest/86/H来源:牛客网 题目描述 其次,Sεlιнα(Selina) 要进行体力比武竞赛. 在 Sεlιнα 所在的城市, ...

  9. 10^9以上素数判定,Miller_Rabin算法

    #include<iostream> #include<cstdio> #include<ctime> #include<string.h> #incl ...

  10. Spring 中IOC(控制反转)&& 通过SET方式为属性注入值 && Spring表达式

    ### 1. Spring IoC IoC:Inversion of control:控制反转:在传统开发模式下,对象的创建过程和管理过程都是由开发者通过Java程序来实现的,操作权在开发者的Java ...