本文主要是介绍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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!