poj 3080 Flying Right(贪心+优先队列)

5 篇文章 0 订阅
3 篇文章 0 订阅

【题目大意】:有一架坐位固定的飞机,每天早上从1号点飞到N号点,晚上从N号点飞回上号点,中途有些点会有人上飞机,在保证不超载的情况下求一天下来,能载的最多乘客数。


【解题思路】:对于每一个起飞站点,尽可能的放入人,遇到放不下的情况就踢除掉最远的人。枚举站点并枚举每个站点为起点可到达的点进行人数的修改。

当人数超过规定值,则利用优先队列的性质贪心去掉最远的点。


【代码】:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <cctype>
#include <map>
#include <iomanip>

using namespace std;

#define eps 1e-8
#define pi acos(-1.0)
#define inf 1<<30
#define linf 1LL<<60
#define pb push_back
#define lc(x) (x << 1)
#define rc(x) (x << 1 | 1)
#define lowbit(x) (x & (-x))
#define ll long long

struct Node{
    int x,y,v;
    Node (){}
    Node (int _x,int _y,int _v){
        x=_x,y=_y,v=_v;
    }
    const bool operator <(const Node &b)const{
        return y<b.y;
    }
};

int n,c,k;

vector<Node> a[10010],b[10010];

int aa[10010],bb[10010];

int solve_ns(){
    priority_queue<Node> que;
    int now=0,ans=0;
    for(int i=1; i<=n; i++){
        ans+=aa[i];
        now-=aa[i];
        for (int j=0; j<a[i].size(); j++){
            now+=a[i][j].v;
            que.push(a[i][j]);
        }
        while (now>c){
            Node tmp=que.top();
            que.pop();
            if(now-c>=tmp.v){
                now-=tmp.v;
                aa[tmp.y]-=tmp.v;
            }
            else {
                aa[tmp.y]-=(now-c);
                tmp.v-=now-c;
                que.push(tmp);
                now=c;
            }
        }
    }
    return ans;
}

int solve_sn(){
    priority_queue<Node> que;
    int now=0,ans=0;
    for(int i=1; i<=n; i++){
        ans+=bb[i];
        now-=bb[i];
        for (int j=0; j<b[i].size(); j++){
            now+=b[i][j].v;
            que.push(b[i][j]);
        }
        while (now>c){
            Node tmp=que.top();
            que.pop();
            if(now-c>=tmp.v){
                now-=tmp.v;
                bb[tmp.y]-=tmp.v;
            }
            else {
                bb[tmp.y]-=(now-c);
                tmp.v-=now-c;
                que.push(tmp);
                now=c;
            }
        }
    }
    return ans;
}


int main() {
    while (~scanf("%d%d%d",&k,&n,&c)){
        int u,v,w;
        for(int i=1; i<=k; i++){
            scanf("%d%d%d",&u,&v,&w);
            if(v>u){
                a[u].push_back(Node(u,v,w));
                aa[v]+=w;
            }
            else {
                b[n-u+1].push_back(Node(n-u+1,n-v+1,w));
                bb[n-v+1]+=w;
            }
        }
        int ans=0;
        ans+=solve_ns();
        ans+=solve_sn();
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值