1084 - Winter
Time Limit: 2 second(s) Memory Limit: 32 MB

Winter is coming. In a land far away, N men are spending the nights in a valley in a largest field. The valley is so narrow that it can be considered to be a straight line running east-to-west.

Although standing in the valley does shield them from the wind, the group still shivers during the cold nights. They, like anyone else, would like to gather together for warmth.

Near the end of each day, each man i finds himself somewhere in the valley at a unique location Li. The men want to gather into groups of three or more persons since two persons just aren't warm enough. They want to be in groups before sunset, so the distance K each man can walk to form a group is limited. Determine the smallest number of groups the men can form.

Input

Input starts with an integer T (≤ 15), denoting the number of test cases.

Each case starts with two integers N (1 ≤ N ≤ 105) and K (1 ≤ K ≤ 106). Each of the next N line contains an integer Li (1 ≤ Li ≤ 108).

Output

For each case, print the case number and smallest number of groups the men can gather into. If there is no way for all the men to gather into groups of at least size three, output -1.

Sample Input

Output for Sample Input

2

6 10

2

10

15

13

28

9

3 1

1 10 20

Case 1: 2

Case 2: -1

Note

Dataset is huge, use faster I/O methods.


Special Thanks: Jane Alam Jan (Description, Solution, Dataset)
思路:dp
首先我们可以想到N*N的方法,dp[i]=min(dp[i],dp[j]+1),(j<=i-2;)并且这个j还需满足ans[i]-ans[j+1]<=k;
dp[i]表示前i个点合法分配的最小值,我们将所有的dp先赋值无穷大,那么先将所有的数先按照升序排。我们可以知道当前面的一些状态不能够达到最优时也必然不能使后面的点达最优,所以前面的点可以不考虑,所以我们维护一个队列,如果一些点可以确定不能使后面的达最优,就出队。
 1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<stdlib.h>
5 #include<string.h>
6 #include<queue>
7 using namespace std;
8 typedef long long LL;
9 int dp[100005];
10 typedef struct pp
11 {
12 int x;
13 int id;
14 } ss;
15 int ans[100005];
16 ss ask[100005];
17 int flag[100005];
18 int main(void)
19 {
20 int i,j,k;
21 scanf("%d",&k);
22 int s;
23 for(s=1; s<=k; s++)
24 {
25 queue<ss>que;
26 memset(flag,0,sizeof(flag));
27 int n,m;
28 scanf("%d %d",&n,&m);
29 for(i=1; i<=n; i++)
30 {
31 scanf("%d",&ans[i]);
32 }
33 sort(ans+1,ans+n+1);
34 for(i=1; i<=n; i++)
35 {
36 ask[i].id=i;
37 ask[i].x=ans[i];
38 }
39 int as=0;
40 dp[0]=0;
41 if(n<=2)
42 as=-1;
43 else
44 {
45 que.push(ask[1]);
46 que.push(ask[2]);
47 flag[1]=1;
48 flag[2]=1;
49 for(i=1; i<=n; i++)
50 dp[i]=1e9;
51 int f=0;
52 for(i=3; i<=n; i++)
53 {
54 while(!que.empty())
55 {
56 f=1;
57 ss cc=que.front();
58 int vv=(cc.x+ask[i].x+1)/2;
59 int uu=vv-cc.x;
60
61 if(uu>m)
62 {
63 que.pop();
64 }
65 else
66 {
67 if(i-cc.id>=2)
68 {
69 dp[i]=min(dp[i],dp[cc.id-1]+1);
70 if(dp[i]<1e9)
71 break;
72 else que.pop();
73 }
74 else break;
75 }
76
77 }
78 que.push(ask[i]);
79 if(!f)
80 break;
81 }
82 if(dp[n]==1e9)
83 {
84 as=-1;
85 }
86 else as=dp[n];
87 }
88 printf("Case %d: %d\n",s,as);
89 }
90 return 0;
91 }

1084 - Winter的更多相关文章

  1. 暑期训练狂刷系列——Lightoj 1084 - Winter bfs

    题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1084 题目大意: 有n个点在一条以零为起点的坐标轴上,每个点最多可以移动k, ...

  2. lightoj 1084 - Winter(dp+二分+线段树or其他数据结构)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1084 题解:不妨设dp[i] 表示考虑到第i个点时最少有几组那么 if a[i ...

  3. Winter(bfs&&dfs)

    1084 - Winter   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Winter is ...

  4. lightoj刷题日记

    提高自己的实力, 也为了证明, 开始板刷lightoj,每天题量>=1: 题目的类型会在这边说明,具体见分页博客: SUM=54; 1000 Greetings from LightOJ [简单 ...

  5. 【SCOI2005】 最大子矩阵 BZOJ 1084

    Description 这里有一个n*m的矩阵,请你选出其中k个子矩阵,使得这个k个子矩阵分值之和最大.注意:选出的k个子矩阵不能相互重叠. Input 第一行为n,m,k(1≤n≤100,1≤m≤2 ...

  6. 开发框架Data Abstract和Hydra发布版本Winter 2013

    Data Abstract Winter 2013即Data Abstract Version 7.0.73 (Build .1111),Winter 2013版对Data Abstract继续做了以 ...

  7. 【BZOJ】1084: [SCOI2005]最大子矩阵(DP)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1084 有一个1A--- 本题没看懂,,不会啊囧..感觉完全设不了状态..看了题解,囧,m<=2 ...

  8. hdu 1084 What Is Your Grade?

    http://acm.hdu.edu.cn/showproblem.php?pid=1084 What Is Your Grade? Time Limit: 2000/1000 MS (Java/Ot ...

  9. BZOJ 1084: [SCOI2005]最大子矩阵 DP

    1084: [SCOI2005]最大子矩阵 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1084 Description 这里有一个n* ...

随机推荐

  1. MySQL全面瓦解29:使用Partition功能实现水平分区

    1 回顾 上一节我们详细讲解了如何对数据库进行分区操作,包括了 垂直拆分(Scale Up 纵向扩展)和 水平拆分(Scale Out 横向扩展) ,同时简要整理了水平分区的几种策略,现在来回顾一下. ...

  2. 学习java的第十七天

    一.今日收获 1.java完全学习手册第三章算法的3.1比较值 2.看哔哩哔哩上的教学视频 二.今日问题 1.在第一个最大值程序运行时经常报错. 2.哔哩哔哩教学视频的一些术语不太理解,还需要了解 三 ...

  3. A Child's History of England.9

    But, first, as it was important to know how numerous those pestilent Danes were, and how they were f ...

  4. express系列(1)概述

    在 Node.js 出现之前,前后端的开发必须使用不同的语言进行.为此你需要学习多种的语言和框架.有了 Node.js 之后,你就可以使用一门语言在前后端开发中自由切换,这是最吸引人的地方. 什么是 ...

  5. 【Reverse】每日必逆0x01

    附件:https://files.buuoj.cn/files/7458c5c0ce999ac491df13cf7a7ed9f1/SimpleRev 题解 查壳 64位ELF文件,无壳 IDApro处 ...

  6. How is Quality Score Calculated?

    Google determines Quality Score slightly differently for each of the different advertising networks ...

  7. Java Web 实现Mysql 数据库备份与还原

    前段时间某某删库事故付出的惨重代价告诉我们: 数据备份的必要性是企业数据管理极其重要的一项工作. 1. Mysql备份与还原命令 备份命令: mysqldump -h127.0.0.1 -uroot ...

  8. OpenStack之二: 安装OpenStack的yum源及相关组件

    #: 在所有节点执行 [root@localhost ~]# yum install centos-release-openstack-stein -y #: 安装相关组件(只在管理端和计算几点安装) ...

  9. Oracle 用户自定义数据类型

    用户自定义数据类型(User-defined Data Type)oracle支持对象类型(Object Type).嵌套类型(Nested Table Type)和可变数组类型(Varray Dat ...

  10. 使用RabbitMQ搭建MQTT服务

    由于近期公司需要搭建一套物联网采集环境,底层设备采用MQTT协议传输数据.服务器环境为linux,考虑到现有环境已经有RabbitMQ环境,Rabbit是基于AMQP协议开发的一套高效的消息传输队列. ...