Kickstart Round D 2017 problem A sightseeing 一道DP

时间:2021-08-09 23:41:40

这是现场完整做出来的唯一一道题Orz。。而且还调了很久的bug。还是太弱了。

Kickstart Round D 2017 problem A sightseeing  一道DP

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: SiFi 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, NTs 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, SiFi 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

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的更多相关文章

  1. google Kickstart Round F 2017 四道题题解

    Problem A. Kicksort 题意抽象一下为: 对于一个每次都从数列正中间取划分数的快速排序,给定一个1-n的排列,问快排的复杂度对于这个排列是否会退化为最坏复杂度. 数据范围: 测试组数1 ...

  2. Kickstart Round H 2019 Problem B&period; Diagonal Puzzle

    有史以来打得最差的一次kickstart竟然发生在winter camp出结果前的最后一次ks = = 感觉自己的winter camp要凉了 究其原因,无非自己太眼高手低,好好做B, C的小数据,也 ...

  3. 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 ...

  4. Kickstart Round D 2017 &colon; A

    思路: 动态规划. large数据的时间范围很大,无法设计入状态中.转换思路为定义dp[i][j]为当前在景点i,并且已经游览了j个景点所花费的最小时间,这种思想与leetcode45类似.于是转移方 ...

  5. Codeforces Round &num;174 &lpar;Div&period; 1&rpar; B&period; Cow Program&lpar;dp &plus; 记忆化&rpar;

    题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...

  6. Codeforces Round &num;620 F2&period; Animal Observation &lpar;hard version&rpar; &lpar;dp &plus; 线段树&rpar;

    Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树) 题目链接 题意 给定一个nm的矩阵,每行取2k的矩阵,求总 ...

  7. String &amp&semi; dp Problem Round 3 2017&period;4&period;22

    对每一个特征求前缀和,如果它减去前面的某一个地方的和,得到的每个特征是相等的,那么然后就可以更新答案. 需要解决这个两个问题 1.如何使答案尽量大? 这个很简单,直接找尽量靠前的地方就好了. 2,如何 ...

  8. Codeforces Round &num;371 &lpar;Div&period; 2&rpar;E&period; Sonya and Problem Wihtout a Legend&lbrack;DP 离散化 LIS相关&rsqb;

    E. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megaby ...

  9. Google Code Jam Round 1C 2015 Problem A&period; Brattleship

    Problem You're about to play a simplified "battleship" game with your little brother. The ...

随机推荐

  1. 【干货分享】流程DEMO-离职流程

    流程名: 离职申请   流程相关文件: 流程包.xml WebService业务服务.xml WebService.asmx WebService.cs   流程说明: 流程中集成了webservic ...

  2. My97DatePicker使用技巧

    My97DatePicker使用是很常用的控件,总结一下常用使用技巧: 1.onpicked是事件,也就选择日期之后触发事件: 2.isShowClear:是否显示清理按钮: 3.maxDate:最大 ...

  3. 实例讲解Nginx下的rewrite规则

    一.正则表达式匹配,其中:* ~ 为区分大小写匹配* ~* 为不区分大小写匹配* !~和!~*分别为区分大小写不匹配及不区分大小写不匹配二.文件及目录匹配,其中:* -f和!-f用来判断是否存在文件* ...

  4. 移动平台中 meta 标签的使用

    一.meta 标签分两大部分:HTTP 标题信息(http-equiv)和页面描述信息(name). 1.http-equiv 属性的 Content-Type 值(显示字符集的设定) 说明:设定页面 ...

  5. gitlab升级方法

    gitlab升级方法:国内网络环境推荐方法二方法一:官网的升级方式 (1)停止git服务 gitlab-ctl stop unicorn gitlab-ctl stop sidekiq gitlab- ...

  6. 怎样为virtualbox添加新的分辨率

    virtualbox是个相当NB的开源跨平台虚拟机软件,只是新创建的虚拟机仅仅支持几种分辨率.比如.安装win8.1,仅仅有例如以下图的几种分辨率. 只是我的显示器是5K哦,这么点分辨率,简直是搞笑. ...

  7. 通过jsonp解决浏览器的跨域共享

    因为浏览器的同源策略,普通ajax访问跨域请求返回的json数据是不会被浏览器接受的.看下面例子可以看出是访问不到的 首先 定义webapi 后台代码 public class JsopControl ...

  8. 选择排序java

    先简述选择排序,然后上代码 进行选择排序就是将所有的元素扫描一遍,从中挑选(或者说是选择,这正是这个排序名字的由来)最小的一个元素,将这个最小的元素与最左边的元素交换位置 ,现在最左边的元素就是有序的 ...

  9. Google的两种广告推广方式

    1搜索关键字广告推送:AdWords: 覆盖广泛:在全球最大的搜索和网络平台上进行推广. 定位精准:锁定目标客户群体,让潜在客户轻松找上门. 成本可控:仅当用户点击广告时,您才支付费用. 2.网站内容 ...

  10. oracle sql语句大全

    ORACLE支持五种类型的完整性约束 NOT NULL (非空)--防止NULL值进入指定的列,在单列基础上定义,默认情况下,ORACLE允许在任何列中有NULL值. CHECK (检查)--检查在约 ...