喷泉问题

10 2
3
5 3
3 7
9 4

  1. /*
  2.  * 喷泉问题
  3.  * tanliboy Nov.25 2008
  4.  */
  5. #include<iostream>
  6. #include<vector>
  7. #include<cmath>
  8. using namespace std;
  9. template<class T,class O>
  10. int partition(T a,int from,int to,O order)
  11. {
  12.     int cur = from;
  13.     for(int i = from;i<to-1;i++)
  14.     {
  15.         if(a[i] < a[to-1])
  16.         {
  17.             swap(a[i],a[cur]);
  18.             swap(order[i],order[cur]);
  19.             cur++;
  20.         }
  21.     }
  22.     swap(a[cur],a[to-1]);
  23.     swap(order[cur],order[to-1]);
  24.     return cur;
  25. }
  26. template<class T,class O>
  27. void quickSort(T a,int from,int to,O order)
  28. {
  29.     if(from>= to)
  30.     {
  31.         return;
  32.     }
  33.     int p = partition(a,from,to,order);
  34.     quickSort(a,from,p,order);
  35.     quickSort(a,p+1,to,order);
  36. }
  37. int main()
  38. {
  39.     double l,w;
  40.     cin>>l>>w;
  41.     int n;
  42.     cin>>n;
  43.     vector<double> a(n);
  44.     vector<double> b(n);
  45.     for(int i =0;i<n;i++)
  46.     {
  47.         double x,r;
  48.         cin>>x>>r;
  49.         double d=0;
  50.         if(r>w/2)
  51.         {
  52.             d = sqrt(r*r - w*w/4);
  53.         }
  54.         a[i] = x - d;
  55.         b[i] = x + d;
  56.     }
  57.     vector<int> order(n);
  58.     for(int i = 0;i<n;i++)
  59.     {
  60.         order[i] = i;
  61.     }
  62.     quickSort(a.begin(),0,n,order.begin());
  63.     //calculate
  64.     double pos = 0;
  65.     for(int i = 0;i<n;)
  66.     {
  67.         int maxIndex = -1;
  68.         double max = 0;
  69.         while(a[i]<pos && i<n)
  70.         {
  71.             double r = b[order[i]];
  72.             if(r > max)
  73.             {
  74.                 max = r;
  75.                 maxIndex = i;
  76.             }
  77.             i++;
  78.         }
  79.         if(maxIndex == -1)
  80.         {
  81.             //error: can't cover
  82.             cerr<<"error"<<endl;
  83.             return -1;
  84.         }else
  85.         {
  86.             cout<<order[maxIndex]<<endl;
  87.             pos = max;
  88.             if(max >= l)
  89.             {
  90.                 return 0;
  91.             }
  92.         }
  93.     }
  94.     return 0;
  95. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值