lightoj Winter 1084 (dfs&&bfs求最少堆数) 好题
来源:互联网 发布:windows平板电脑哪个好 编辑:程序博客网 时间:2024/06/11 01:41
Submit Status
Description
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
2
6 10
2
10
15
13
28
9
3 1
1 10 20
Sample Output
Case 1: 2
Case 2: -1
#include<stdio.h>#include<string.h>#include<algorithm>#define INF 0x3f3f3f3f#define N 100010using namespace std;int n,k;int a[N];int f[N];int dfs(int x){if(x>=n)return 0;if(f[x]!=-1)return f[x];int &ans=f[x];int m=upper_bound(a,a+n,a[x]+k)-a;ans=INF;if(m-x>=3)ans=min(ans,dfs(m)+1);if(m-x>=4)ans=min(ans,dfs(m-1)+1);if(m-x>=5)ans=min(ans,dfs(m-2)+1);return ans;}int main(){int t,T=1;scanf("%d",&t);while(t--){scanf("%d%d",&n,&k);k<<=1;int i;for(i=0;i<n;i++)scanf("%d",&a[i]);sort(a,a+n);memset(f,-1,sizeof(f));int ans=dfs(0);if(ans==INF)ans=-1;printf("Case %d: %d\n",T++,ans);}}
//bfs解
#include<stdio.h>#include<string.h>#include<algorithm>#include<queue>#define N 100010#define INF 0x3f3f3f3fusing namespace std;int a[N];int vis[N];int n,k;struct zz{int x;int m;}p,q;int bfs(){queue<zz>qq;memset(vis,0,sizeof(vis));p.m=0;p.x=0;qq.push(p);vis[0]=1;while(!qq.empty()){p=qq.front();qq.pop();if(p.m>=n)return p.x;int tmp=p.m;while(tmp<n&&a[tmp]-a[p.m]<=2*k)tmp++;int num=tmp-p.m;if(num>=3&&!vis[tmp]){q.m=tmp;q.x=p.x+1;qq.push(q);vis[tmp]=1;}if(num>=4&&!vis[tmp-1]){q.m=tmp-1;q.x=p.x+1;qq.push(q);vis[tmp-1]=1;}if(num>=5&&!vis[tmp-2]){q.m=tmp-2;q.x=p.x+1;qq.push(q);vis[tmp-2]=1;}}return -1;}int main(){int t,T=1;scanf("%d",&t);while(t--){scanf("%d%d",&n,&k);for(int i=0;i<n;i++)scanf("%d",&a[i]);sort(a,a+n);int ans=bfs();printf("Case %d: %d\n",T++,ans);}return 0;}
//看了上面两篇大神的,自己又写了一遍,感觉又收获不少。
#include<stdio.h>#include<string.h>#include<algorithm>#define N 100010#define INF 0x3f3f3f3fusing namespace std;int a[N];int f[N];int n,k;int dfs(int x){if(x>=n)return 0;if(f[x]!=-1)return f[x];int m=upper_bound(a,a+n,a[x]+k)-a;int ans=INF;if(m-x>=3)//一个都不给 ans=min(ans,dfs(m)+1);if(m-x>=4)//分一个给后面 ans=min(ans,dfs(m-1)+1);if(m-x>=5)//分两个给后面 ans=min(ans,dfs(m-2)+1);f[x]=ans;return f[x];}int main(){int t,T=1;scanf("%d",&t);while(t--){scanf("%d%d",&n,&k);k<<=1;for(int i=0;i<n;i++)scanf("%d",&a[i]);sort(a,a+n);memset(f,-1,sizeof(f));int ans=dfs(0);if(ans==INF)ans=-1;printf("Case %d: %d\n",T++,ans);}return 0;}
- lightoj Winter 1084 (dfs&&bfs求最少堆数) 好题
- nyoj 最少步数(BFS,DFS)
- 【NYOJ】-最少步数(DFS || BFS&&队列)
- DFS && BFS 最少步数(nyoj 58)
- NYOJ 58-最少步数【DFS&&BFS】
- nyoj--58--最少步数(bfs&dfs)
- POJ 3126 Prime Path (BFS求最少步数)
- lightoj 1084 - Winter DP
- lightoj 1084 - Winter
- 最少步数(BFS)
- 最少步数(bfs)
- 最少步数 南阳oj 58【DFS||BFS】
- NYOJ58 最少步数(深搜DFS+广搜BFS+剪枝)
- LightOJ--1049--One Way Roads(dfs)(好题)
- nyoj 58 最少步数(BFS)
- NYOJ58最少步数(bfs)
- ny 58 最少步数(bfs)
- nyoj 58 最少步数 (bfs)
- 我对SSDA方法的理解
- C#之初识
- RTP协议的报文头格式结构
- online_judge_1024
- CSS transition的过渡效果
- lightoj Winter 1084 (dfs&&bfs求最少堆数) 好题
- CentOS编译PHP的错误信息处理
- 中断与性能
- iOS项目加载本地的html文件
- 设计模式之—装饰者
- 煤泥水浓度-20151124
- 粘接引理
- 文件操作(3)--fread
- 堆的实现