Winter(bfs&&dfs)
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.
题意:N个人要在日落前都组成>=3的组,最多走距离k;问组成最少的组数;不能输出-1;
思路:本来自己想着贪心的,最后发现自己想错了,因为聚集点是不确定的,而且要的是最小的组,看了巨巨的有dfs还有bfs的;于是就借鉴了巨巨的思路;感觉应该是优先队列的,巨巨竟然不是优先队列也对了,膜拜;
bfs:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
#define T_T while(T--)
#define mem(x,y) memset(x,y,sizeof(x))
const int MAXN=1e5+100;
int a[MAXN];
int N,K;
int vis[MAXN];
/*struct Node{
int l,r;
friend bool operator < (Node a,Node b){
if(a.l!=b.l)return a.l<b.l;
else return a.r<b.r;
}
};
Node d[MAXN];*/
struct Node{
int pos,gg;
friend bool operator < (Node a,Node b){
return a.gg>b.gg;
}
};
priority_queue<Node>dl;
int bfs(){
Node p,q;
mem(vis,0);
vis[0]=1;
while(!dl.empty())dl.pop();
p.pos=0;p.gg=0;
dl.push(p);
while(!dl.empty()){
p=dl.top();dl.pop();
int temp=p.pos;
if(temp>=N)return p.gg;
while(temp<N&&a[temp]-a[p.pos]<=2*K)temp++;
//PI(temp);puts("");
if(temp-p.pos>=3){
q.pos=temp;q.gg=p.gg+1;
if(!vis[q.pos])
dl.push(q);
vis[q.pos]=1;
}
if(temp-p.pos>=4){
q.pos=temp-1;q.gg=p.gg+1;
if(!vis[q.pos])
dl.push(q);
vis[q.pos]=1;
}
if(temp-p.pos>=5){
q.pos=temp-2;q.gg=p.gg+1;
if(!vis[q.pos])
dl.push(q);
vis[q.pos]=1;
}
}
return -1;
}
int main(){
int T,kase=0;
SI(T);
T_T{
SI(N);SI(K);
for(int i=0;i<N;i++)SI(a[i]);
sort(a,a+N);
//for(int i=0;i<N;i++)PI(a[i]),P_;puts("");
/*int l,r;
for(int i=0;i<N;i++){
d[i].l=lower_bound(a,a+N,a[i]-K)-a;
d[i].r=upper_bound(a,a+N,a[i]+K)-a-1;
}
sort(d,d+N);*/
printf("Case %d: %d\n",++kase,bfs());
}
return 0;
}
dfs:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
#define T_T while(T--)
#define mem(x,y) memset(x,y,sizeof(x))
const int MAXN=1e5+100;
int a[MAXN];
int N,K;
int vis[MAXN];
/*struct Node{
int pos,gg;
friend bool operator < (Node a,Node b){
return a.gg>b.gg;
}
};
priority_queue<Node>dl;
int bfs(){
Node p,q;
mem(vis,0);
vis[0]=1;
while(!dl.empty())dl.pop();
p.pos=0;p.gg=0;
dl.push(p);
while(!dl.empty()){
p=dl.top();dl.pop();
int temp=p.pos;
if(temp>=N)return p.gg;
while(temp<N&&a[temp]-a[p.pos]<=2*K)temp++;
//PI(temp);puts("");
if(temp-p.pos>=3){
q.pos=temp;q.gg=p.gg+1;
if(!vis[q.pos])
dl.push(q);
vis[q.pos]=1;
}
if(temp-p.pos>=4){
q.pos=temp-1;q.gg=p.gg+1;
if(!vis[q.pos])
dl.push(q);
vis[q.pos]=1;
}
if(temp-p.pos>=5){
q.pos=temp-2;q.gg=p.gg+1;
if(!vis[q.pos])
dl.push(q);
vis[q.pos]=1;
}
}
return -1;
}
int main(){
int T,kase=0;
SI(T);
T_T{
SI(N);SI(K);
for(int i=0;i<N;i++)SI(a[i]);
sort(a,a+N);
//for(int i=0;i<N;i++)PI(a[i]),P_;puts("");
printf("Case %d: %d\n",++kase,bfs());
}
return 0;
}*/
int ans,flot;
void dfs(int pos,int gg){
int temp=pos;
if(flot)return;
if(pos>=N){
flot=1;
ans=gg;
return;
}
while(temp<N&&a[temp]-a[pos]<=2*K)temp++;
if(!vis[temp])if(temp-pos>=3)vis[temp]=1,dfs(temp,gg+1);
if(!vis[temp-1])if(temp-pos>=4)vis[temp-1]=1,dfs(temp-1,gg+1);
if(!vis[temp-2])if(temp-pos>=5)vis[temp-2]=1,dfs(temp-2,gg+1);
}
int main(){
int T,kase=0;
SI(T);
T_T{
SI(N);SI(K);
for(int i=0;i<N;i++)SI(a[i]);
sort(a,a+N);
//for(int i=0;i<N;i++)PI(a[i]),P_;puts("");
flot=0;
mem(vis,0);
vis[0]=1;
dfs(0,0);
if(!flot)ans=-1;
printf("Case %d: %d\n",++kase,ans);
}
return 0;
}
Winter(bfs&&dfs)的更多相关文章
- POJ 2227 The Wedding Juicer (优先级队列+bfs+dfs)
思路描述来自:http://hi.baidu.com/perfectcai_/item/701f2efa460cedcb0dd1c820也可以参考黑书P89的积水. 题意:Farmer John有一个 ...
- 邻结矩阵的建立和 BFS,DFS;;
邻结矩阵比较简单,, 它的BFS,DFS, 两种遍历也比较简单,一个用队列, 一个用数组即可!!!但是邻接矩阵极其浪费空间,尤其是当它是一个稀疏矩阵的时候!!!-------------------- ...
- Collect More Jewels(hdu1044)(BFS+DFS)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Cleaning Robot (bfs+dfs)
Cleaning Robot (bfs+dfs) Here, we want to solve path planning for a mobile robot cleaning a rectangu ...
- LeetCode:BFS/DFS
BFS/DFS 在树专题和回溯算法中其实已经涉及到了BFS和DFS算法,这里单独提出再进一步学习一下 BFS 广度优先遍历 Breadth-First-Search 这部分的内容也主要是学习了labu ...
- 图的基本遍历算法的实现(BFS & DFS)复习
#include <stdio.h> #define INF 32767 typedef struct MGraph{ ]; ][]; int ver_num, edge_num; }MG ...
- BFS/DFS算法介绍与实现(转)
广度优先搜索(Breadth-First-Search)和深度优先搜索(Deep-First-Search)是搜索策略中最经常用到的两种方法,特别常用于图的搜索.其中有很多的算法都用到了这两种思想,比 ...
- NOIP2010引水入城[BFS DFS 贪心]
题目描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N 行M 列的矩形,如上图所示,其中每个格子都代表一座城市,每座城市都有一个海拔高度. ...
- HDU 1044 Collect More Jewels(BFS+DFS)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
随机推荐
- 使用SWFUpload插件上传文件
演示代码由两部分组成,包括前台文件和后台文件: 1.前台文件index.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transi ...
- 使用 http://httpbin.org/ 验证代理地址
发现一个很方便的工具,在Linux 下使用 curl http://httpbin.org/ 可以返回当前使用的一些网络信息
- 四轴飞行器1.2.3 STM32F407时钟配置和升级标准库文件
原创文章,欢迎转载,转载请注明出处 这个星期进度比较慢哈,只有周末和晚上下班回来才能做,事件不连续,琐碎的事情又比较多,挺烦的,有多琐碎呢? 1.本人有点小强迫症哈,虽然RTT将文 ...
- 快速的CDN加速服务
jQuery Migrate jQuery官网CDN地址jQuery版本迁移辅助插件,用jquery不同版本开发的程序在修改jquery版本出现的兼容问题可以使用jQuery Migrate解决此问题 ...
- [js - 算法可视化] 汉诺塔(Hanoi)演示程序
前段时间偶然看到有个日本人很早之前写了js的多种排序程序,使用js+html实现的排序动画,效果非常好. 受此启发,我决定写几个js的算法动画,第一个就用汉诺塔. 演示地址:http://tut.ap ...
- 宣布发布全新的 Windows Azure 缓存预览版
全新 Windows Azure 缓存的预览版现已发布.此托管服务可以提供闪电般的数据访问速度,以帮助您构建更具可伸缩性.响应更快的应用程序. 详情如下: · 托管式缓存:这一全新的托管服务为需 ...
- Sunday字符串匹配算法
逛ACM神犇的博客的时候看到的这个神奇的算法 KMP吧,失配函数难理解,代码量长 BF吧,慢,很慢,特别慢. BM吧,我不会写... 现在看到了Sunday算法呀,眼前一亮,神清气爽啊. 字符串匹配算 ...
- localstroge与cookie的区别
HTML5本地存储是一种让网页可以把键值对存储在用户浏览器客户端的方法.像Cookie一样,这些数据不会因为你打开新网站,刷新页面,乃至关闭你的浏览器而消失. 而与Cookie不同的时,这些数据不会每 ...
- android媒体--图库与API层MediaPlayer的交互
众所周知一个媒体播放器新建的几个步骤: Mediaplayer mp = new MediaPlayer(0 mp.setDatasource(xxx); mp.setDispalyer(xxx); ...
- uva 10026 Shoemaker's Problem(排序)
题目连接:10026 Shoemaker's Problem 题目大意:有一个鞋匠接了n双要修的鞋子, 修每双鞋需要d天,每推迟一天修将亏损val元,问按什么样的顺序修鞋可以保证损失最少,如果有多种情 ...