Sicily 1366. Ferry Loading IV
来源:互联网 发布:电信网络能申请外网吗 编辑:程序博客网 时间:2024/06/05 03:25
1366. Ferry Loading IV
Constraints
Time Limit: 3 secs, Memory Limit: 32 MB
Description
Before bridges were common, ferries were used to transport cars across rivers. River ferries, unlike their larger cousins, run on a guide line and are powered by the river's current. Cars drive onto the ferry from one end, the ferry crosses the river, and the cars exit from the other end of the ferry.
There is an l-meter-long ferry that crosses the river. A car may arrive at either river bank to be transported by the ferry to the opposite bank. The ferry travels continuously back and forth between the banks so long as it is carrying a car or there is at least one car waiting at either bank. Whenever the ferry arrives at one of the banks, it unloads its cargo and loads up cars that are waiting to cross as long as they fit on its deck. The cars are loaded in the order of their arrival; ferry's deck accommodates only one lane of cars. The ferry is initially on the left bank where it broke and it took quite some time to fix it. In the meantime, lines of cars formed on both banks that await to cross the river.
Input
The first line of input contains c, the number of test cases. Each test case begins with l, m. m lines follow describing the cars that arrive in this order to be transported. Each line gives the length of a car (in centimeters), and the bank at which the car arrives ("left" or "right").
Output
For each test case, output one line giving the number of times the ferry has to cross the river in order to serve all waiting cars.
Sample Input
420 4380 left720 left1340 right1040 left15 4 380 left720 left1340 right1040 left15 4 380 left720 left1340 left1040 left15 4 380 right720 right1340 right1040 right
Sample Output
3356
// Problem#: 1366// Submission#: 3305864// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <iostream>#include <vector>#include <algorithm>#include <stdio.h>#include <math.h>#include <string.h>#include <string>#include <queue>using namespace std;int main() { std::cout.sync_with_stdio(false); int caseNum; cin >> caseNum; while (caseNum--) { int l, m; cin >> l >> m; l *= 100; int car, allCar = 0; int ans = 0; bool atLeft = true; string pos; queue<int> ll, rr; while (m--) { cin >> car >> pos; if (pos == "left") ll.push(car); else rr.push(car); } while (1) { while (!ll.empty() && ll.front() + allCar <= l) { allCar += ll.front(); ll.pop(); } allCar = 0; ans++; if (ll.empty() && rr.empty()) break; while (!rr.empty() && rr.front() + allCar <= l) { allCar += rr.front(); rr.pop(); } allCar = 0; ans++; if (ll.empty() && rr.empty()) break; } cout << ans << endl; } return 0;}
- Sicily 1366. Ferry Loading IV
- Ferry Loading IV(队列)
- POJ 3282 Ferry Loading IV(模拟,队列)
- POJ 3282 Ferry Loading IV(模拟)
- POJ 3282 Ferry Loading IV(模拟,队列)
- POJ 3282 Ferry Loading IV 可能会
- POJ-3282(摆渡问题)(Ferry Loading IV )
- POJ 3282 Ferry Loading IV(简单模拟)
- Ferry Loading III
- Ferry Loading II
- ZOJ 1918 Ferry Loading II
- 【贪心】poj2336 ferry loading II
- hdu 3741 Ferry Loading V
- Ferry Loading II 贪心算法
- uva 10440 Ferry Loading II
- poj 2336 Ferry Loading II
- uva 10440 Ferry Loading II
- UVa 10440 - Ferry Loading II
- Sicily 1441. Pie
- linux查看磁盘使用情况
- [译]【Storm入门指南】第五章 Bolts
- Sicily 1373. Cows Of The Round Ta
- Sicily 1428. B Baby Climber(心宝去爬山)
- Sicily 1366. Ferry Loading IV
- jdbc+tomcat+mysql插入utf-8中文乱码解决方案
- Performance Insights to Intel® Hyper-Threading Technology
- 数组的行列变换
- 黑马程序员--单例模式与模板方法模式
- HTML第五节(为网页插入图片)
- Sicily 1363. Semi-prime H-numbers
- 【黑马程序员】Objetive-C 对象详解 一
- 编程之美:中国象棋将帅问题(开篇)