写个二分真不容易啊....
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
int a[30001];
int gcd(int a,int b)
  {
return b?gcd(b,a%b):a;
}
int bin_find(int k,int l,int r,int t,int cas)
  {
int i,j,x=l,y=r,h;
while(l<r)
 {
h=(l+r)/2;
if(a[k]*a[h]>t)
 {
 if(cas==0) {if(l==h) break; else l=h;}
 else {if(r==h) break; else r=h;}
}
else
 {
if(cas==0) r=h-1;
else l=h+1;
}
}
if(a[l]*a[k]>t)
 {
 if(cas==0) {while(l<=y && a[l]*a[k]>t) l++; l--;}
 else {while(l>=x && a[l]*a[k]>t) l--; l++;}
if(cas==1) return y-l+1;
else return l-x+1;
}
else return 0;
}
int main()
  {
int n,t,i,ca=1,ans1,ans2,temp;
while(scanf("%d%d",&n,&t),n)
 {
int x=0,y=0,z=0;
for(i=0;i<n;i++)
 {
scanf("%d",&a[i]);
if(a[i]<0) x++;
else if(a[i]==0) y++;
else z++;
}
sort(a,a+n);
ans2=n*n;
if(t==0) ans1=x*x+z*z;
else if(t>0)
 {
ans1=0;
for(i=x-1;i>=0;i--)
 if(a[i]*a[i]>t) {ans1+=(i+1)*(i+1);break;}
else ans1+=2*bin_find(i,0,i-1,t,0);
for(i=x+y;i<n;i++)
 if(a[i]*a[i]>t) {ans1+=(n-i)*(n-i);break;}
else ans1+=2*bin_find(i,x+y,n-1,t,1);
}
else
 {
ans1=0;
ans1=(y+z)*(y+z)+2*y*x+x*x;
for(i=0;i<x;i++)
ans1+=2*bin_find(i,x+y,n-1,t,0);
}
temp=gcd(ans1,ans2);
printf("Case %d: %d/%d\n",ca++,ans1/temp,ans2/temp);
}
return 0;
}
|
|
随笔:1
文章:4
评论:5
引用:0
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
30 | 31 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|
常用链接
留言簿(1)
随笔档案
文章档案
搜索
最新评论

|
|