Time Zone

 Data:
    5
0.17 2.50 4.0 2.01 0.02

  1. /*
  2.  * Time Zone Problem
  3.  * tanliboy<tanliboy2003@yahoo.com.cn> Nov. 30 2008
  4.  */
  5. #include<iostream>
  6. #include<vector>
  7. #include<cmath>
  8. using namespace std;
  9. const double tol = 1e-5;
  10. bool find(const vector<double>& v,vector<bool> setted,vector<double>& low,vector<double>& high,int cnt,const int len,vector<double>& res)
  11. {
  12.     if(cnt==len)
  13.     {
  14.         return true;
  15.     }
  16.     for(int i = 0;i<len;i++)
  17.     {
  18.         if(setted[i])
  19.             continue;
  20.         if(low[i] <= cnt && high[i] >= cnt)
  21.         {
  22.             vector<double> l(low);
  23.             vector<double> h(high);
  24.             // set new l and h
  25.             for(int j = 0;j<len;j++)
  26.             {
  27.                 if(setted[i] || j == i)
  28.                     continue;
  29.                 if(j>i)
  30.                 {
  31.                     l[j] = max(l[j], v[i]+ cnt -v[j]);
  32.                 }else
  33.                 {
  34.                     h[j] = max(h[j], v[i] + cnt- v[j]);
  35.                 }
  36.             }
  37.             setted[i] = true;
  38.             // searched in the next level
  39.             if(find(v,setted,l,h,cnt+1,len,res))
  40.             {
  41.                 res[i] = v[i] + cnt;    
  42.                 return true;
  43.             }
  44.             setted[i] = false;
  45.         }
  46.     }
  47.     return false;
  48. }
  49. int main()
  50. {
  51.     int n;
  52.     cin>> n;
  53.     vector<double> v(n);
  54.     for(int i = 0;i< n ;i++)
  55.     {
  56.         cin>>v[i];
  57.     }
  58.     vector<double> low(n,-tol);
  59.     vector<double> high(n,n-1+ tol);
  60.     // set initial low and hight
  61.     for(int i = 0;i<n;i++)
  62.     {
  63.         for(int j = i+1;j<n;j++)
  64.         {
  65.             double lbar = v[i] - v[j];
  66.             double hbar = n-1 +v[j] -v[i];
  67.             low[j] = max(low[j],lbar>0 ? lbar : -tol);
  68.             high[i] = min(high[i], hbar< n-1 ? hbar : n-1 + tol);
  69.         }
  70.     }
  71.     vector<bool> setted(n);
  72.     vector<double> res(n);
  73.     // recursive search.
  74.     if(find(v,setted,low,high,0,n,res))
  75.     {
  76.         for(int i = 0;i<n;i++)
  77.         {
  78.             cout<<res[i]<<endl;
  79.         }
  80.     }else
  81.     {
  82.         cout<<"not found!"<<endl;
  83.     }
  84.     return 0;
  85. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值