1084 - Winter

  PDF (English) Statistics Forum
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-3;)并且这个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 }
时间: 2024-08-24 18:43:09

1084 - Winter的相关文章

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

题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1084 题目大意: 有n个点在一条以零为起点的坐标轴上,每个点最多可以移动k,问最终能不能把所有点都聚集在大于等于三个点的集合里面,如果能最少需要几个这样的集合? 解题思路: 刚开始看到题目感觉好简单,就开始了sort然后贪心之旅,这就是错误的开始.最后发现这样并不行,然后再Alex的提醒下想用单调队列优化,在我愚昧的理解下竟然写成了bfs,提交竟然ac了,surprise~

Winter(bfs&amp;&amp;dfs)

1084 - Winter   PDF (English) Statistics Forum 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 s

BZOJ 1084 最大子矩阵 终于过了

一开始看到这道题,由于觉得m <= 2, 所以觉得这是道水题,回去后想了一下.在晚上来机房的时候已经想出来了,但是我必须承认细节决定成败.远在一个小时前我就已经把算法的主体都写好了,但是就是一直WA,为什么就是各种粗心,真心想捏死自己.一个小时就这么白白浪费了.我希望明天的我能变得强大一点.在有了今日惨痛的教训之后. 这道题并不难.用d[i][j][k] 来表示状态.i表示第几行,j表示之前取了多少个矩阵,k表示上一行的状态.即上一行的矩阵取法.如果k == 0 那么没有一个矩阵延伸到上一行,如

51nod 1084 矩阵取数问题 V2

1084 矩阵取数问题 V2 基准时间限制:2 秒 空间限制:131072 KB 一个M*N矩阵中有不同的正整数,经过这个格子,就能获得相应价值的奖励,先从左上走到右下,再从右下走到左上.第1遍时只能向下和向右走,第2遍时只能向上和向左走.两次如果经过同一个格子,则该格子的奖励只计算一次,求能够获得的最大价值. 例如:3 * 3的方格. 1 3 3 2 1 3 2 2 1 能够获得的最大价值为:17.1 -> 3 -> 3 -> 3 -> 1 -> 2 -> 2 -&g

1084: [SCOI2005]最大子矩阵

1084: [SCOI2005]最大子矩阵 Description 这里有一个n*m的矩阵,请你选出其中k个子矩阵,使得这个k个子矩阵分值之和最大.注意:选出的k个子矩阵不能相互重叠. Input 第一行为n,m,k(1≤n≤100,1≤m≤2,1≤k≤10),接下来n行描述矩阵每行中的每个元素的分值(每个元素的分值的绝对值不超过32767). Output 只有一行为k个子矩阵分值之和最大为多少. Sample Input 3 2 2 1 -3 2 3 -2 3 Sample Output 9

poj 1084 Square Destroyer dlx解重复覆盖

分析: 将问题转化为重复覆盖问题,DancingLink解决. 代码: //poj 1084 //sep9 #include <iostream> using namespace std; const int maxN=10024; const int maxL=128; int L[maxN],R[maxN],U[maxN],D[maxN]; int C[maxN],H[maxN]; int S[maxN],A[maxN],X[maxN]; bool makeup[maxL][maxL];

HDU 1084 [What Is Your Grade?] 结构体排序

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1084 题目大意:共5道题,N个学生.做出5道满分,0道50分.做出1-4道的同学,若在前50%(向下取整),则获得95.85.75.65,若在后50%则获得90.80.70.60. 关键思想:结构体排序 //结构体排序,考虑边界 #include <iostream> #include <algorithm> #include <cmath> #include <me

BZOJ 1084 最大子矩阵

http://www.lydsy.com/JudgeOnline/problem.php?id=1084 思路:分m=1和m=2操作 1 #include<algorithm> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 #include<iostream> 6 int f[1005][1005],F[1005][1005][11]; 7 int sum[10005],su

【转载】STA 4273H Winter 2015 - Lectures

STA4273H Home   Lecture Schedule/Notes Assignments/Project Computing   Ruslan Salakhutdinov Homepage http://www.cs.toronto.edu/~rsalakhu/   STA 4273H Winter 2015 - Lectures Video Archive here. Lecture Schedule Lecture 1 -- Machine Learning:Introducti