一些二分题

POJ2976:

/*
 * POJ2976DroppingTests.cpp
 *
 *  Created on: 2014年7月25日
 *      Author: Prophet
 */
#include<stdio.h>
#include<algorithm>
const int MAX_N = 1000+10;
#define eps 1e-6
using namespace std;

int n,k;
double a[MAX_N],b[MAX_N],t[MAX_N];

double binary_judge(double mid);
int main(){
	while(scanf("%d%d",&n,&k),n+k>0){
		for(int i=0;i<n;i++)
			scanf("%lf",&a[i]);
		for(int i=0;i<n;i++)
			scanf("%lf",&b[i]);

		double l=0.0,r=1.0;
		while(r-l>eps){
			double mid = (l+r)/2;
			if(binary_judge(mid) > 0)//如果用mid作为比例的值大于0,说明mid取小了
				l = mid;
			else
				r = mid;
		}
		//printf("%d\n",(int)(100*r));
		 printf("%.0f\n", l * 100);
	}
}

double binary_judge(double mid){
	for(int i=0;i<n;i++)
		t[i] = a[i] - mid*b[i];
	sort(t,t+n);
	double sum = 0;
	for(int i=k;i<n;i++)
		sum+=t[i];
	return sum;
}


POJ3579:

/*
 * POJ3579Median.cpp
 *
 *  Created on: 2014年7月25日
 *      Author: Prophet
 */
#include<stdio.h>
#include<algorithm>
using namespace std;

const int MAX_N = 100000+10;
int N,m;
int a[MAX_N];

bool binary_judge(int x);
int main(){
	while(scanf("%d",&N)!=EOF){
		for(int i=0;i<N;i++)
			scanf("%d",&a[i]);

		sort(a,a+N);
		m = (N-1)*N/2;
		int l=0 , r=a[N-1]-a[0];
		while(r-l>1){
			int mid = (l+r)/2;
			if(binary_judge(mid))
				l = mid;
			else
				r = mid;
		}
		printf("%d\n",l);
	}
	return 0;
}

bool binary_judge(int x){
	int count=0;//记录以x为差时,满足
	for(int i=0;i<N;i++){
		count += a+N - lower_bound(a,a+N,a[i]+x);
		if(count == 0)
			break;
	}
	/*if(count < m/2)//若有少于一半的差满足条件,则说明选取的x大了
		return false;*/
	return count > m/2;
	/*int cnt = 0;
		for(int i = 0; i < N; i++)
		{
			cnt += a+N - lower_bound(a, a+N, a[i]+x);
			if(cnt == 0) break;
		}
		return cnt > m/2;
		if(cnt < m/2)//若有少于一半的差满足条件,则说明选取的x大了
				return false;
			else
				return true;*/
}



POJ3662:

/*
 * POJ3662TelephoneLines.cpp
 *
 *  Created on: 2014年7月27日
 *      Author: Prophet
 */
#include<stdio.h>
#include<vector>
#include<queue>
using namespace std;
const int MAX_N = 1000+16;
//const int MAX_V = 10000+10;
const int MAX_L = 1000000;
const int INF = 100000000;

struct edge{
	int to,cost;
};

vector<edge> G[MAX_N];
int d[MAX_N];
int N,P,K;

int dijkstra(int s,int x);
int main(){
	scanf("%d%d%d",&N,&P,&K);
	for(int i=0;i<P;i++){
		int from,to,cost;
		scanf("%d%d%d",&from,&to,&cost);
		from--;
		to--;
		edge e1;e1.to = to;e1.cost = cost;
		edge e2;e2.to = from;e2.cost = cost;
		G[from].push_back(e1);
		G[to].push_back(e2);
	}

	int lb = 0,ub = MAX_L+2;
	while(ub-lb>1)
	{
		int mid = (ub+lb)/2;
		if(dijkstra(0,mid)>K)
			lb = mid;
		else
			ub = mid;
	}
	printf("%d\n",lb>MAX_L?-1:lb);
	return 0;
}

int dijkstra(int s,int x){
	priority_queue<pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > que;
	fill(d,d+N,INF);
	d[s] = 0;
	que.push(pair<int,int>(0,s));

	while(!que.empty()){
		pair<int,int> p = que.top();
		que.pop();
		int v = p.second;
		if(d[v] < p.first)
			continue;
		for(int i=0;i<(int)G[v].size();i++){
			edge e = G[v][i];
			int new_d = d[v] + (e.cost>=x?1:0);
			if(d[e.to]>new_d){
				d[e.to] = new_d;
				que.push(pair<int,int>(d[e.to],e.to));
			}
		}
	}
	return d[N-1];
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值