1.2.1 Milking Cows

       我写的很麻烦……

#include<iostream>
#include<fstream>
#include<algorithm>
using namespace std;
struct Node
{
	int s;
	int e;
}node[5001];

bool cmp(Node a, Node b)//排序 
{
	if( a.s<b.s) return 1;
	else if( a.s==b.s )
	{
		if( a.e<=b.e)
		    return 1;
	}
	
	return 0;
}
int main()
{
	ifstream fin("milk2.in");
    ofstream fout("milk2.out");
    int n, start, temp, maxx1=0, maxx2=0, i, j, count=0;
    fin>>n;
    
    for(i=0; i<n; i++)
       fin>>node[i].s>>node[i].e;
    
    sort(node, node+n, cmp);
 
    for(i=1; i<n-count; i++)//合并 
    {
        if( node[i].s<node[i-1].e )
        {
           if( node[i].e>node[i-1].e)
                node[i-1].e=node[i].e;
           for(j=i+1; j<n; j++)
           {
           	   node[j-1].s=node[j].s;
           	   node[j-1].e=node[j].e;
           }
           count++;
           i--;
        }
        
        else if( node[i].s==node[i-1].e )
        {
       	  
           node[i-1].e=node[i].e;
           for(j=i+1; j<n; j++)
           {
           	   node[j-1].s=node[j].s;
           	   node[j-1].e=node[j].e;
           }
           count++;
           i--;
        }
       
    }
    
    start=node[0].s;
    maxx1=node[0].e-node[0].s;
    for(i=1; i<n-count; i++)
    {
    	maxx1=max(maxx1, node[i].e-node[i].s);
    	
    	maxx2=max(maxx2, node[i].s-node[i-1].e);
    }
         
    fout<<maxx1<<" "<<maxx2<<endl; 
		
} 


你可能感兴趣的:(1.2.1 Milking Cows)