coreBugZJ

此 blog 已弃。

EOJ 1855 Expedition

  1/*
  2EOJ 1855 Expedition
  3
  4
  5----题意:
  6一辆卡车从起点驶向终点,每行进一单位距离,消耗一单位燃料。
  7起点距终点有 L 单位距离,车上有 P 单位燃料。
  8中途有 N 个补给站,第 i 个补给站距终点有 Di 单位距离,可提供的补给为 Pi 单位燃料。
  9假设车上可以装载无限多的燃料。
 10
 11求最少需要几次补给可以到达终点。
 12
 13
 14----输入:
 15第一行,一个整数 N;
 16第二行到第N+1行,每行两个用空格分开的整数 Di 和 Pi;
 17最后一行,两个用空格分开的整数 L 和 P。
 18
 19
 20----输出:
 21一个整数,到达终点所需的最少补给次数;若不能到达终点,输出整数 -1 。
 22
 23
 24----数据范围:
 251 <= N  <= 10,000;
 261 <= Pi <= 100
 271 <= P  <= 1,000,000
 28     L  <= 1,000,000
 29
 30
 31----样例输入:
 324
 334 4
 345 2
 3511 5
 3615 10
 3725 10
 38
 39
 40----样例输出:
 412
 42
 43
 44----分析:
 45A.由于车上可以装载无限多的燃料,所以不必考虑车上的燃料是否过多;
 46只需考虑在保证车上燃料够用的情况下减少补给次数。
 47B.若车上燃料不够用,则必须补给;
 48若必须补给却无法补给,则无法到达终点。
 49C.由于起点到终点的距离已知,且车上所装载的燃料量已知,且行驶距离与消耗燃料量的关系已知,
 50        所以所需补给的燃料量已知;
 51D.由于起点到终点的距离已知,且车上所装载的燃料量已知,且行驶距离与消耗燃料量的关系已知,补给站与终点距离已知,
 52        所以可以确定当车上有多少燃料时,可以到哪些补给站获得补给。
 53E.从某补给站获得 Pi 单位燃料补给,等价于卡车在起点处的燃料储备增加了 Pi 单位 且 此补给站不存在。
 54
 55
 56----结论:
 57贪心算法。
 58若卡车在起点处的燃料储备不足以到达终点,则从其可以到达的补给站中挑选补给量最大的站点获得补给,
 59获得补给的方式为 卡车在起点处的燃料储备增加 Pi 单位并删除该补给站。
 60重复执行以上步骤,直到燃料储备充足或无法获得补给。
 61
 62
 63*/

 64
 65
 66/*********************************************************
 67版本三
 68*/

 69#include <iostream>
 70#include <cstdio>
 71#include <vector>
 72#include <queue>
 73#include <algorithm>
 74
 75using namespace std;
 76
 77typedef  pair< intint >  Stop;
 78#define  d first
 79#define  p second
 80
 81#define  L  10009
 82
 83int   n;
 84Stop  stop[ L ];
 85Stop  town;
 86
 87int main() {
 88        int i, ans = 0;
 89        priority_queue< int >  heap;
 90
 91        scanf( "%d"&n );
 92        for ( i = 0; i < n; ++i ) {
 93                scanf( "%d%d"&(stop[ i ].d), &(stop[ i ].p) );
 94        }

 95        scanf( "%d%d"&(town.d), &(town.p) );
 96
 97        sort( stop, stop+n );
 98
 99        i = n - 1;
100        while ( (i >= 0&& (town.d - stop[ i ].d <= town.p) ) {
101                heap.push( stop[ i ].p );
102                --i;
103        }

104
105        while ( (town.d > town.p) && (! heap.empty()) ) {
106                ++ans;
107                town.p += heap.top();
108                heap.pop();
109
110                while ( (i >= 0&& (town.d - stop[ i ].d <= town.p) ) {
111                        heap.push( stop[ i ].p );
112                        --i;
113                }

114        }

115
116        printf( "%d\n", ( (town.d <= town.p) ? ans : (-1) ) );
117        return 0;
118}

119
120
121/*********************************************************
122版本二
123*/

124/*
125#include <iostream>
126#include <vector>
127#include <queue>
128#include <algorithm>
129
130using namespace std;
131
132typedef  pair< int, int >  Stop;
133#define  d first
134#define  p second
135
136#define  L  10009
137
138int   n;
139Stop  stop[ L ];
140Stop  town;
141
142int main() {
143        int i, ans = 0;
144        priority_queue< int >  heap;
145
146        cin >> n;
147        for ( i = 0; i < n; ++i ) {
148                cin >> stop[ i ].d >> stop[ i ].p;
149        }
150        cin >> town.d >> town.p;
151
152        sort( stop, stop+n );
153
154        i = n - 1;
155        while ( (i >= 0) && (town.d - stop[ i ].d <= town.p) ) {
156                heap.push( stop[ i ].p );
157                --i;
158        }
159
160        while ( (town.d > town.p) && (! heap.empty()) ) {
161                ++ans;
162                town.p += heap.top();
163                heap.pop();
164
165                while ( (i >= 0) && (town.d - stop[ i ].d <= town.p) ) {
166                        heap.push( stop[ i ].p );
167                        --i;
168                }
169        }
170
171        cout << ( (town.d <= town.p) ? ans : (-1) ) << endl;
172        return 0;
173}
174*/

175
176
177/*********************************************************
178版本一
179*/

180/*
181#include <iostream>
182#include <vector>
183#include <queue>
184#include <algorithm>
185
186using namespace std;
187
188struct  Stop
189{
190        int d, p;
191};
192bool operator<( const Stop &a, const Stop &b ) {
193        return ( a.d < b.d );
194}
195
196struct  Cmp
197{
198        bool operator()( const Stop &a, const Stop &b );
199};
200bool Cmp::operator()( const Stop &a, const Stop &b ) {
201        return (a.p < b.p);
202}
203
204
205#define  L  10009
206
207int   n;
208Stop  stop[ L ];
209Stop  town;
210
211int main() {
212        int i, ans = 0;
213        priority_queue< Stop, vector< Stop >, Cmp >  heap;
214
215        cin >> n;
216        for ( i = 0; i < n; ++i ) {
217                cin >> stop[ i ].d >> stop[ i ].p;
218        }
219        cin >> town.d >> town.p;
220
221        sort( stop, stop+n );
222
223        i = n - 1;
224        while ( (i >= 0) && (town.d - stop[ i ].d <= town.p) ) {
225                heap.push( stop[ i ] );
226                --i;
227        }
228
229        while ( (town.d > town.p) && (! heap.empty()) ) {
230                ++ans;
231                town.p += heap.top().p;
232                heap.pop();
233
234                while ( (i >= 0) && (town.d - stop[ i ].d <= town.p) ) {
235                        heap.push( stop[ i ] );
236                        --i;
237                }
238        }
239
240        cout << ( (town.d <= town.p) ? ans : (-1) ) << endl;
241        return 0;
242}
243*/

244

posted on 2012-03-04 22:35 coreBugZJ 阅读(373) 评论(0)  编辑 收藏 引用 所属分类: ACMAlgorithm课内作业


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理