P2249 【深基13.例1】查找
#include<iostream>
using namespace std;
const int N=1e6+5;
int n,m,a[N];
int main(){
ios::sync_with_stdio(false);
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=m;i++){
int t=0;
cin>>t;
int left=1,right=n;
while(left<right){
int mid=(left+right)/2;
if(a[mid]>=t) right=mid;
else left=mid+1;
}
if(a[left]==t) cout<<left<<" ";
else cout<<"-1 ";
}
return 0;
}
P1102 A-B 数对
#include<iostream>
#include<algorithm>
using namespace std;
const int N=2e5+5;
int n,c,a[N];
long long ans;
int main(){
ios::sync_with_stdio(false);
cin>>n>>c;
for(int i=1;i<=n;i++) cin>>a[i]; sort(a+1,a+1+n);
for(int i=1,j=1,k=1;i<=n;i++){
while(j<=i&&a[i]-a[j]>=c){
j++;
}
while(k<=i&&a[i]-a[k]>c){
k++;
}
ans+=j-k;
}
cout<<ans<<endl;
return 0;
}
P1024 [NOIP2001 提高组] 一元三次方程求解
#include<iostream>
#include<cmath>
using namespace std;
const double eps=1e-8;
double a,b,c,d;
void Dsearch1(double l,double r){
while(r-l>eps){
double mid=(l+r)/2;
if(a*pow(mid,3)+b*pow(mid,2)+c*mid+d>=0) r=mid;
else l=mid;
}
printf("%.2lf ",l);
}
void Dsearch2(double l,double r){
while(r-l>eps){
double mid=(l+r)/2;
if(a*pow(mid,3)+b*pow(mid,2)+c*mid+d<=0) r=mid;
else l=mid;
}
printf("%.2lf ",l);
}
int main(){
ios::sync_with_stdio(false);
cin>>a>>b>>c>>d;
double temp=4.0*b*b-12.0*a*c;
double t=sqrt(temp);
double t1=(-2.0*b-t)/6.0/a;
double t2=(-2.0*b+t)/6.0/a;
if(t1>t2) {double tt=t1;t1=t2;t2=tt;}
double flag=100.0;
if(a>0){Dsearch1(-flag,t1);Dsearch2(t1,t2);Dsearch1(t2,flag);}
else {Dsearch2(-flag,t1);Dsearch1(t1,t2);Dsearch2(t2,flag);}
return 0;
}
P1678 烦恼的高考志愿
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=1e6+5;
int n,m,a[N];
long long ans;
int main(){
ios::sync_with_stdio(false);
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>a[i]; sort(a+1,a+1+n);
while(m--){
int score=0;
cin>>score;
int l=1,r=n;
while(l<r){
int mid=(l+r)/2;
if(a[mid]<score) l=mid+1;
else r=mid;
}
int x=abs(score-a[l]);
int y=abs(score-a[l-1]);
if(l==1) y=1000000;
ans+=(long long)min(x,y);
}
cout<<ans<<endl;
return 0;
}
P1163 银行贷款
#include<iostream>
#include<cmath>
using namespace std;
const double eps=1e-8;
int w0,w,m;
bool check(double x){
double sum=0.0;
for(int i=1;i<=m;i++) sum+=w/pow((1.0+x/100),i);
if(sum<=w0) return true;
else return false;
}
void Dsearch(double l,double r){
while(r-l>eps){
double mid=(l+r)/2;
if(check(mid)) r=mid;
else l=mid;
}
printf("%.1lf",l);
}
int main(){
ios::sync_with_stdio(false);
cin>>w0>>w>>m;
double left=0.0,right=300.0;
Dsearch(left,right);
return 0;
}
P1873 [COCI 2011/2012 #5] EKO / 砍树
#include<iostream>
using namespace std;
const int N=1e6+5;
int n,m,a[N];
bool check(int x){
long long temp=0;
for(int i=1;i<=n;i++){
if(a[i]>x) temp+=a[i]-x;
if(temp>=m) return true;
}
return false;
}
int main(){
ios::sync_with_stdio(false);
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>a[i];
int l=1,r=1e6;
while(l<r){
int mid=(r+l+1)>>1;
if(check(mid)) l=mid;
else r=mid-1;
}
cout<<l<<endl;
return 0;
}
P2440 木材加工
#include<iostream>
using namespace std;
const int N=1e5+5;
int n,k,a[N];
bool check(int x){
int temp=0;
for(int i=1;i<=n;i++){
temp+=a[i]/x;
if(temp>=k) return true;
}
return false;
}
int main(){
ios::sync_with_stdio(false);
cin>>n>>k;
for(int i=1;i<=n;i++) cin>>a[i];
int l=0,r=1e8;
while(l<r){
int mid=(l+r+1)/2;
if(check(mid)) l=mid;
else r=mid-1;
}
cout<<l<<endl;
return 0;
}
P2678 [NOIP2015 提高组] 跳石头
#include<iostream>
#include<algorithm>
using namespace std;
const int N=5e4+5;
int l,n,m,a[N];
bool check(int x){
int last=0,cnt=0;
for(int i=1;i<=n+1;i++){
if(a[i]-last<x) cnt++;
else last=a[i];
}
if(cnt<=m) return true;
else return false;
}
int main(){
ios::sync_with_stdio(false);
cin>>l>>n>>m;
for(int i=1;i<=n;i++) cin>>a[i];
a[n+1]=l;
sort(a+1,a+1+n);
int left=0,right=1e8;
while(left<right){
int mid=(left+right+1)/2;
if(check(mid)) left=mid;
else right=mid-1;
}
cout<<left<<endl;
return 0;
}
P3853 [TJOI2007] 路标设置
#include<iostream>
using namespace std;
const int N=1e5+5;
int l,n,k,a[N];
bool check(int x){
long long cnt=0;
for(int i=1;i<n;i++){
long long temp=a[i+1]-a[i];
if(temp%x==0) cnt+=temp/x-1;
else cnt+=temp/x;
}
if(cnt<=k) return true;
else return false;
}
int main(){
ios::sync_with_stdio(false);
//输入
cin>>l>>n>>k;
for(int i=1;i<=n;i++) cin>>a[i];
//整数的二分
int left=1,right=l;
while(left<right){
int mid=(left+right)/2;
if(check(mid)) right=mid;
else left=mid+1;
}
//输出
cout<<left<<endl;
return 0;
}
P1182 数列分段 Section II
#include<iostream>
using namespace std;
const int N=1e5+5;
int n,m,a[N];
bool check(int x){
int cnt=0,temp=0;
for(int i=1;i<=n;i++){
if(temp+a[i]>x) cnt++,temp=a[i];
else temp+=a[i];
}
if(cnt+1<=m) return true;
else return false;
}
int main(){
ios::sync_with_stdio(false);
cin>>n>>m;
int l=0,r=1e9;
for(int i=1;i<=n;i++){
cin>>a[i];
l=max(l,a[i]);
r+=a[i];
}
while(l<r){
int mid=(l+r)>>1;
if(check(mid)) r=mid;
else l=mid+1;
}
cout<<l<<endl;
return 0;
}
P3743 小鸟的设备
#include<iostream>
using namespace std;
const int N=1e5+5;
int n;
double p,a[N],b[N];
bool check(double x){
double maxx=p*x;
double sum=0.0;
for(int i=1;i<=n;i++){
if(a[i]*x<=b[i]) continue;
else sum+=(a[i]*x-b[i]);
}
if(sum<=maxx) return true;
else return false;
}
int main(){
ios::sync_with_stdio(false);
cin>>n>>p;
double temp=0.0;
for(int i=1;i<=n;i++){
cin>>a[i]>>b[i];
temp+=a[i];
}
if(temp<=p){cout<<"-1"<<endl; return 0;}
double l=0.0,r=5e9;
while(r-l>1e-6){
double mid=(l+r)/2;
if(check(mid)) l=mid;
else r=mid;
}
printf("%.10lf",l);
return 0;
}