2017 google code jam D轮
来源:互联网 发布:ipa 反编译源码 编辑:程序博客网 时间:2024/06/05 00:59
题目描述:
When you travel, you like to spend time sightseeing in as many cities as possible, but sometimes you might not be able to because you need to catch the bus to the next city. To maximize your travel enjoyment, you decide to write a program to optimize your schedule.
You begin at city 1 at time 0 and plan to travel to cities 2 to N in ascending order, visiting every city. There is a bus service from every city i to the next city i + 1. The i-th bus service runs on a schedule that is specified by 3 integers: Si, Fi and Di, the start time, frequency and ride duration. Formally, this means that there is a bus leaving from city i at all times Si+ xFi, where x is an integer and x ≥ 0, and the bus takes Di time to reach city i + 1.
At each city between 1 and N - 1, inclusive, you can decide to spend Ts time sightseeing before waiting for the next bus, or you can immediately wait for the next bus. You cannot go sightseeing multiple times in the same city. You may assume that boarding and leaving buses takes no time. You must arrive at city N by time Tf at the latest. (Note that you cannot go sightseeing in city N, even if you arrive early. There’s nothing to see there!)
What is the maximum number of cities you can go sightseeing in?
Input
The input starts with one line containing one integer T, which is the number of test cases. T test cases follow.
Each test case begins with a line containing 3 integers, N, Ts and Tf, representing the number of cities, the time taken for sightseeing in any city, and the latest time you can arrive in city N.
This is followed by N - 1 lines. On the i-th line, there are 3 integers, Si, Fi and Di, indicating the start time, frequency, and duration of buses travelling from city i to city i + 1.
Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the maximum number of cities you can go sightseeing in such that you can still arrive at city N by time Tf at the latest. If it is impossible to arrive at city N by time Tf, output Case #x: IMPOSSIBLE.
Limits
1 ≤ T ≤ 100.
Small dataset
2 ≤ N ≤ 16.
1 ≤ Si ≤ 5000.
1 ≤ Fi ≤ 5000.
1 ≤ Di ≤ 5000.
1 ≤ Ts ≤ 5000.
1 ≤ Tf ≤ 5000.
Large dataset
2 ≤ N ≤ 2000.
1 ≤ Si ≤ 109.
1 ≤ Fi ≤ 109.
1 ≤ Di ≤ 109.
1 ≤ Ts ≤ 109.
1 ≤ Tf ≤ 109.
Sample
Input
Output
4
4 3 12
3 2 1
6 2 2
1 3 2
3 2 30
1 2 27
3 2 1
4 1 11
2 1 2
4 1 5
8 2 2
5 10 5000
14 27 31
27 11 44
30 8 20
2000 4000 3
Case #1: 2
Case #2: 0
Case #3: IMPOSSIBLE
Case #4: 4
我的解题思路,主要是一个回溯的思想,每一次都会选择是否要参观当前城市,如果参观时间是否足够,以下是我的源码。
#include <iostream>#include <vector>#include <fstream>using namespace std;int find(vector<vector<double> > nums, double ts, int count);bool judge(vector<vector<double> > nums, double ts);int main(){ ifstream ifile; ifile.open("in.txt"); ofstream ofile; //定义输出文件 ofile.open("out.txt"); //作为输出文件打开 int T; ifile>>T; vector<vector<double> > nums; for(int i=0; i<T; i++){ int N, Ts, Tf; ifile>>N; ifile>>Ts; ifile>>Tf; vector<double> num; num.push_back(N); num.push_back(Ts); num.push_back(Tf); nums.push_back(num); for(int j=0; j<N-1; j++){ int Si, Fi, Di; vector<double> mid; ifile>>Si; ifile>>Fi; ifile>>Di; mid.push_back(Si); mid.push_back(Fi); mid.push_back(Di); nums.push_back(mid); } } ifile.close(); int j=0, count = 0; for(int i=j; j<nums.size(); ){ i = j; count++; vector<vector<double> > child; child.push_back(nums[i]); for(j=i+1; j< i+nums[i][0];j++){ child.push_back(nums[j]); } if(!judge(child, 0)){ cout<<"Case #"<<count<<": IMPOSSIBLE"<<endl; ofile<<"Case #"<<count<<": IMPOSSIBLE"<<endl; } else{ int res = find(child, 0, 0); if(res < 0){ cout<<"Case #"<<count<<": IMPOSSIBLE"<<endl; ofile<<"Case #"<<count<<": IMPOSSIBLE"<<endl; } else{ cout<<"Case #"<<count<<": "<<res<<endl; ofile<<"Case #"<<count<<": "<<res<<endl; } } } ofile.close();}int find(vector<vector<double> > nums, double ts, int count){ count ++; if(nums.size()==0){ return 0; } double N, Ts, Tf; N = nums[0][0]; Ts = nums[0][1]; Tf = nums[0][2]; double Si, Fi, Di; if(N==1){ return 0; } else{ Si = nums[1][0]; Fi = nums[1][1]; Di = nums[1][2]; nums.erase(nums.begin()+1); double newts; while(ts > Si){ Si += Fi; } newts = Si + Di; double newts2; if(ts+Ts <= Si){ newts2 = Si + Di; } else{ while(ts + Ts > Si){ Si += Fi; } newts2 = Si+Di; } nums[0][0] -= 1; //判断是否能够在下余生 if(judge(nums, newts2)){ return max(1+find(nums, newts2, count), find(nums, newts, count)); } else if(judge(nums, newts)){ return find(nums, newts, count); } else{ return -100; } }}bool judge(vector<vector<double> > nums, double ts){ double Tf = nums[0][2]; if(nums.size()==1){ if(ts <= Tf){ return true; } else{ return false; } } for(int i=1; i< nums.size(); i++){ double Si = nums[i][0]; double Fi = nums[i][1]; double Di = nums[i][2]; if(Si > Tf || Di > Tf || Si+Di>Tf){ return false; } if(ts < Si){ ts = Si+Di; } else{ while(ts > Si){ Si += Fi; } ts = Si+Di; } if(ts > Tf){ return false; } } return true;}
源码通过第一题的小数据集测试,但是大数据集没有通过,大家一起来看看呀,欢迎指正。大数据集如下:
- 2017 google code jam D轮
- Google Code Jam 2014 A,B,D
- google code jam 2015 D. Ominous Omino
- Google Code Jam 2017 资格赛
- [刷题]Google Code Jam 2017
- Google Code Jam 2014 Problem D. Deceitful War
- Google Code Jam 2014 Qualification Round 2014 A B D
- Google Code Jam 2014 Qualification Round:D. Deceitful War
- Google Code Jam 2006
- Google China Code Jam
- Google Code Jam
- Google Code Jam 130914
- Google Code Jam 2015
- Google Code Jam 2015
- 2016 google code jam
- Google code jam
- Google Code Jam预选赛完成.
- My Google Code Jam 2010
- iOS开发:真机或者模拟器中,APP中的按钮文字下面自动出现下划线的问题的解决
- 对象池
- CMD命令打包文件夹成jar
- TCP协议的3次握手与4次挥手过程详解
- easyui-tree相关
- 2017 google code jam D轮
- 修改后缀名工具
- iOS 光标默认获取当前文本框输入位置
- Unity插件EasyToucu安卓平台的个人开发实例
- 【effective Java读书笔记】类和接口(二)
- 无题
- tensorflow学习——numpy
- Web报表系统葡萄城报表:Java 报表
- 英语语法5-现在完成时(续)