WOJ1072-Order

来源:互联网 发布:淘宝客佣金查询插件 编辑:程序博客网 时间:2024/05/18 02:55

Knuthocean is a coach of WHUACM team now. One day, he wants to have a meeting with the team members, so he should order a classroom for the meeting. Classrooms in WHU are lacking, so the administrator must accept some orders and refuse the others to optimize 

the classroom's use efficency. If one order is accepted, the time between the start time and the finish time of this order becomes active. 
There can be at most one order during each time slice. Because of your capacity in programming, the administator asks you to 
find a method to maximize the total active time. Your task is like that:read the orders and calculate the maximal length of the active time.

输入格式

Standard input will contain multiple test cases. For each test case, the first line is a number N,(N<=10000) followed by N lines.
Each line contains two integers, p and k.(1<=p<=k<=300000), where p is the start time and k is the finish time of an order.

输出格式

For each test case, you should output one line containing the maximal length of the active time.

样例输入

41 23 51 44 5

样例输出

5

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define MAX 10001int N,p,k;int dp[MAX];int T[MAX];typedef struct nt {int start,end,length;bool operator < (const nt a)const {if(start == a.start)return a.end > end;elsereturn start < a.start;}} TS;TS node[MAX];int main() {while(scanf("%d", &N) != EOF) {for(int i = 0; i < N; i ++) {scanf("%d %d",&node[i].start, &node[i].end);node[i].length = node[i].end - node[i].start + 1;}stable_sort(node,node+N);memset(dp,0,sizeof(dp));dp[0] = node[0].length;T[0] = node[0].length;for(int i = 1; i < N; i ++) {int s = i - 1;T[i] = node[i].length;int dT = 0;for(; s >=0; s --) {if(node[i].start > node[s].end)dT = max(dT ,T[s]);}T[i] += dT;dp[i] = max(dp[i-1],T[i]);}printf("%d\n",dp[N - 1]) ;}return 0;}