Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 6234   Accepted: 1698

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

Source

 
搜索。预处理出所有可能存在的公交线路,然后DFS,看选用其中的哪些可以正好使用到数据中的所有车各一次,且选用的线路最少。
剪枝1:由于这一小时里一种车至少来两次,所以只有30分以前来的才可能是始发车
剪枝2:对所有可能的公交线路按车停靠次数大小排序,如果当前选用线路数加上“剩余没安排的车除以当前线路需要的车”(即估计需要线路数)比答案大,剪枝
 
 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int ct[];//ct[i]表示第i分钟到达的车数
int n,ans,tp;//车数 答案 总备选线路数
struct node{
int s;//第一次到达时间
int j;//发车间隔
int t;//需要车数
}p[];
int cmp(node a,node b){
return a.t>b.t;
}
bool test(int s,int ti){//s_起始时间 ti_间隔时间
for(int i=s;i<;i+=ti)
if(!ct[i])return false;
return true;
}
void dfs(int t,int now){
int i,j,k,tmp;
if(n==){
if(now<ans)ans=now;
return;
}
for(i=t;i<=tp && p[i].t>n;i++);//寻找合适线路,排除需要车数比剩余车数大的线路
for(k=i;k<=tp;k++){
if(now+n/p[k].t>=ans)return;//剪枝
if(test(p[k].s,p[k].j)){
tmp=p[k].j;
for(j=p[k].s;j<;j+=tmp){
ct[j]--;
n--;
}
dfs(k,now+);
for(j=p[k].s;j<;j+=tmp){
ct[j]++;
n++;
}
}
}
}
int main(){
scanf("%d",&n);
int i,j,a;
for(i=;i<=n;i++){
scanf("%d",&a);
ct[a]++;
}
tp=;
for(i=;i<=;i++){
if(!ct[i])continue;
for(j=i+;j<=-i;j++){
if(test(i,j)){
tp++;
p[tp].s=i;
p[tp].j=j;
p[tp].t=+(-i)/j;
}
}
}
sort(p+,p+tp+,cmp);
ans=;
dfs(,);
printf("%d",ans);
return ;
}

最新文章

  1. CH Round #52 还教室[线段树 方差]
  2. 百度上传工具webuploader,图片上传附加参数
  3. CSS3魔法堂:禁止用户改变textarea大小
  4. javascript中parentNode,childNodes,children的应用详解
  5. HTML5之字体
  6. Windows phone 之独立存储
  7. jsp中Java代码中怎么获取jsp页面元素
  8. Node.js服务的重启与监控
  9. Photon的使用
  10. GreenDao与Rx的完美搭配
  11. PHP简单分页省略中间页码
  12. 你应该知道的 volatile 关键字
  13. 如何引入iconfont图标与Element-UI组件
  14. SpringBoot之RabbitMQ的使用
  15. w3m 在ubuntu中的使用
  16. poj1873(枚举+凸包)
  17. 《LOST》 电视
  18. 让Ubuntu可以压缩/解压缩RAR文件
  19. 谷歌算法研究员:我为什么钟爱PyTorch?
  20. hbase源码系列(十四)Compact和Split

热门文章

  1. Ajax请求出现406的原因
  2. MySql常用数据操作
  3. Golang 谷歌搜索api 实现搜索引擎(前端 bootstrap + jquery)
  4. C#基础-字符串
  5. day13-生成器
  6. Jsoup -- 网络爬虫解析器
  7. JAVA基础篇—String和StringBuffer
  8. Githun&amp;HEXO建站小记
  9. python 学习分享-购物车实操篇
  10. Leetcode with Python