Data:
5
0.17 2.50 4.0 2.01 0.02
5
0.17 2.50 4.0 2.01 0.02
- /*
- * Time Zone Problem
- * tanliboy<tanliboy2003@yahoo.com.cn> Nov. 30 2008
- */
- #include<iostream>
- #include<vector>
- #include<cmath>
- using namespace std;
- const double tol = 1e-5;
- bool find(const vector<double>& v,vector<bool> setted,vector<double>& low,vector<double>& high,int cnt,const int len,vector<double>& res)
- {
- if(cnt==len)
- {
- return true;
- }
- for(int i = 0;i<len;i++)
- {
- if(setted[i])
- continue;
- if(low[i] <= cnt && high[i] >= cnt)
- {
- vector<double> l(low);
- vector<double> h(high);
- // set new l and h
- for(int j = 0;j<len;j++)
- {
- if(setted[i] || j == i)
- continue;
- if(j>i)
- {
- l[j] = max(l[j], v[i]+ cnt -v[j]);
- }else
- {
- h[j] = max(h[j], v[i] + cnt- v[j]);
- }
- }
- setted[i] = true;
- // searched in the next level
- if(find(v,setted,l,h,cnt+1,len,res))
- {
- res[i] = v[i] + cnt;
- return true;
- }
- setted[i] = false;
- }
- }
- return false;
- }
- int main()
- {
- int n;
- cin>> n;
- vector<double> v(n);
- for(int i = 0;i< n ;i++)
- {
- cin>>v[i];
- }
- vector<double> low(n,-tol);
- vector<double> high(n,n-1+ tol);
- // set initial low and hight
- for(int i = 0;i<n;i++)
- {
- for(int j = i+1;j<n;j++)
- {
- double lbar = v[i] - v[j];
- double hbar = n-1 +v[j] -v[i];
- low[j] = max(low[j],lbar>0 ? lbar : -tol);
- high[i] = min(high[i], hbar< n-1 ? hbar : n-1 + tol);
- }
- }
- vector<bool> setted(n);
- vector<double> res(n);
- // recursive search.
- if(find(v,setted,low,high,0,n,res))
- {
- for(int i = 0;i<n;i++)
- {
- cout<<res[i]<<endl;
- }
- }else
- {
- cout<<"not found!"<<endl;
- }
- return 0;
- }