【BZOJ】【水题合集】【P1271】【P1800】【P2150】【P1015】【#6】

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1271

http://www.lydsy.com/JudgeOnline/problem.php?id=1800

http://www.lydsy.com/JudgeOnline/problem.php?id=2150

http://www.lydsy.com/JudgeOnline/problem.php?id=1015

P1271:

二分水题

#include <cstdio>
#include <climits>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long lld;
lld s[200001],e[200001],d[200001];
lld tot=0;
lld n,m,T;
lld l,r,mid;
bool ok(){
	lld cnt=0;
	for(lld i=1;i<=n;i++){
		lld t=min(mid,(lld)e[i]);
		if(t>=s[i])
		cnt+=(t-s[i])/d[i]+1;
	}
	return cnt&1;
}
int main(){
	cin>>T;
	while(T--){
		cin>>n;
		for(lld i=1;i<=n;i++)
		cin>>s[i]>>e[i]>>d[i];
		l=0;r=INT_MAX+1LL;
		while(l<r){
			mid=(l+r)/2;
			if(ok())
				r=mid;
			else
				l=mid+1;
		}
		if(l==INT_MAX+1LL){
			puts("Poor QIN Teng:(");
			continue;
		}
		lld ans=0;
		for(lld i=1;i<=n;i++){
			if(s[i]<=l&&l<=e[i]&&(l-s[i])%d[i]==0)
				ans++;
		}
		cout<<l<<" "<<ans<<endl;
	}	
	return 0;
}


P1800

还能更水么?

#include<cmath>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int n;
int chs=0;
int ans=0;
int way[21];
double len[21];
double sum[21];
double T;
const double eps=1e-6;

int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>len[i];
		sum[i]=sum[i-1]+len[i];
	}
	for(int i=1;i<=n;i++)
	for(int j=i+1;j<=n;j++)
	if((sum[j]-sum[i])*2==sum[n])ans++;
	
	cout<<ans*(ans-1)/2<<endl;
	return 0;
}


P2150

最小路径覆盖

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int n,m,r,c;
int dx[4],dy[4];
int w[2555][2555];
int mp[51][51];
int hash[51][51];
int tot=0;
int vis[2555];
int Link[2555];
bool find(int x){
    for(int i=1;i<=tot;i++){
        if(w[x][i]&&!vis[i]){
            vis[i]=1;
            if(!Link[i]||find(Link[i])){
                Link[i]=x;
                return true;
            }
        }
    }
    return false;
}
int main(){
    cin>>n>>m>>r>>c;
    dx[0]=r;dy[0]=c;
    dx[1]=r;dy[1]=-c;
    dx[2]=c;dy[2]=r;
    dx[3]=c;dy[3]=-r;
    string s;
    for(int i=1;i<=n;i++){
        cin>>s;
        for(int j=0;j<m;j++){
            if(s[j]=='.')
                mp[i][j+1]=0;
            else
                mp[i][j+1]=1;
        }
    }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            if(!mp[i][j])
                hash[i][j]=++tot;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++){
            for(int k=0;k<4;k++){
                int x=i+dx[k],y=j+dy[k];
                if(x<1||y<1||x>n||y>m)continue;
                w[hash[i][j]][hash[x][y]]=1;
            }
        }
    int ans=0;
    
    for(int i=1;i<=tot;i++){
        memset(vis,0,sizeof(vis));
        if(find(i))ans+=1;
    }
    cout<<tot-ans<<endl;
    return 0;
}


P1015

倒着并查集

#include<vector>
#include<queue>
#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#define X first
#define Y second
#define read(x) x=getint()
#define read3(x,y,z) x=getint();y=getint();z=getint()
using namespace std;
typedef pair<int,int> pii;
vector<vector<int> >G;
void add(int u,int v){
	G[u].push_back(v);
	G[v].push_back(u);
}
int getint(){
	int ok=0;
	int res=0;
	char ch;
	while(1){
		ch=getchar();
		if(ch>='0'&&ch<='9'){
			ok=1;
			res*=10;
			res+=ch-'0';
		}else
			if(ok)break;
	}
	return res;
}
int F;
int fa[400010];
int vis[400010];
int find(int x){
	if(fa[x]!=x)return fa[x]=find(fa[x]);
	return x;
}
int hash[400010]={0};
void dfs(int x){
	fa[x]=F;
	vis[x]=1;
	for(int i=0;i<G[x].size();i++){
		int e=G[x][i];
		if(!vis[e]&&!hash[e]){
			vis[e]=1;
			dfs(e);
		}
		//dfs(e);
	}
}
int n,m,qs;
vector<pii>M;
vector<int>E;
int ans[400010];
int hash2[400010];
int main(){
	read(n);read(m);
	for(int i=0;i<n;i++)fa[i]=i;
	G.resize(n+1);
	for(int i=0;i<m;i++){
		int u,v;
		read(u);read(v);
		add(u,v);
	}
	read(qs);int e;
	for(int i=0;i<qs;i++){
		read(e);
		E.push_back(e);	
		hash[e]=1;
	}
	int sum=n-qs;
	for(int i=0;i<n;i++){
		if(!hash[i]){
			for(int j=0;j<G[i].size();j++){
				e=G[i][j];
				if(!hash[e]){
					if(find(i)!=find(e)){
						fa[find(e)]=find(i);
						sum--;
					}
				}
			}
		}
	}
	ans[qs]=sum;
	for(int i=qs-1;i>=0;i--){
		sum++;
		int fi=E[i];
		for(int j=0;j<G[fi].size();j++){
			e=G[fi][j];
			if(!hash[e]){
				if(fi!=find(e)){
					fa[find(e)]=fi;
					sum--;
				}
			}
		}
		ans[i]=sum;
		hash[E[i]]=0;
	}
	for(int i=0;i<=qs;i++)
	printf("%d\n",ans[i]);
	return 0;
}


你可能感兴趣的:(bzoj)