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];
}