POJ1167 The Buses
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 6234 | Accepted: 1698 |
Description
- 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
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
Sample Input
17
0 3 5 13 13 15 21 26 27 29 37 39 39 45 51 52 53
Sample Output
3
Source
#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的更多相关文章
- 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 ...
- codeforces 459C Pashmak and Buses 解题报告
题目链接:http://codeforces.com/problemset/problem/459/C 题目意思:有 n 个 students,k 辆 buses.问是否能对 n 个students安 ...
- ural 1434. Buses in Vasyuki
1434. Buses in Vasyuki Time limit: 3.0 secondMemory limit: 64 MB The Vasyuki University is holding a ...
- UVA12653 Buses
Problem HBusesFile: buses.[c|cpp|java]Programming competitions usually require infrastructure and or ...
- cf459C Pashmak and Buses
C. Pashmak and Buses time limit per test 1 second memory limit per test 256 megabytes input standard ...
- codeforces #261 C题 Pashmak and Buses(瞎搞)
题目地址:http://codeforces.com/contest/459/problem/C C. Pashmak and Buses time limit per test 1 second m ...
- Problem J: Island Buses
主要题意是:大海之间有岛,有的岛之间有桥,问你岛的个数,桥的个数,以及没有桥联通岛的个数,其中最后一次输入的没有回车,不注意的话最后一次会被吞,第二,桥的两端的标记是“X”(X也代表陆地),“X”的四 ...
- Codeforces 665A. Buses Between Cities 模拟
A. Buses Between Cities time limit per test: 1 second memory limit per test: 256 megabytes input: s ...
- Educational Codeforces Round 12 A. Buses Between Cities 水题
A. Buses Between Cities 题目连接: http://www.codeforces.com/contest/665/problem/A Description Buses run ...
随机推荐
- Bootstrap历练实例:导航内的下拉菜单
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- Mutations-freecodecamp算法题目
Mutations(比较字符串) 要求 如果数组第一个字符串元素包含了第二个字符串元素的所有字符,函数返回true. 不用考虑大小写和字符顺序 思路 将数组中的两个字符串小写化 将第二个数组元素(第二 ...
- NodeJS基础入门-fs文件系统
文件I/O是由简单封装的标准POSIX函数提供.通过require('fs') 使用该模块.所有的方法都有异步和同步的形式. 异步方法的最后一个参数都是一个回调函数.传给回调函数的参数取决于具体方法, ...
- crm项目之整体内容(一)
一.项目背景 YW公司是一家电池供应商,目前由于公司内部的需要,需要做一个CRM项目,需要每一个不同角色的员工登陆系统后处理自己的事情.其流程大致如下: 其项目包括三部分内容: 1.权限分配组件(rb ...
- vijos--繁华的都市
描述 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C的道路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连,两个交叉路口之间最多有一条道路 ...
- 迷宫问题&MakeFile
先看一个有意思的问题, 我们定义一个二维数组表示迷宫. 它表示一个迷宫, 其中的1表示墙壁,0表示可以走的路, 只能横着走或竖着走,不能斜着走, 我们要编程序找出从左上角到右下角的路线.其实这个问题可 ...
- HTML中块级元素和行内元素的总结和区分。
HTML标签 html标签定义: 是由一对尖括号包裹的单词构成,例如: <html>. 标签不区分大小写<html> 和 <HTML>, 推荐使用小写. 标签分为两 ...
- html--元素显示优先级
HTML元素的显示优先级 一.HTML元素的显示优先级(显示层次问题,哪个在上哪个在下!总是显示在最前面) 帧元素>HTML元素优先,表单元素总>非表单元素优先 ...
- 28、editText只输入英文字母和'-',用于授权码输入
InputFilter filter = new InputFilter() { @Override public CharSequence filter(CharSequence source, i ...
- js中的事件委托和事件代理详解
起因: 1.这是前端面试的经典题型,要去找工作的小伙伴看看还是有帮助的: 2.其实我一直都没弄明白,写这个一是为了备忘,二是给其他的知其然不知其所以然的小伙伴们以参考: 概述: 那什么叫事件委托呢?它 ...