HOJ 2651 pie
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#define PI acos(-1)//PI可以用 acos(-1)定义
#define ll long long
#define N 10010
ll r[N],area[N],low,high,mid;
int n,f;
bool judge(ll mid){
int num=0;
for(int i=0;i<n;i++){
num+=area[i]/mid;
}
return num>f;
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&f);
ll s=0,mid,ans;
for(int i=0;i<n;i++){
scanf("%d",&r[i]);
area[i]=r[i]*r[i]*PI*1000000;//提高二分精度
s+=area[i];
}
low=0;
high=s;
while(low<=high){
mid=(low+high)/2;
if(judge(mid))
{
low=mid+1;
ans=mid;
}
else
high=mid-1;
}
printf("%.4lf\n",(double)ans/1000000);
}
}
HOJ 2278 - IP Filtering
#include <cstdio>
#include <iostream>
#include <algorithm>
#include<iostream>
using namespace std;
#define ll long long
char str[30];
struct IP{
ll start,ter;
bool operator < (const IP &a){
if(start!=a.start) return start<a.start;
}
}ip[1000010];
ll convert(char *s){
int a,b,c,d;
sscanf(s,"%d.%d.%d.%d",&a,&b,&c,&d);//将str按照格式读出为a,b,c,d四种运算
return((a<<24)+(b<<16)+(c<<8)+d);//<<左移n,相当于乘以2的n次方,>>为除
}
int main()
{
int cur=0;
while(scanf("%s",&str)!=EOF){
if(str[0]=='#') break;
ip[cur].start=convert(str);
scanf("%s",&str);
ip[cur].ter=convert(str);
if (ip[cur].start>ip[cur].ter) swap(ip[cur].start,ip[cur].ter);
cur++;
}
sort(ip,ip+cur);
while(scanf("%s",&str)!=EOF){
ll temp=convert(str);
int low=0,high=cur-1,mid,res=0;
while(low<=high){
mid=(low+high)>>1;
if(temp>=ip[mid].start&&temp<=ip[mid].ter){
res=1;
break;
}
else if(temp>ip[mid].ter){
low=mid+1;
}
else{
high=mid-1;
}
}
if(res) printf("yes\n");
else printf("no\n");
}
return 0;
}
ZOJ 3203 Light Bulb
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
const double esp=1e-8;
double H,h,D;
double cal(double x)
{
return D-x+H-(H-h)*D/x;
}
double three_devide(double l,double r)
{
double left=l,right=r,mid,midmid;
while (left+esp<right)
{
mid=(right+left)/2;
midmid=(right+mid)/2;
if (cal(mid)>=cal(midmid))
right=midmid;
else
left=mid;
}
return cal(right);
}
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
scanf("%lf%lf%lf",&H,&h,&D);
double l=D-h*D/H,r=D;//l为人的影子刚好投影到墙角距离灯的距离
double ans=three_devide(D-h*D/H,r);
printf("%.3lf\n",ans);
}
return 0;
}
HOJ 2608 - Assemble
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
int kind[1010];
int m,num,budge;
struct component{
char type[25];
char para[25];
int price;
int quality;
bool operator <(const component &a) const {
return strcmp(type,a.type)<0 || (strcmp(type,a.type) == 0 && quality<a.quality);
}
}comp[1005];
bool judge(int mid){
int sum=0,tmin;
for(int i=0;i<m-1;i++){
tmin= 0x3f3f3f3f;
for(int j = kind[i];j < kind[i+1];j++){
if(comp[j].quality >= mid){//在每一类中找到质量大于或者等于MID的型号,选择价格较低的
tmin = (comp[j].price < tmin)?comp[j].price:tmin;
}
}
if(tmin == 0x3f3f3f3f)//如果为此值那么表示该类别中的质量都小于mid 因为min 是一个超大值
return false;
sum += tmin;
}
if(sum<= budge)//低于预算,则这样的质量可以接受,试着查找更高质量的
return true;
else//高出预算,不行找更低质量的
return false;
}
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
scanf("%d%d",&num,&budge);
int qmax= 0;
int qmin= 0x3f3f3f3f;
for(int i=0;i<num;i++){
scanf("%s%s%d%d",&comp[i].type,&comp[i].para,&comp[i].price,&comp[i].quality);
qmax=(comp[i].quality>qmax)?comp[i].quality:qmax;
qmin=(comp[i].quality<qmin)?comp[i].quality:qmin;
}
sort(comp,comp+num);
memset(kind,0,sizeof(kind));
m=1;
for(int i = 1;i < num;i++){
if(strcmp(comp[i].type,comp[i-1].type))//找到每一类的起始位置也就是前一类的终止位置
kind[m++] = i;
}
kind[m++] = num;
int low,high,mid,res=0,ans;
low=qmin;
high=qmax;
while(low<=high){
mid=(low+high)>>1;
if(judge(mid)){
low=mid+1;
ans=mid;
res=1;
}
else
high=mid-1;
}
if(res)
printf("%d\n",ans);
else
printf("0\n");
}
return 0;
}