POJ 3273
二分最小值
#include <stdio.h>
#include <iostream>
#include <queue>
#include <algorithm>
#include <map>
#include <vector>
#include <cmath>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>
#include <set>
#include <stack>
#include <list>
using namespace std;
#define READ freopen("acm.in","r",stdin)
#define WRITE freopen("acm.out","w",stdout)
#define ll long long
#define ull unsigned long long
#define uint unsigned int
#define PII pair<int,int>
#define PDD pair<double,double>
#define fst first
#define sec second
#define MS(x,d) memset(x,d,sizeof(x))
#define INF 1000001000
#define ALL(x) x.begin(),x.end()
#define PB push_back
#define MOD 99991
#define MAX 111111
int n,m;
int mon[200000];
bool C(int x)
{
int cnt=0;
int sum=0;
for(int i=0;i<n;i++)
{
if(mon[i]>x)
return false;
if(sum+mon[i]<=x)
sum+=mon[i];
else
sum=mon[i],cnt++;
if(cnt>m)
return false;
}
cnt++;
return cnt<=m;
}
int solve()
{
int lb=0,ub=INF;
while(ub-lb>1)
{
//cout<<lb<<" "<<ub<<endl;
int m=(ub+lb)/2;
if(C(m))
ub=m;
else
lb=m;
}
return ub;
}
int main()
{
READ;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=0;i<n;i++)
scanf("%d",&mon[i]);
cout<<solve()<<endl;
}
return 0;
}
POJ 3258
二分最大值
#include <stdio.h>
#include <iostream>
#include <queue>
#include <algorithm>
#include <map>
#include <vector>
#include <cmath>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>
#include <set>
#include <stack>
#include <list>
using namespace std;
#define READ freopen("acm.in","r",stdin)
#define WRITE freopen("acm.out","w",stdout)
#define ll long long
#define ull unsigned long long
#define uint unsigned int
#define PII pair<int,int>
#define PDD pair<double,double>
#define fst first
#define sec second
#define MS(x,d) memset(x,d,sizeof(x))
#define INF 1000001000
#define ALL(x) x.begin(),x.end()
#define PB push_back
#define MOD 99991
#define MAX 111111
int L,n,m;
int len[200000];
bool C(int x)
{
int l=0;// 上一个石头和上上一个石头之间的距离 如果上一个石头没被去掉则为0
int cnt=0;
if(x>L)
return false;
for(int i=1;i<n;i++)
{
if(l+len[i]-len[i-1]<x)
cnt++,l+=len[i]-len[i-1];
else
l=0;
}
return cnt<=m;
}
int solve()
{
int lb=0,ub=INF;
while(ub-lb>1)
{
int m=(lb+ub)>>1;
if(C(m))
lb=m;
else
ub=m;
}
return lb;
}
int main()
{
READ;
while(scanf("%d%d%d",&L,&n,&m)!=EOF)
{
len[0]=0;
len[n+1]=L;
for(int i=1;i<=n;i++)
scanf("%d",&len[i]);
n++;
sort(len,len+n+1);
cout<<solve()<<endl;
}
return 0;
}
OJ 上的一道二分+tarjan
http://ecnu.acmclub.com/index.php?app=problem_title&id=29&problem_id=21730
#include <stdio.h>
#include <iostream>
#include <queue>
#include <algorithm>
#include <map>
#include <vector>
#include <cmath>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>
#include <set>
#include <stack>
#include <list>
using namespace std;
#define READ freopen("acm.in","r",stdin)
#define WRITE freopen("acm.out","w",stdout)
#define ll long long
#define ull unsigned long long
#define uint unsigned int
#define PII pair<int,int>
#define PDD pair<double,double>
#define fst first
#define sec second
#define MS(x,d) memset(x,d,sizeof(x))
#define INF 1000001000
#define ALL(x) x.begin(),x.end()
#define PB push_back
#define MOD 99991
#define MAX 11111
int dfn[MAX],low[MAX];
bool instack[MAX];
stack<int> S;
int cnt,cnt_scc;
vector<int> G[MAX];
int n,m;
PII rel[200000];
int tarjan(int u)
{
if(dfn[u]==-1)
{
dfn[u]=low[u]=++cnt;
S.push(u),instack[u]=1;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
if(dfn[v]==-1)
low[u]=min(low[u],tarjan(v));
else if(instack[v])
low[u]=min(dfn[v],low[u]);
}
if(dfn[u]==low[u])
{
while(instack[u])
{
instack[S.top()]=0;
S.pop();
}
cnt_scc++;
}
}
return low[u];
}
bool C(int x)
{
MS(dfn,-1),cnt=cnt_scc=0,MS(instack,0);
for(int i=0;i<MAX;i++)
G[i].clear();
while(!S.empty()) S.pop();
for(int i=0;i<x;i++)
{
int f,t;
f=rel[i].fst;
t=rel[i].sec;
G[f].PB(t);
}
for(int i=1;i<=n;i++)
{
if(dfn[i]==-1)
tarjan(i);
}
return cnt_scc==n;
}
int solve()
{
int lb=0,ub=m+1;
while(ub-lb>1)
{
int mid=(ub+lb)>>1;
if(C(mid))
lb=mid;
else
ub=mid;
}
return lb;
}
int main()
{
// READ;
int cas;
scanf("%d",&cas);
for(int T=1;T<=cas;T++)
{
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
scanf("%d%d",&rel[i].fst,&rel[i].sec);
printf("Case %d: %d\n",T,solve());
}
return 0;
}
二分总还是蛮。。。。厉害的。
这也是一种枚举方式嘛,要求最大值最小值,基本就是二分的标志了。
专心复习考试去了。。