1,acwing 102.最佳牛围栏;2,有向图的强连通分量:eg1:acwing 1174.受欢迎的牛
1,最佳牛围栏;
二分答案;
答案就从0~max(ai)里二分找,(答案一定在这个区间);
如何在0~n区间里找到符合答案的k的划分呢,采用双指针和前缀和预处理;
i=0,j=f,i和j同时右移,用minv来存储i~j-f的最小值,因为sj-最小值最容易找到解;
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rep1(i,a,n) for( int i=a;i<n;++i)
#define rep2(i,a,n) for( int i=a;i<=n;++i)
#define per1(i,n,a) for( int i=n;i>a;i--)
#define per2(i,n,a) for( int i=n;i>=a;i--)
#define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
#define memset(a,i,b) memset((a),(i),sizeof (b))
#define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
#define pro_q priority_queue
#define pb push_back
#define pf push_front
#define endl "\n"
#define lowbit(m) ((-m)&(m))
#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define Yes cout<<"Yes\n"
#define No cout<<"No\n"
#define yes cout<<"yes\n"
#define no cout<<"no\n"
#define yi first
#define er second
using namespace std;
typedef pair<int,int> PII;
typedef pair<long long,long long>PLL;
typedef pair<int,PII> PIII;
typedef long long ll;
typedef double dob;
const int N=1e5+10;
dob a[N],s[N];
int n,f;
dob mins=0;
bool check(dob mid)
{
rep2(i,1,n)s[i]=s[i-1]+a[i]-mid;
dob minv=0;
for(int i=0,j=f;j<=n;i++,j++)
{
minv=min(minv,s[i]);
if(s[j]-minv>=0)return 1;
}
return 0;
}
signed main()
{
quick_cin();
cin>>n>>f;
dob l=0,r=0;
rep2(i,1,n)
{
cin>>a[i];
r=max(r,a[i]);
}
while(r-l>1e-5)
{
dob mid=(r+l)/2;
if(check(mid))l=mid;
else r=mid;
}
cout<<(int)(r*1000);
return 0;
}
2,有向图的强连通分量;
连通分量的定义:图的极大连通子图被称为连通分量(从并查集角度理解,就是一个集合即为一个连通分量);
强连通分量:图的极大连通分量;(加上一个点后不再是连通分量);
求强连通分量往往是将有向有环图通过缩点转化成有向无环图,即拓扑图;
缩点的方式:
存在以上两种情况,说明该点在强连通分量里;
算法:tarjan算法求scc(强连通分量);
做法就是dfs,模板:
void tarjan(int u)
{
dfn[u]=low[u]=++timestamp;
skt[++top]=u,in_skt[u]=1;
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
if(!dfn[j])
{
tarjan(j);
low[u]=min(low[u],low[j]);
}
else if(in_skt[j])
low[u]=min(low[u],dfn[j]);
}
if(dfn[u]==low[u])
{
int y;
++scc_cnt;
do
{
y=skt[top--];
in_skt[y]=0;
id[y]=scc_cnt;
} while (y!=u);
}
}
eg1:受欢迎的牛;
先来看朴素做法:对于每个点,建反图,看是否能遍历到除所选点之外的所有点,遍历一次时间复杂度O(n+m),共n个点,所以O(n*(n+m));
考虑拓扑图,如果是拓扑图的话,就看有几个终点(拓扑图最终总是要走到一个出度为0的点,这个点即为终点);所以答案只能是0和1;
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rep1(i,a,n) for( int i=a;i<n;++i)
#define rep2(i,a,n) for( int i=a;i<=n;++i)
#define per1(i,n,a) for( int i=n;i>a;i--)
#define per2(i,n,a) for( int i=n;i>=a;i--)
#define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
#define memset(a,i,b) memset((a),(i),sizeof (b))
#define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
#define pro_q priority_queue
#define pb push_back
#define pf push_front
#define endl "\n"
#define lowbit(m) ((-m)&(m))
#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define Yes cout<<"Yes\n"
#define No cout<<"No\n"
#define yes cout<<"yes\n"
#define no cout<<"no\n"
#define yi first
#define er second
using namespace std;
typedef pair<int,int> PII;
typedef pair<long long,long long>PLL;
typedef pair<int,PII> PIII;
typedef long long ll;
typedef double dob;
const int N=1e4+10,M=5e4+10;
int n,m;
int h[N],e[M],ne[M],idx;
int dfn[N],low[N],timestamp;
int stk[N],top;
int in_stk[N];
int id[N],scc_cnt,Size[N];
int dout[N];
void add(int a,int b)
{
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void tarjan(int u)
{
dfn[u]=low[u]=++timestamp;
stk[++top]=u,in_stk[u]=1;
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
if(!dfn[j])
{
tarjan(j);
low[u]=min(low[u],low[j]);
}
else if(in_stk[j])
low[u]=min(low[u],dfn[j]);
}
if(dfn[u]==low[u])
{
int y;
++scc_cnt;
do
{
y=stk[top--];
in_stk[y]=0;
id[y]=scc_cnt;
Size[scc_cnt]++;
} while (y!=u);
}
}
signed main()
{
quick_cin();
cin>>n>>m;
memset(h,-1,h);
while(m--)
{
int a,b;
cin>>a>>b;
add(a,b);
}
rep2(i,1,n)
if(!dfn[i])
tarjan(i);
rep2(i,1,n)
{
for(int j=h[i];~j;j=ne[j])
{
int k=e[j];
int a=id[i],b=id[k];
if(a!=b)dout[a]++;
}
}
int zero=0,sum=0;
rep2(i,1,scc_cnt)
{
if(!dout[i])
{
zero++;
sum+=Size[i];
if(zero>1)
{
sum=0;
break;
}
}
}
cout<<sum;
return 0;
}