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. spring mvc + freemarker 整合

    <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.s ...

  2. python - 日期处理模块

    首先就是模块的调用,很多IDE都已经安装好了很多Python经常使用到的模块,所以我们暂时不需要安装模块了. ? 1 2 3 import datetime import time import ca ...

  3. UI调试神器 for ios:Reveal的使用与破解

    aaarticlea/jpeg;base64,/9j/4AAQSkZJRgABAQEAkACQAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aH

  4. ubuntu下RedisDesktopManager的安装,redis可视化工具

    官方网站:https://redisdesktop.com/download 一句命令行解决: sudo snap install redis-desktop-manager 或者直接通过软件管理中心 ...

  5. linux SVN安装及配置教程

    1.环境centos6.4 2.安装svnyum -y install subversion 3.配置 建立版本库目录mkdir /www/svndata svnserve -d -r /www/sv ...

  6. 用Python抓取并分析了1982场英雄联盟数据,教你开局前预测游戏对局胜负!

    英雄联盟想必大多数读者不会陌生,这是一款来自拳头,由腾讯代理的大型网络游戏,现在一进网吧,你就能发现一大片玩英雄联盟的人.在2017年中国战队无缘鸟巢的世界总决赛后,一大片人选择了弃游,只是终究没躲过 ...

  7. leetcode-19-merge

    88. Merge Sorted Array 解题思路: 需要注意,两个数组是排好序的,且nums1够大.所以从两个数组的尾端开始比较,大的那个放在nums1的尾部,并且放了之后就可以前进. 例如nu ...

  8. 自动设置IP地址bat脚本

    自动获取IP及DNS: netsh interface ip set address name="本地连接" source=dhcpnetsh interface ip set d ...

  9. 【HIHOCODER 1599】逃离迷宫4

    描述 小Hi被坏女巫抓进一座由无限多个格子组成的矩阵迷宫. 小Hi一开始处于迷宫(x, y)的位置,迷宫的出口在(a, b).小Hi发现迷宫被女巫施加了魔法,假设当前他处在(x, y)的位置,那么他只 ...

  10. Servlet注意事项

    注意事项 1.对于Servlet的应用程序的目录结构来说,若想让有些文件Servlet可以访问,而用户不能访问的时候,可以将其放置在WEB-INF目录下 2.ServletResponse中getwr ...