博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1084 - Winter
阅读量:4568 次
发布时间:2019-06-08

本文共 2975 字,大约阅读时间需要 9 分钟。

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
2 #include
3 #include
4 #include
5 #include
6 #include
7 using namespace std; 8 typedef long long LL; 9 int dp[100005];10 typedef struct pp11 {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
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 else44 {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 else66 {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

你可能感兴趣的文章
面向对象(五)
查看>>
android平台下使用点九PNG技术
查看>>
Python学习3,列表
查看>>
最长回文子串
查看>>
JAVA基础-JDBC(一)
查看>>
js中for和while运行速度比较
查看>>
算法第5章作业
查看>>
7.9 练习
查看>>
基于ArcGIS JS API的在线专题地图实现
查看>>
learnByWork
查看>>
lua 函数
查看>>
Git的基本命令
查看>>
四平方和
查看>>
第十八周 12.27-1.2
查看>>
C# IP地址字符串和数值转换
查看>>
TCHAR和CHAR类型的互转
查看>>
常用界面布局
查看>>
C语言—— for 循环
查看>>
IBM lotus9.0测试版即将公测
查看>>
xml常用方法
查看>>