思路:考虑一个任务的“最晚开始时间”即任务的起止时间的中点( startTime + (endTime-startTime)/2 ),因为如果该任务过了最晚开始时间仍然没有开始执行,那么肯定无法完成该任务,所以按照最晚开始时间进行排序。
代码一:
#include<iostream> #include<algorithm> #include<vector> using namespace std; struct Time{ Time(){} Time(int s,int e,int m){ startTime = s; endTime = e; midTime = m; } int startTime; int endTime; int midTime; }; bool cmp(Time a, Time b){ return a.midTime < b.midTime; } int main(){ int n,i,now; cin>>n; vector<Time> vec(n); int start,end,mid; for(i=0; i<n; i++){ cin>>start>>end; mid = start + (end - start)/2; vec[i].startTime = start; vec[i].endTime = end; vec[i].midTime = mid; } sort(vec.begin(), vec.end(), cmp); bool attend = true; now = 0; for(i=0; i<vec.size(); i++){ if( now < vec[i].startTime ){ now = vec[i].startTime; } now += (vec[i].endTime-vec[i].startTime)/2 + 1 ; if(now > vec[i].endTime){ attend = false; break; } } if(attend){ cout<<"YES"<<endl; }else{ cout<<"NO"<<endl; } return 0; }
#include <iostream> #include<algorithm> using namespace std; struct point { int s, t; } p[10100]; int now, i, n; bool boo; bool cmp(point a, point b) { return (a.t + a.s) < (b.t + b.s); } int main() { //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d%d", &p[i].s, &p[i].t); } boo = true; sort(p, p + n, cmp); now = 0; for (i = 0; i < n; i++) { if (now < p[i].s) now = p[i].s; now += (p[i].t - p[i].s) / 2 + 1; if (now > p[i].t) boo = false; } if (boo) printf("YES\n"); else printf("NO\n"); return 0; }
原文地址:http://blog.csdn.net/realxuejin/article/details/42120843