equation

equation

枚举分段函数

在每个区间段内求解

切记:cmp函数里面不能有=号

 

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 typedef long long ll;
  4 #define P pair<ll,ll>
  5 P A[100005];
  6 P B[300005];
  7 bool cmp(P a,P b)
  8 {
  9     //if((ll)a.first*b.second==(ll)a.second*b.first)cout<<"NO"<<endl;
 10     return (ll)a.first*b.second<(ll)a.second*b.first;
 11 
 12 }
 13 bool equ(P a,P b)
 14 {
 15     if(a.first==b.first&&a.second==b.second)return true;
 16     return false;
 17 }
 18 int main()
 19 {
 20     ll T;
 21     ll N,C,t,cnt;
 22     P o;
 23     freopen("1.in","r",stdin);
 24     scanf("%lld",&T);
 25     while(T--)
 26     {
 27         cnt=0;
 28         scanf("%lld%lld",&N,&C);
 29         for(int i=0; i<N; i++)
 30         {
 31             scanf("%lld%lld",&A[i].second,&A[i].first);
 32             A[i].first*=-1;
 33         }
 34         sort(A,A+N,cmp);
 35         ll a=0,b=0,c,d;
 36 
 37         bool f=0;
 38         for(int i=0; i<N; i++)
 39         {
 40             b+=A[i].first;
 41             a-=A[i].second;
 42         }
 43         for(int i=0; i<=N; i++)
 44         {
 45             if(i==0);
 46             else
 47             {
 48                 b-=2*A[i-1].first;
 49                 a+=2*A[i-1].second;
 50             }
 51 
 52             if(a==0)
 53             {
 54                 if(b==C)
 55                 {
 56                     puts("-1");
 57                     f=1;
 58                     break;
 59                 }
 60                 else continue;
 61             }
 62             else
 63             {
 64 
 65                 c=C-b;
 66 
 67                 t=__gcd(abs(a),abs(c));
 68                 d=a;
 69                 d/=t;
 70                 c/=t;
 71                 o.first=c;
 72                 o.second=d;
 73                 if(o.second<0)o.second*=-1,o.first*=-1;
 74                 if(((i==0||cmp(A[i-1],o))&&(cmp(o,A[i])||i==N))||equ(A[i],o))
 75                 {
 76                     if(B[cnt-1].first!=o.first||B[cnt-1].second!=o.second){
 77                         B[cnt]=o;
 78                         cnt++;
 79                     }
 80                 }
 81             }
 82 
 83         }
 84         if(f)continue;
 85         else
 86         {
 87             if(cnt==0){
 88                 cout<<cnt<<'\n';
 89                 continue;
 90             }
 91             cout<<cnt<<' ';
 92             for(int i=0; i<cnt-1; i++)
 93             {
 94                 cout<<B[i].first<<'/'<<B[i].second<<' ';
 95             }
 96             cout<<B[cnt-1].first<<'/'<<B[cnt-1].second;
 97             cout<<'\n';
 98         }
 99     }
100 
101 }

 

posted @ 2019-08-05 22:29  liulex  阅读(319)  评论(0编辑  收藏  举报