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 ;
}

POJ1167 The Buses的更多相关文章

  1. CF459C Pashmak and Buses (构造d位k进制数

    C - Pashmak and Buses Codeforces Round #261 (Div. 2) C. Pashmak and Buses time limit per test 1 seco ...

  2. codeforces 459C Pashmak and Buses 解题报告

    题目链接:http://codeforces.com/problemset/problem/459/C 题目意思:有 n 个 students,k 辆 buses.问是否能对 n 个students安 ...

  3. ural 1434. Buses in Vasyuki

    1434. Buses in Vasyuki Time limit: 3.0 secondMemory limit: 64 MB The Vasyuki University is holding a ...

  4. UVA12653 Buses

    Problem HBusesFile: buses.[c|cpp|java]Programming competitions usually require infrastructure and or ...

  5. cf459C Pashmak and Buses

    C. Pashmak and Buses time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. codeforces #261 C题 Pashmak and Buses(瞎搞)

    题目地址:http://codeforces.com/contest/459/problem/C C. Pashmak and Buses time limit per test 1 second m ...

  7. Problem J: Island Buses

    主要题意是:大海之间有岛,有的岛之间有桥,问你岛的个数,桥的个数,以及没有桥联通岛的个数,其中最后一次输入的没有回车,不注意的话最后一次会被吞,第二,桥的两端的标记是“X”(X也代表陆地),“X”的四 ...

  8. Codeforces 665A. Buses Between Cities 模拟

    A. Buses Between Cities time limit per test: 1 second memory  limit per test: 256 megabytes input: s ...

  9. Educational Codeforces Round 12 A. Buses Between Cities 水题

    A. Buses Between Cities 题目连接: http://www.codeforces.com/contest/665/problem/A Description Buses run ...

随机推荐

  1. ASP.NET WebApi 路由配置

    ASP.NET Web API路由是整个API的入口.我们访问某个资源就是通过路由映射找到对应资源的URL.通过URL来获取资源的. 对于ASP.NET Web API内部实现来讲,我们的请求最终将定 ...

  2. mysql 从陌生到熟练之----数据库备份恢复的实现方法

    mysql 从陌生到熟练之----数据库备份恢复的实现方法 MySQL数据库使用命令行备份|MySQL数据库备份命令 例如: 数据库地址:127.0.0.1 数据库用户名:root 数据库密码:roo ...

  3. cin 和 getline 混用中需要注意的问题

    这段时间在刷题过程中遇到一个cin和getline混合使用中的问题,解决之后记录如下: 先来看一段代码 #include <iostream> #include <string> ...

  4. 【莫队】bzoj4866: [Ynoi2017]由乃的商场之旅

    莫队的一些套路 Description 由乃有一天去参加一个商场举办的游戏.商场派了一些球王排成一行.每个人面前有几堆球.说来也巧,由乃和你 一样,觉得这游戏很无聊,于是决定换一个商场.另一个商场是D ...

  5. (74)zabbix第三方认证之http(nginx basic auth)

    HTTP Basic Auth认证方式,我们将在实例中使用nginx来演示,Apache也类似. zabbix认证配置 Administration>> Authentication,将h ...

  6. 【js】【vue】获取当前dom层

    多层嵌套,$event.currentTarget 指当前点击层

  7. sql执行过长,如何入手优化

    一条sql执行过长的时间,你如何优化,从哪些方面 1.查看sql是否涉及多表的联表或者子查询,如果有,看是否能进行业务拆分,相关字段冗余或者合并成临时表(业务和算法的优化)2.涉及链表的查询,是否能进 ...

  8. Python之路-时间模块

    time模块 import time 时间戳(time.time())--结构化时间(time.localtime)--字符串时间(time.strftime) import time print(t ...

  9. Windows7_64位 NVIDIA 卡 OpenCl环境配置

    序 最近做一个项目需要用到OpenCL,由于之前没有接触过,所以在环境配置第一关就遇到了一些问题,查阅很多资料才配置完成,现在记录如下,希望给一些童鞋一些帮助. 整个步骤也很简单: 了解系统配置,选择 ...

  10. Kubernetes(k8s)底层网络原理刨析

    目录 1 典型的数据传输流程图 2 3种ip说明 3 Docker0网桥和flannel网络方案 4 Service和DNS 4.1 service 4.2 DNS 5 外部访问集群 5.1 外部访问 ...