Flying Right POJ - 3038

有一条从南到北的航线,航线上有N个机场1-n从南到北分布,每天早上飞机从1飞到n,傍晚从n飞到1。有k组乘客,他们数量为M[k],从S飞到E,飞机上只有C个座位,计算每天飞机最多能拉多少乘客

贪心可以解决这个问题~(我一开始一直在想dp(lll¬ω¬))

每个站点让所有乘客都上飞机,如果此时超载了,那么就让目的地离当前站点最远的乘客下飞机。可以用优先队列来维护。

emmm这个代码来自 https://blog.csdn.net/severus_qin/article/details/18956647,侵删

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <iostream>
 5 #include <queue>
 6 #include <vector>
 7 using namespace std;
 8 const int maxn = 10010;
 9 struct event{
10     int t, c;
11     event(){}
12     event(int a, int b) : t(a), c(b){}
13     bool operator < (const event &rhs)const{
14         return t < rhs.t;
15     }
16 };
17 vector<event> v1[maxn], v2[maxn];
18 int k, n, c, num[2][maxn], ans;
19 int work(vector<event> vv[], int k){
20     priority_queue<event> que;
21     int res = 0, tmp = 0;
22     for (int i = 1; i <= n; i++){
23         res += num[k][i];
24         tmp -= num[k][i];
25         for (int j = 0; j < vv[i].size(); j++){
26             tmp += vv[i][j].c;
27             que.push(vv[i][j]);
28         }
29         while(tmp > c){
30             event tt = que.top(); que.pop();
31             if (tmp - c >= tt.c){
32                 tmp -= tt.c;
33                 num[k][tt.t] -= tt.c;
34             }else{
35                 num[k][tt.t] -= (tmp - c);
36                 tt.c -= (tmp - c);
37                 tmp = c;
38                 que.push(tt);
39             }
40         }
41     }
42     return res;
43 }
44 void solve(){
45     memset(num, 0, sizeof(num));
46     for (int i = 0; i < maxn; i++){
47         v1[i].clear(); v2[i].clear();
48     }
49     for (int i = 0; i < k; i++){
50         int x, y, z;
51         scanf("%d%d%d", &x, &y, &z);
52         if (x < y){
53             v1[x].push_back(event(y, z));
54             num[0][y] += z;
55         }else{
56             x = n - x + 1; y = n - y + 1;
57             v2[x].push_back(event(y, z));
58             num[1][y] += z;
59         }
60     }
61     ans = 0;
62     ans += work(v1, 0); ans += work(v2, 1);
63     printf("%d\n", ans);
64     return;
65 }
66 int main(){
67     while(scanf("%d%d%d", &k, &n, &c) == 3) solve();
68     return 0;
69 }

原文地址:https://www.cnblogs.com/LQLlulu/p/8710085.html

时间: 2024-11-13 10:02:36

Flying Right POJ - 3038的相关文章

poj 3038

Flying Right Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1580   Accepted: 557 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

dfs+bfs poj 3038

Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10331   Accepted: 4466 Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombie

POJ Xiangqi 4001 &amp;&amp; HDOJ 4121 Xiangqi

题目链接(POJ):http://poj.org/problem?id=4001 题目链接(HDOJ):http://acm.hdu.edu.cn/showproblem.php?pid=4121 Xiangqi Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1108   Accepted: 299 Description Xiangqi is one of the most popular two-player boa

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

对dijkstra的浅见(引例 poj 2457)

非负权值的单源最短路之 dijkstra Tanky Woo之dijkstra:  http://www.wutianqi.com/?p=1890 dijkstra-------我认为这个算法的核心思想是:最短路径长度递增.其实就是一个贪心算法. 怎么理解呢? 最短路的最优子结构:假如有一条最短路径已经存在了,那么其中任意两点的路径都将是最短的,否则假设是不成立了. 算法实现过程: 已当前点 pos 更新,dis[ i ]的值(即 点 i 到源点的距离) 找出dis[ i ] 最小的 i 点,作

POJ 2599 A funny game#树形SG(DFS实现)

http://poj.org/problem?id=2599 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> using namespace std; int n,k,pos; vector<int> g[1005]; bool flag[1005]; int dfs(int k) { for(int

poj 动态规划题目列表及总结

此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276,1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740(博弈),1742, 1887, 1926(马尔科夫矩阵,求平衡), 1936, 1952, 1953, 1958, 1959, 1962, 1975,

POJ - 3186 Treats for the Cows (区间DP)

题目链接:http://poj.org/problem?id=3186 题意:给定一组序列,取n次,每次可以取序列最前面的数或最后面的数,第n次出来就乘n,然后求和的最大值. 题解:用dp[i][j]表示i~j区间和的最大值,然后根据这个状态可以从删前和删后转移过来,推出状态转移方程: dp[i][j]=max(dp[i+1][j]+value[i]*k,dp[i][j-1]+value[j]*k) 1 #include <iostream> 2 #include <algorithm&

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)