POJ3038 Flying Right
Description
Figuring that they cannot do worse than the humans have, Farmer John's cows have decided to start an airline. Being cows, they decide to cater to the heretofore-untapped market of cows as passengers. They plan to serve the cows who live along the western coast of Lake Michigan. Each morning, they will fly from the northern-most point of the coast southward towards Chicowgo, making many stops along the way. Each evening, they will fly back north to the northern-most point.
They need your help to decide which passengers to carry each day. Each of N (1 <= N <= 10,000) farms numbered 1..N along the coast contains an airport (Farm 1 is northern-most; farm N is southern-most). On this day, K (1 <= K <= 50,000) groups of cows wish to travel.Each group of cows wants to fly from a particular farm to another particular farm. The airline, if it wishes, is allowed to stop and pick up only part of a group. Cows that start a flight, however,must stay on the plane until they reach their destination.
Given the capacity C (1 <= C <= 100) of the airplane and the groups of cows that want to travel, determine the maximum number of cows that the airline can fly to their destination.
They need your help to decide which passengers to carry each day. Each of N (1 <= N <= 10,000) farms numbered 1..N along the coast contains an airport (Farm 1 is northern-most; farm N is southern-most). On this day, K (1 <= K <= 50,000) groups of cows wish to travel.Each group of cows wants to fly from a particular farm to another particular farm. The airline, if it wishes, is allowed to stop and pick up only part of a group. Cows that start a flight, however,must stay on the plane until they reach their destination.
Given the capacity C (1 <= C <= 100) of the airplane and the groups of cows that want to travel, determine the maximum number of cows that the airline can fly to their destination.
Input
* Line 1: Three space-separated integers: K, N, and C
* Lines 2..K+1: Each line contains three space-separated integers S, E, and M that specify a group of cows that wishes to travel. The M (1 <= M <= C) cows are currently at farm S and want to travel to farm E (S != E).
* Lines 2..K+1: Each line contains three space-separated integers S, E, and M that specify a group of cows that wishes to travel. The M (1 <= M <= C) cows are currently at farm S and want to travel to farm E (S != E).
Output
*
Line 1: The maximum number of cows that can be flown to their
destination. This is the sum of the number of cows flown to
their destination on the flight southward in the morning plus the number
of cows flown to their destination on the flight northward in the
evening.
Sample Input
4 8 3
1 3 2
2 8 3
4 7 1
8 3 2
Sample Output
6
Hint
INPUT DETAILS:
Four groups of cows, eight farms, and three seats on the plane.
OUTPUT DETAILS:
In the morning, the flight takes 2 cows from 1->3, 1 cow from 2->8,and 1 cow from 4->7. In the evening, the flight takes 2 cows from 8->3.
Four groups of cows, eight farms, and three seats on the plane.
OUTPUT DETAILS:
In the morning, the flight takes 2 cows from 1->3, 1 cow from 2->8,and 1 cow from 4->7. In the evening, the flight takes 2 cows from 8->3.
Source
正解:贪心
解题报告:
想了很久的DP,发现不会做。其实就是一个xjb贪心题。
考虑一个问题,如果可以上飞机就全部上飞机,而且同等情况下越早下飞机的越优秀。那么这样的话可能会导致后面有更优秀的被挤掉,所以我们需要用下飞机时间更优秀的把那些不够优秀的“踹下去”,所以每次都把飞机上的拖下来重新sort一遍,再上飞机就可以了。
1 //It is made by jump~ 2 #include <iostream> 3 #include <cstdlib> 4 #include <cstring> 5 #include <cstdio> 6 #include <cmath> 7 #include <algorithm> 8 #include <ctime> 9 #include <vector> 10 #include <queue> 11 #include <map> 12 #include <set> 13 using namespace std; 14 typedef long long LL; 15 const int MAXK = 50011; 16 const int MAXC = 150; 17 const int MAXN = 10011; 18 int k,n,C,ans; 19 int cnt,cnt2; 20 int total; 21 struct Niu{ 22 int l,r,w; 23 }cow[MAXK],cow2[MAXK]; 24 struct plane{ 25 int to; 26 int num; 27 }a[MAXK],tmp[MAXK]; 28 29 inline int getint() 30 { 31 int w=0,q=0; char c=getchar(); 32 while((c<'0' || c>'9') && c!='-') c=getchar(); if(c=='-') q=1,c=getchar(); 33 while (c>='0' && c<='9') w=w*10+c-'0', c=getchar(); return q ? -w : w; 34 } 35 inline bool cmpl(Niu q,Niu qq){ return q.l<qq.l; } 36 inline bool cmpr(Niu q,Niu qq){ return q.l>qq.l; } 37 inline bool cmp(plane q,plane qq){ return q.to<qq.to; } 38 inline bool ccmp(plane q,plane qq){ return q.to>qq.to; } 39 40 inline void work(){ 41 k=getint(); n=getint(); C=getint(); int x,y; 42 for(int i=1;i<=k;i++) { 43 x=getint(); y=getint(); 44 if(x<y) cow[++cnt].l=x,cow[cnt].r=y,cow[cnt].w=getint(); 45 else cow2[++cnt2].l=x,cow2[cnt2].r=y,cow2[cnt2].w=getint(); 46 } 47 sort(cow+1,cow+cnt+1,cmpl); 48 int now=1; int lin,remain; 49 for(int i=1;i<=n;i++) { 50 lin=0; 51 for(int j=1;j<=total;j++) { 52 if(a[j].to==i) ans+=a[j].num; 53 else tmp[++lin]=a[j]; 54 } 55 for(int j=now;j<=cnt;j++) { 56 if(cow[j].l==i) tmp[++lin].to=cow[j].r,tmp[lin].num=cow[j].w,now++; 57 else break; 58 } 59 sort(tmp+1,tmp+lin+1,cmp); total=0; remain=C; 60 for(int j=1;j<=lin;j++) { 61 if(tmp[j].num>=remain) { a[++total]=tmp[j]; a[total].num=remain; remain=0; break; } 62 else remain-=tmp[j].num,a[++total]=tmp[j]; 63 } 64 } 65 66 sort(cow2+1,cow2+cnt2+1,cmpr); 67 now=1; total=0; 68 for(int i=n;i>=1;i--) { 69 lin=0; 70 for(int j=1;j<=total;j++) { 71 if(a[j].to==i) ans+=a[j].num; 72 else tmp[++lin]=a[j]; 73 } 74 for(int j=now;j<=cnt2;j++) { 75 if(cow2[j].l==i) tmp[++lin].to=cow2[j].r,tmp[lin].num=cow2[j].w,now++; 76 else break; 77 } 78 sort(tmp+1,tmp+lin+1,ccmp); total=0; remain=C; 79 for(int j=1;j<=lin;j++) { 80 if(tmp[j].num>=remain) { a[++total]=tmp[j]; a[total].num=remain; remain=0; break; } 81 else remain-=tmp[j].num,a[++total]=tmp[j]; 82 } 83 } 84 printf("%d",ans); 85 } 86 87 int main() 88 { 89 work(); 90 return 0; 91 }
本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!