zoukankan      html  css  js  c++  java
  • POJ 3038 Flying Right

    Flying Right
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 1270   Accepted: 464

    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.

    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).

    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.

    Source

    //开始没看清题目、还以为是不能拆开的,原来一群牛可以选几头就可以了
    //然后就贪心了,尽量不要选去远的牛、因为那样占据空间比较久
    #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #include <cmath> #include <vector> #define N 10004 #define Size 500004 using namespace std; struct node { int from,to,person; bool operator<(const node&a)const { return from<a.from; } }to[Size],back[Size]; int off[N]; int cnt; int C; void del(node *go,int &nn) { memset(off,0,sizeof(off)); int i; int c=0,tp; int t1; priority_queue<int>qd; priority_queue<int,vector<int>,greater<int> >qx; for(i=0;i<nn;i++) { qd.push(go[i].to); qx.push(go[i].to); off[go[i].to]+=go[i].person; while(qx.top()<=go[i].from) { c-=off[qx.top()]; off[qx.top()]=0;//尼玛(忍不住了),少了这句,从早上WA到现在 qx.pop(); } c+=go[i].person; cnt+=go[i].person; if(c>C) { tp=c-C;cnt-=tp;c=C; while(tp) { t1=qd.top(); if(off[t1]>tp) {off[t1]-=tp;tp=0;} else { tp-=off[t1]; off[t1]=0; qd.pop(); } } } } } int main() { int S,E,M; int sn,en; int K,n; scanf("%d%d%d",&K,&n,&C); cnt=0;sn=en=0; while(K--) { scanf("%d%d%d",&S,&E,&M); if(S<E) { to[sn].from=S; to[sn].to=E;to[sn++].person=M; } else { back[en].from=E; back[en].to=S;back[en++].person=M; } } sort(to,to+sn); del(to,sn); sort(back,back+en); del(back,en); printf("%d\n",cnt); return 0; }
  • 相关阅读:
    java面向对象day01
    找工作——JVM内存管理
    找工作——多线程
    找工作-——网络IO
    找工作--volatile
    找工作——异常处理
    找工作--设计模式
    Sqoop安装
    NIO
    Hadoop源码编译
  • 原文地址:https://www.cnblogs.com/372465774y/p/2628361.html
Copyright © 2011-2022 走看看