1084 - Winter

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 }

 

转载于:https://www.cnblogs.com/zzuli2sjy/p/5509957.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: openwrt-gdq-winter2022是指OpenWrt开源路由器固件的一个分支,特指2022年冬季的版本。OpenWrt是一个基于Linux的操作系统,用于路由器和嵌入式设备,它提供了一种更开放、灵活的方式来定制和管理网络设备。 在openwrt-gdq-winter2022版本中,开发团队通常会对以前的版本进行一些改进和更新。可能会包括新的功能、修复现有问题、增强安全性等。这意味着用户可以享受到更好的性能、更稳定的网络连接和更多的自定义选项。 OpenWrt的主要优点之一是它的灵活性。用户可以利用openwrt-gdq-winter2022版本的各种选项来自定义路由器的功能和行为,包括网络设置、安全功能、流量控制等。它还支持各种各样的应用程序和软件包,可以满足用户不同的需求,比如设置即时通讯服务器、搭建网络存储设备等。 OpenWrt-gdq-winter2022版本的一个重要更新可能是更好的兼容性和支持。它可能包括更多的硬件设备和路由器型号,使更多的用户能够使用OpenWrt固件来改善他们的网络。 总之,openwrt-gdq-winter2022是OpenWrt的一个特定版本,旨在改进路由器和嵌入式设备的性能和功能,并提供更好的自定义选项和更广泛的兼容性和支持。用户可以通过更新到该版本来享受到更好的网络体验。 ### 回答2: openwrt-gdq-winter2022是一个项目的名称,可能是指计划在2022年冬季开发的OpenWrt固件的特定版本。OpenWrt是一款开源的嵌入式操作系统,专注于路由器和嵌入式设备。它提供了一个稳定、安全和灵活的平台,使用户可以自定义和优化他们的网络设备。 在OpenWrt团队的指导下,openwrt-gdq-winter2022项目的目标可能是改进现有的OpenWrt版本,修复漏洞和错误,并加入新的功能和性能优化。通过这个项目的开发,可以期望得到一个更稳定、更安全且更高效的OpenWrt固件版本。 项目可能会涉及不同的技术方面,例如网络协议栈、无线网络驱动程序、IPv6支持、安全性、网络管理等。团队成员可能需要具备扎实的网络知识和开发经验,以便解决各种与路由器和嵌入式设备相关的问题。 参与openwrt-gdq-winter2022项目的开发工作有多个方面的好处。首先,团队成员可以通过参与开源项目,提升自己的技术能力和经验。其次,他们可以为OpenWrt社区做出贡献,帮助改进固件的质量和功能,使用户获得更好的体验。最后,借此机会,他们还可以与其他开发者进行合作,共同解决技术难题,并在技术交流和合作中建立网络。 总之,openwrt-gdq-winter2022是一个计划在2022年冬季开发的OpenWrt固件的特定版本的项目。通过参与该项目的开发,人们可以提升自己的技术能力,改进固件的质量和功能,并为开源社区做出贡献。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值