Kickstart Round D 2017 problem A sightseeing 一道DP
这是现场完整做出来的唯一一道题Orz。。而且还调了很久的bug。还是太弱了。
Problem
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 |
Case #1: 2 |
In the first test case, you can go sightseeing in city 1, catching the bus leaving at time 3 and arriving at time 4. You can go sightseeing in city 2, leaving on the bus at time 8. When you arrive in city 3 at time 10 you immediately board the next bus and arrive in city 4 just in time at time 12.
大致思路:以dp[j][k]表示到达第j个城市,路上看过k次风景的最小时间,设计状态转移方程即可。
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#define rep(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
long long int dp[][];
int s[];
int f[];
int d[];
long long int seartim(int num,long long int timenow)
{
//if(num==1&&timenow==3) printf("num=%d snum=%d dnum=%d f[num]=%d",num,s[num],d[num],f[num]);
if(timenow<=s[num]) return s[num]+d[num];
else
{
if(f[num]==) return timenow+d[num];
int t=(timenow-s[num])/f[num];
if((timenow-s[num])%f[num]>) t=t+;
return (long long int)s[num]+t*f[num]+d[num];
}
}
int main()
{
freopen("A-large.in","r",stdin);
freopen("A-large.out","w",stdout);
int T;
scanf("%d",&T);
int n,spend,ddl,ans;
rep(i,,T)
{
scanf("%d%d%d",&n,&spend,&ddl);
rep(j,,)
{
rep(k,,) dp[j][k]=ddl+;
}
rep(j,,n-)
{
scanf("%d%d%d",&s[j],&f[j],&d[j]);
}
dp[][]=;
rep(j,,n-)
{
rep(k,,j-) dp[j+][k]=seartim(j,dp[j][k]);
rep(k,,j)
{
// printf("spend=%d\n",spend);
// if(i==1&&j==1) printf("dp=%d \n",seartim(j,dp[j][k-1]+spend));
dp[j+][k]=min(dp[j+][k],seartim(j,dp[j][k-]+spend));
}
}
ans=n;
rep(j,,n-)
{
if(dp[n][j]<=ddl) ans=j;
}
if(ans==n) printf("Case #%d: IMPOSSIBLE\n",i);
else printf("Case #%d: %d\n",i,ans);
}
return ;
}
Kickstart Round D 2017 problem A sightseeing 一道DP的更多相关文章
- google Kickstart Round F 2017 四道题题解
Problem A. Kicksort 题意抽象一下为: 对于一个每次都从数列正中间取划分数的快速排序,给定一个1-n的排列,问快排的复杂度对于这个排列是否会退化为最坏复杂度. 数据范围: 测试组数1 ...
- Kickstart Round H 2019 Problem B. Diagonal Puzzle
有史以来打得最差的一次kickstart竟然发生在winter camp出结果前的最后一次ks = = 感觉自己的winter camp要凉了 究其原因,无非自己太眼高手低,好好做B, C的小数据,也 ...
- google Kickstart Round G 2017 三道题题解
A题:给定A,N,P,计算A的N!次幂对P取模的结果. 数据范围: T次测试,1 ≤ T ≤ 100 1<=A,N,P<=105 快速幂一下就好了.O(nlogn). AC代码: #inc ...
- Kickstart Round D 2017 : A
思路: 动态规划. large数据的时间范围很大,无法设计入状态中.转换思路为定义dp[i][j]为当前在景点i,并且已经游览了j个景点所花费的最小时间,这种思想与leetcode45类似.于是转移方 ...
- Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)
题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...
- Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树)
Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树) 题目链接 题意 给定一个nm的矩阵,每行取2k的矩阵,求总 ...
- String & dp Problem Round 3 2017.4.22
对每一个特征求前缀和,如果它减去前面的某一个地方的和,得到的每个特征是相等的,那么然后就可以更新答案. 需要解决这个两个问题 1.如何使答案尽量大? 这个很简单,直接找尽量靠前的地方就好了. 2,如何 ...
- Codeforces Round #371 (Div. 2)E. Sonya and Problem Wihtout a Legend[DP 离散化 LIS相关]
E. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megaby ...
- Google Code Jam Round 1C 2015 Problem A. Brattleship
Problem You're about to play a simplified "battleship" game with your little brother. The ...
随机推荐
- Git操作大全[实际用到的都放在这里总结]
1.如何合并远程两个分支feature-rebuild和develop? g fetch g checkout -b develop origin/develop g merge feature-re ...
- css动画属性--轮播图效果
通过css的动画属性实现轮播图的显示效果 代码如下: 主体部分: <div id="move"> <ul> <li><img src=&q ...
- sqlserver提高篇
Microsoft SQL Server2008复习提高 一.Microsoft SQL Server 系统的体系结构 1.Microsoft SQL Server2008由4个主要的部分组成,即4个 ...
- Windows 10 IoT Serials 9 – 如何利用IoTCoreAudioControlTool改变设备的音频设备
大家知道,在Windows 10 IoT Core上,如果用户外接了USB声卡.带有麦克风的摄像头之类的硬件,就会有多个音频设备可以用.但是,系统目前并没有提供直接的UI来设置音频的输入或者输出设备. ...
- lfcp——PB使用
场景:记录以备忘. 1 直接说明 pb连接说明:目前项目共有性能.功能.业务.联调四个环境.所以pb可以连接四个不同的数据库.1.copy一份pb程序,修改main.ini文件中的DBCONNECT/ ...
- LeetCode-Maximum Subarray[dp]
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- 【网络】dns_probe_finished_nxdomain 错误
解决方案: 谷歌浏览器地址栏输入 chrome://net-internals/#dns 清除 dns 缓存即可
- linux查看是否安装Apache,mysql,python等
1.Apache httpd -v service httpd start 启动 service httpd restart 重新启动 service httpd stop 停止服务 2.mysql ...
- [js高手之路] es6系列教程 - 对象功能扩展详解
第一:字面量对象的方法,支持缩写形式 //es6之前,这么写 var User = { name : 'ghostwu', showName : function(){ return this.nam ...
- go的变量redeclare的问题,golang的一个小坑
go的变量声明有几种方式: 1 通过关键字 var 进行声明 例如:var i int 然后进行赋值操作 i = 5 2 最简单的,通过符号 := 进行声明和赋值 例如: i:=5 golang会 ...