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 ...
随机推荐
- sleep() 和 wait() 有什么区别?
sleep()方法是使线程停止一段时间的方法.在sleep 时间间隔期满后,线程不一定立即恢复执行.这是因为在那个时刻,其它线程可能正在运行而且没有被调度为放弃执行,除非"醒来"的 ...
- QuartusII 13.0 PLL IP Core调用及仿真
有一个多月没用用Quartus II了,都快忘了IP 是怎么用调用的了,还好有之前做的笔记,现在整理出来,终于体会到做笔记的好处. 一. QuartusII的pll的调用 打开软件界面 Tool—— ...
- [luogu P3797] 妖梦斩木棒 [线段树]
题目背景 妖梦是住在白玉楼的半人半灵,拥有使用剑术程度的能力. 题目描述 有一天,妖梦正在练习剑术.地面上摆放了一支非常长的木棒,妖梦把它们切成了等长的n段.现在这个木棒可以看做由三种小段构成,中间的 ...
- luogu P1361 小猫爬山 [iddfs]
题目描述 WD和LHX饲养了N只小猫,这天,小猫们要去爬山.经历了千辛万苦,小猫们终于爬上了山顶,但是疲倦的它们再也不想徒步走下山了. WD和LHX只好花钱让它们坐索道下山.索道上的缆车最大承重量为W ...
- Java中的数值和集合
数组array和集合的区别: (1) 数值是大小固定的,同一数组只能存放一样的数据. (2) java集合可以存放不固定的一组数据 (3) 若程序事不知道究竟需要多少对象,需要在空间不足时自动扩增容量 ...
- jquery 禁止herf跳转,并执行相应的js代码
<a class="oh_btn bg_3" href="javascript:void(0);" onclick="myfun(this)&q ...
- 【逻辑漏洞】基于BurpSuite的越权测试实战教程
一.什么是越权漏洞?它是如何产生的? 越权漏洞是Web应用程序中一种常见的安全漏洞.它的威胁在于一个账户即可控制全站用户数据.当然这些数据仅限于存在漏洞功能对应的数据.越权漏洞的成因主要是因为开发人员 ...
- vsftp虚拟主机
################################Vsftp服务器实战##########################################3 文件传输协议,基于该协议FT ...
- F数圈圈
Description 幼儿园的小朋友对数字其实不是很感兴趣,他们更感兴趣的是形状,现在给你一个数字,小朋友都会数出其中一共有多少圆圈圈 Input 一个数字n长度不超过19位 Output 输出其中 ...
- vue的一些坑(第一天)
1:刚开始在创建项目的时候一直出现vue不是内部或外部命令 关于这个的解决方案网上给出的很多的解决方案是配置环境,这个我就不在赘述了: 但是我觉得那样太麻烦,就没用,只是重新 npm i vue-cl ...