USACO题解milk2 挤牛奶

来源:互联网 发布:汉族是什么人种知乎 编辑:程序博客网 时间:2024/06/09 19:00

类型:模拟.

发现自己做模拟老是漏这漏那的,导致数据总是过不了!!!!!

不给出中文了,一个求数列并集最长子串的模拟.很水  但是我很坑...

思路:对挤奶时间结构体排序,然后递归求最大值;

收获:学会用结构体的排序,qsort的使用; 

错误情况,模拟时的方法漏洞!!!!!!!!!!!!!!!!!!!!!!!!!!坑..

Milking Cows

Three farmers rise at 5 am each morning and head for the barn to milk three cows. The first farmer begins milking his cow at time 300 (measured in seconds after 5 am) and ends at time 1000. The second farmer begins at time 700 and ends at time 1200. The third farmer begins at time 1500 and ends at time 2100. The longest continuous time during which at least one farmer was milking a cow was 900 seconds (from 300 to 1200). The longest time no milking was done, between the beginning and the ending of all milking, was 300 seconds (1500 minus 1200).

Your job is to write a program that will examine a list of beginning and ending times for N (1 <= N <= 5000) farmers milking N cows and compute (in seconds): 
The longest time interval at least one cow was milked. 
The longest time interval (after milking starts) during which no cows were being milked. 
PROGRAM NAME: milk2
INPUT FORMAT

Line 1:The single integerLines 2..N+1:Two non-negative integers less than 1000000, the starting and ending time in seconds after 0500
SAMPLE INPUT (file milk2.in) 

3  300 1000  700 1200  1500 2100    


OUTPUT FORMAT
A single line with two integers that represent the longest continuous time of milking and the longest idle time. 
SAMPLE OUTPUT (file milk2.out)

900 300  

代码:

/*
ID: jun41821
PROG: milk2
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <cstring>
#include <algorithm>
using namespace std;
struct milk{
int x;
int y;
}m[6000];
int cmp(const void *a,const void *b)               //排序
{
    struct milk *c=(milk*)a;
    struct milk *d=(milk*)b;
    return c->x-d->x;
}
int main()
{
    ofstream fout ("milk2.out");
    ifstream fin ("milk2.in");

    int N,i,a,b,T;
    fin>>N;
    T=N;
    i=0;         //奶牛   确定开始和结束     设已知最长的时间  头   尾  maxn长度
    while(T--)          //思路    对 每组数据头  排序  sort 然后模拟//模拟思路 找到最长子串
    {                   //输入的数据    给出信息   头  尾   长度//每次读入一组  判断是否在
        fin>>m[i].x>>m[i].y;        //用数组记录头尾   i为下标
        i++;
    }
    qsort(m,N,sizeof(m[0]),cmp);          //sort  自动升序  如果要改为降序  则自己编写compare函数 调用sort(begin,end,compare)
    int max1=0,max2=0,m1=0,m2=0,max=0,maxn=0;               //记录最大子串   新子串          //试编写一个类  或  结构体的快排;
    m1=m[0].y-m[0].x;
    max1=m1;max=m[0].y;maxn=m[0].y;
    //for(i=0;i<N;i++)
    //cout<<m[i].x<<' '<<m[i].y<<endl;            //打印
    for(i=1;i<N;i++)
    {
        if(maxn>=m[i].x)                        //再是记录空串
        {
            if(m[i].y>=m[i-1].y&&m[i].y>=maxn)
            m1+=m[i].y-maxn;                //少了一种情况
            if(m1>=max1)  {max1=m1;}           //该步求最大子串  一次循环过后得出最大max1
        }
        else
        {
            m1=m[i].y-m[i].x;                   //更新m1
            m2=m[i].x-max;                 //仅在断点判断一次是否最大
            if(m2>max2&&max<=m[i].x) max2=m2;                //换max2的时候进行判断
            if(m1>max1) {max1=m1;}
        }
        if(m[i].y>maxn)maxn=m[i].y;
        if(m[i].y>max)max=m[i].y;
    }
    fout<<max1<<' '<<max2<<endl; //输出结果
    return 0;
}

原创粉丝点击