VK Cup 2012 Qualification Round 1 E. Phone Talks —— DP
题目链接:http://codeforces.com/contest/158/problem/E
3 seconds
256 megabytes
standard input
standard output
Cool J has recently become a businessman Mr. Jackson, and he has to make a lot of phone calls now. Today he has n calls planned. For
each call we know the moment ti (in
seconds since the start of the day) when it is scheduled to start and its duration di (in
seconds). All ti are
different. Mr. Jackson is a very important person, so he never dials anybody himself, all calls will be incoming.
Mr. Jackson isn't Caesar and he can't do several things at once. If somebody calls him while he hasn't finished the previous conversation, Mr. Jackson puts the new call on hold in the queue. In this case immediately after the end of the current call Mr. Jackson
takes the earliest incoming call from the queue and starts the conversation. If Mr. Jackson started the call at the second t, and
the call continues for d seconds, then Mr. Jackson is busy at seconds t, t + 1, ..., t + d - 1,
and he can start a new call at second t + d. Note that if Mr. Jackson is not busy talking when somebody calls, he can't put
this call on hold.
Mr. Jackson isn't Napoleon either, he likes to sleep. So sometimes he allows himself the luxury of ignoring a call, as if it never was scheduled. He can ignore at most k calls.
Note that a call which comes while he is busy talking can be ignored as well.
What is the maximum number of seconds Mr. Jackson can sleep today, assuming that he can choose an arbitrary continuous time segment from the current day (that is, with seconds from the 1-st to the 86400-th, inclusive) when he is not busy talking?
Note that some calls can be continued or postponed to the next day or even later. However, the interval for sleep should be completely within the current day.
The first input line contains a pair of integers n, k (0 ≤ k ≤ n ≤ 4000)
separated by a space. Following n lines contain the description of calls for today. The description of each call is located on the
single line and consists of two space-separated integers ti and di,
(1 ≤ ti, di ≤ 86400).
All ti are
distinct, the calls are given in the order of strict increasing ti.
Scheduled times of calls [ti, ti + di - 1]
can arbitrarily intersect.
Print a number from 0 to 86400, inclusive — the maximally possible number of seconds for Mr. Jackson to sleep today.
3 2
30000 15000
40000 15000
50000 15000
49999
5 1
1 20000
10000 10000
20000 20000
25000 10000
80000 60000
39999
In the first sample the most convenient way is to ignore the first two calls.
In the second sample it is best to ignore the third call. In this case Mr. Jackson will have been speaking:
- first call: from 1-st to 20000-th second,
- second call: from 20001-st to 30000-th second,
- fourth call: from 30001-st to 40000-th second (the third call is ignored),
- fifth call: from 80000-th to 139999-th second.
Thus, the longest period of free time is from the 40001-th to the 79999-th second.
题解:
一开始以为是前缀和+后缀和就能解决的,结果发现接电话的时间不是固定的(如果跳过了前面的电话),所以行不通。
再后来看看n的大小:4e3。O(n^2)也不会超时,于是就试一下用DP。
设dp[i][j]为操作到第i个电话,跳过了 j个电话的状态下的最大休息时间。
然而:怎么找最大休息时间呢?还有:状态怎么转移呢?所以这种方法行不通。
正确做法:
1.设dp[i][j]为操作到第i个电话,跳过了j个电话的状态下的最小用时。
2.那么对于当前这个电话来说,就有跳过和不跳过两种选择,
跳过:则dp[i][j] = dp[i-1][j-1]
接通:则dp[i][j] = max(dp[i-1][j], t[i]-1) + d[i]。
两者取其小,则: dp[i][j] = min( dp[i-1][j-1], max(dp[i-1][j], t[i]-1) + d[i] )
当然,对于j==0时(即所有电话都接通), 直接计算即可: dp[i][0] = max(t[i]-1, dp[i-1][0]) + d[i]
3.最后:可知跳过的电话越多,休息时间可能越大,那么就直接取跳过k个电话。然后枚举每个电话与跳过之前k个电话后的时间间隔,取最大。
反思:
当初设置的状态:dp[i][j]为操作到第i个电话,跳过了j个电话的状态下的最大休息时间。
本想着递推到最后的dp[n][k]就是答案(想着一步登天),奈何此种状态找不到相关的转移方程。
看了题解后发现答案分两步求出:1.先求最小结束时间,2.再求最大间隔。
所以:不要总认为答案能一步求出,很可能要经过多步操作。尤其是DP: dp[n]不一定就是最终答案,或许还要经过几步操作方可得出。
代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-6;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 4e3+10; int n,k;
int t[maxn], d[maxn], dp[maxn][maxn]; int main()
{
scanf("%d%d",&n,&k);
for(int i = 1; i<=n; i++)
scanf("%d%d",&t[i], &d[i]); // for(int i = 0; i<=n; i++) //表明删除的电话>=实际电话时,结束时刻为0
// for(int j = i; j<=n; j++)
// dp[i][j] = 0; for(int i = 1; i<=n; i++)
{
dp[i][0] = max(t[i]-1, dp[i-1][0]) + d[i]; //全部接通
for(int j = 1; j<=min(i,k); j++)
dp[i][j] = min( dp[i-1][j-1], max(dp[i-1][j],t[i]-1)+d[i] );
} int ans = 0;
t[++n] = 86400+1;
for(int i = 1; i<=n; i++)
ans = max( ans, t[i]-dp[i-1][min(i-1,k)]-1 );
cout<<ans<<endl;
}
VK Cup 2012 Qualification Round 1 E. Phone Talks —— DP的更多相关文章
- DP VK Cup 2012 Qualification Round D. Palindrome pairs
题目地址:http://blog.csdn.net/shiyuankongbu/article/details/10004443 /* 题意:在i前面找回文子串,在i后面找回文子串相互配对,问有几对 ...
- VK Cup 2012 Qualification Round 1 C. Cd and pwd commands 模拟
C. Cd and pwd commands Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...
- VK Cup 2012 Qualification Round 2 C. String Manipulation 1.0 字符串模拟
C. String Manipulation 1.0 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/pr ...
- VK Cup 2012 Qualification Round 1---C. Cd and pwd commands
Cd and pwd commands time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- VK Cup 2016 - Qualification Round 2 B. Making Genome in Berland
今天在codeforces上面做到一道题:http://codeforces.com/contest/638/problem/B 题目大意是:给定n个字符串,找到最短的字符串S使得n个字符串都是这个字 ...
- VK Cup 2015 - Qualification Round 1 D. Closest Equals 离线+线段树
题目链接: http://codeforces.com/problemset/problem/522/D D. Closest Equals time limit per test3 secondsm ...
- VK Cup 2016 - Qualification Round 2 D. Three-dimensional Turtle Super Computer 暴力
D. Three-dimensional Turtle Super Computer 题目连接: http://www.codeforces.com/contest/638/problem/D Des ...
- VK Cup 2016 - Qualification Round 2 C. Road Improvement dfs
C. Road Improvement 题目连接: http://www.codeforces.com/contest/638/problem/C Description In Berland the ...
- VK Cup 2016 - Qualification Round 2 B. Making Genome in Berland 水题
B. Making Genome in Berland 题目连接: http://www.codeforces.com/contest/638/problem/B Description Berlan ...
随机推荐
- VMware虚拟机直连物理网络的两种方式
VMware虚拟机直连物理网络的两种方式 使用VMware构建虚拟机,通常虚拟机都使用NAT模式.这时,虚拟机有独立的网段.使用NAT模式,虚拟机之间数据都通过虚拟网络传输,不会影响实体机所在的实 ...
- JavaScript实现弹幕效果
效果如下 <html> <head> <title></title> <script src="https://cdn.staticfi ...
- ANT---调用外部命令的一些坑
最近用到了Ant,发现还是有许多功能是Ant没有提供相应Task支持,而操作系统提供了相应的系统命令.Ant说明书上说了,用<exec>可以调用系统命令,实际操作起来才发现陷阱可不少,一不 ...
- JVM加载的初始化类
首先Throws(抛出)几个自己学习过程中一直疑惑的问题: 1.什么是类加载?什么时候进行类加载? 2.什么是类初始化?什么时候进行类初始化? 3.什么时候会为变量分配内存? 4.什么时候会为变量赋默 ...
- 解决.NET Core MVC 视图中的中文被html编码的问题
在 .net core mvc 视图输出 变量的时候 默认使用的是 UnicodeRanges.BasicLatin 进行的编码 所以 输出中文后在查看源码的时候是进过编码了的 . 解决方案 在 ...
- 定时任务crontab如何实现每秒执行?
linux crontab 命令,最小的执行时间是一分钟.如需要在小于一分钟内重复执行,可以有两个方法实现. 方法一:crontab -l内容如下,则每10秒执行一次/home/fdipzone/ph ...
- activiti自己定义流程之整合(二):使用angular js整合ueditor创建表单
基础环境搭建完成,接下来就该正式着手代码编写了,在说代码之前.我认为有必要先说明一下activit自己定义流程的操作. 抛开自己定义的表单不谈.通过之前的了解,我们知道一个新的流程開始.是在启动流程实 ...
- [影像技术与PACS] 从技术角度看国内部份PACS厂商
天健PACS较早从事影像医院处理系统,为国外系统或设备以OEM方式提供软件模块.天健的PACS里面三维重建.容积重建.血管分析.虚拟腔镜.头部灌注等部分是用西安盈谷科技的,手术麻醉和重症监护系统是奥迪 ...
- C#遇见的函数
1.类Stopwatch 提供一组方法和属性,可用于准确地测量运行时间. 命名空间: System.Diagnostics Stopwatch timePerParse = Stopwatc ...
- bootstrap3分页
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...