您的位置:首页 > 其它

poj 1167 The Buses

2015-08-03 18:10 204 查看
The Buses

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 5848Accepted: 1617
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

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<utility>
//#pragma comment(linker, "/STACK:102400000,102400000")
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)
#define  lson   num<<1,le,mid
#define rson    num<<1|1,mid+1,ri
#define MID   int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)

using namespace std;
const int INF =0x3f3f3f3f;
const int maxn=60   ;
//const int maxm=    ;
//const int INF=    ;
//typedef long long ll;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
int n,ans;
int routecnt,hash[65];
struct Route
{
    int start;
    int delta;
    int times;
   Route() {}
     Route(int st,int del,int t):start(st),delta(del),times(t)
    {}
   void Route2(int st,int del,int t)
    {start=st,delta=del,times=t;}

  bool  operator <(const Route &  c)const
    {
        return times>c.times;
    }

}  route[maxn*maxn+5];
bool hasroute(int st,int delta)
{
    for(int i=st;i<=59;i+=delta)
    {
        if(!hash[i])  return false;

    }
    return true;
}
void dfs(int x,int num)
{
    if(num>=ans)  return ;
  if(n==0)  {ans=min(ans,num);return;}
    for(int i=x;i<routecnt;i++)
    {

        if(route[i].times>n)  continue;
        if(num+n/route[i].times>=ans)  return;
        if(!hasroute( route[i].start,route[i].delta       )  )  continue;

        for(int j=route[i].start;j<=59;j+=route[i].delta)
            hash[j]--,n--;

       /* if(n==0)   {ans=min(ans,num+1);return;}//结束条件放在这永远也不对,那不是dfs
        else*/
            dfs(i,num+1 );                   //不能写dfs(i+1,num+1);

        for(int j=route[i].start;j<=59;j+=route[i].delta)
            hash[j]++,n++;

    }

}

int main()
{
    int x;
     scanf("%d",&n);
         routecnt=0;
         memset(hash,0,sizeof hash);
         FOR1(i,n)  scanf("%d",&x),hash[x]++;
//         if(n==0)  {printf("0\n");return 0;}
         for(int i=0;i<30;i++)
         {
             for(int j=i+1;j+i<=59;j++)
             {
                 if(hasroute (i, j )  )
                 {
                      route[  routecnt++].Route2(  i,j,(59-i)/j+1    );
                 }
             }
         }
         sort(route,route+routecnt);
          ans=17;
         dfs( 0,0    );
         printf("%d\n",ans);

    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: