poj 1167 the buses
来源:互联网 发布:网络视听许可证续办 编辑:程序博客网 时间:2024/05/22 10:50
The Buses
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 6239 Accepted: 1699
Description
A man arrives at a bus stop at 12:00. He remains there during 12:00-12:59. The bus stop is used by a number of bus routes. The man notes the times of arriving buses. The times when buses arrive are given.
• Buses on the same route arrive at regular intervals from 12:00 to 12:59 throughout the entire hour.
• Times are given in whole minutes from 0 to 59.
• Each bus route stops at least 2 times.
• The number of bus routes in the test examples will be <=17.
• Buses from different routes may arrive at the same time.
• Several bus routes can have the same time of first arrival and/or time interval. If two bus routes have the same starting time and interval, they are distinct and are both to be presented.
Find the schedule with the fewest number of bus routes that must stop at the bus stop to satisfy the input data. For each bus route, output the starting time and the interval.
Input
Your program is to read from standard input. The input contains a number n (n <= 300) telling how many arriving buses have been noted, followed by the arrival times in ascending order.
Output
Your program is to write to standard output. The output contains one integer, which is the fewest number of bus routes.
Sample Input
17
0 3 5 13 13 15 21 26 27 29 37 39 39 45 51 52 53
Sample Output
3
【分析】
dfs+剪枝
【代码】
#include<cstdio>#include<cstring>#include<cstdlib>#include<cstdlib>int a[65],n,m,p[20];bool sc(int x,int mm){ if (mm>m) return 0; if (x==n) { for (int i=1;i<=mm;i++) if (p[i]!=-1) return 0; return 1; } int i,j,k,y,z,temp,t,arr[65]; bool b; k=0; while (!a[k]) k++; for (i=1;i<=mm;i++) if (p[i]!=-1&&2*p[i]-k<0) { b=1; t=0; memset(arr,0,sizeof(arr)); for (j=k;j<=59;j+=(k-p[i])) if (a[j]) arr[++t]=j; else { b=0; break; } if (b) { temp=p[i]; p[i]=-1; for (j=1;j<=t;j++) a[arr[j]]--; if (sc(x+t,mm)) return 1; p[i]=temp; for (j=1;j<=t;j++) a[arr[j]]++; } } if (k<30) { p[++mm]=k; a[k]--; if (sc(x+1,mm)) return 1; p[mm]=-1; a[k]++; } return 0;}int main(){ int i,j,x,y,z; scanf("%d",&n); for (i=1;i<=n;i++) { scanf("%d",&x); a[x]++; } memset(p,0xff,sizeof(p)); for (m=1;m<=17;m++) if (sc(0,0)) { printf("%d\n",m); return 0; }}
- poj 1167 The Buses
- POJ 1167 The Buses
- poj 1167 the buses
- POJ 1167 The Buses 笔记
- The Buses (poj 1167 搜索)
- POJ 1167 The Buses 暴搜+剪枝
- 【搜索+强剪枝】PKU-1167-The Buses
- PKU1167 the buses
- hdoj-2273-The buses
- HDU-2273 The buses
- HDU2273-The buses
- IOI 1994 169:The Buses
- Buses
- Address Lines and Buses
- CF D. Buses
- Shared buses vs NoCs
- CF 101B - Buses
- cf459C Pashmak and Buses
- 腾讯校招面试题
- 【软工】概要设计
- 16 删除节点 17创建节点
- React + Redux 入门(一):抛开 React 学 Redux
- N的阶乘末尾有多少个0
- poj 1167 the buses
- 移动web学习笔记(1)
- java中常用基本类API
- php中is_uploaded_file()函数的用法
- 封装一个转换成千分符的函数
- 对于自己初学JAVA代码写及体会
- Python for Data Analysis (3)
- Mybatis传多个参数
- [Python]使用Scrapy爬虫框架简单爬取图片并保存本地