lightoj Winter 1084 (dfs&&bfs求最少堆数) 好题
2015-12-06 19:53
405 查看
LightOJ - 1084
Winter
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
//bfs解
//看了上面两篇大神的,自己又写了一遍,感觉又收获不少。
Winter
Time Limit: 2000MS | Memory Limit: 32768KB | 64bit IO Format: %lld & %llu |
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 100010 using namespace std; int n,k; int a ; int f ; 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 0x3f3f3f3f using namespace std; int a ; int vis ; 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 0x3f3f3f3f using namespace std; int a ; int f ; 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; }
相关文章推荐
- uva 10118 ——Free Candies
- [string]Roman to Integer,Integer to Roman
- 关于Redis数据过期策略
- 查找内存泄露的工具 (转)
- Java41---42
- spring框架学习(三)junit单元测试
- hibernate映射文件配置
- Android开发学习之路-自定义ListView(继承BaseAdapter)
- Objective-C消息模式
- linux常用命令
- 20135220谈愈敏--信息安全系统设计基础第十一周学习总结
- 新版本的 Xcode 7+ Alcatraz 插件安装
- [探测与发现]星际之门计划
- Java-TreeSet的用法-入门
- Execution default-testResources of goal org.apache.maven.plugins:maven-resources-plugin:2.5:testReso
- JUnit的jar包和hamcrest的jar包关系
- python随机数整理
- 中缀表达式,前缀表达式,后缀表达式
- Java中Iterator(迭代器)的用法及其背后机制的探究
- 居中方法