训练记录6:数据结构

优先队列

poj3614

题意:一群奶牛晒太阳,每只奶牛有一个太阳强度的限制minSPA和maxSPA,但太阳太猛烈,奶牛需要涂防晒霜才能晒太阳。防晒霜可以把太阳强度固定在一个值,如果比minSPA小还是会烧伤,如果比maxSPA大奶牛就没啥感觉。每只奶牛最多涂一瓶防晒霜,给出防晒霜的SPA值和数量,问有多少奶牛可以享受阳光

思路:主要是贪心,可以分别枚举奶牛和防晒霜。如果枚举奶牛的话不方便维护防晒霜的数量,因此选择枚举防晒霜,假设防晒霜以SPA从小到大排序,那么在符合条件(minspa≤SPA)中的奶牛中应该选哪只奶牛呢,答案是maxSPA最小的那一只,因为防晒霜SPA值越来越大,maxSPA大的奶牛可以留给更大的防晒霜涂。

维护maxSPA就可以用优先队列(小根堆)就可以了,如果堆顶满足条件(maxSPA≥SPA),就可以对这种防晒霜num--了,如果不满足直接丢弃即可,因为如果当前防晒霜都不满足那后面更大的防晒霜就更不满足了

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<iomanip>
using namespace std;
typedef long long ll;
const long long ll_inf=0x3f3f3f3f3f3f3f3f;
const int inf=0x3f3f3f3f;
const double pi=acos(-1.0);
const int Max=3000+5;
const int Mod=1e9;
#define _for(i,n) for(int i=0;i<n;++i)
#define _rep(i,n) for(int i=1;i<=n;++i)
//cout<<setw(8)<<fixed<<setprecision(3);
//ios::sync_with_stdio(false);
//priority_queue<int,vector<int>,greater<int> >
struct node{
	int ma,mi;
	bool operator < (const node &r)const{
		return mi<r.mi;
	}
}cow[Max];
struct node2{
	int spa,num;
	bool operator < (const node2 &r)const{
		return spa<r.spa;
	}
}sp[Max];
int c,l;
int main(){
	ios::sync_with_stdio(false);
	cin>>c>>l;
	_for(i,c) cin>>cow[i].mi>>cow[i].ma;
	_for(i,l) cin>>sp[i].spa>>sp[i].num;
	sort(sp,sp+l);
	sort(cow,cow+c);
	int ans=0,j=0;
	priority_queue<int,vector<int>,greater<int> > q;
	_for(i,l){//对每种防晒霜枚举 
		while(j<c&&cow[j].mi<=sp[i].spa){
			q.push(cow[j].ma);
			j++; 	
		}
		while(!q.empty()&&sp[i].num){
			int x=q.top();
            q.pop();
            if(x<sp[i].spa) continue;
            ans++;
            sp[i].num--;
		}
	}
	cout<<ans<<'\n';
	return 0;
}

while(j<c&&cow[j].mi<=sp[i].spa) 这个入队列的条件注意不能加上cow[j].ma>=sp[i].spa,虽然看似很合理:如果最大值小于spa就不用入队,但是会导致循环直接结束,遗漏掉排在这只奶牛后面的奶牛


poj2010

题意:一共有C头奶牛,要选N头奶牛,每头奶牛有一个价值和花费,要求选N头奶牛的总花费小于F的同时这N头奶牛的价值的中位数最大,输出这个中位数

思路:假设选第i头奶牛,则会在1到i之间,i到c之间分别选N/2头奶牛,而选那些奶牛对中位数没有影响。因此选择那些花费最小的奶牛即可,花费最小可以考虑采用优先队列,但是如果枚举每头奶牛的时候才进行计算会T,因此考虑先预处理好1到i,i到c头奶牛中选N/2头奶牛的最小值,然后再用O(n)的时间查询即可

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<iomanip>
using namespace std;
typedef long long ll;
const long long ll_inf=0x3f3f3f3f3f3f3f3f;
const int inf=0x3f3f3f3f;
const double pi=acos(-1.0);
const int Max=100000+5;
const int Mod=1e9;
//cout<<setw(8)<<fixed<<setprecision(3);
//ios::sync_with_stdio(false);
//priority_queue<int,vector<int>,greater<int> >
#define _for(i,n) for(int i=0;i<n;++i)
#define _rep(i,n) for(int i=1;i<=n;++i)
struct node{
	int val,cos;
	bool operator < (const node &r)const{
		return val>r.val;
	}
}cow[Max];
int dp1[Max];
int dp2[Max];
int N,C,F; 
int main(){
	ios::sync_with_stdio(false);
	cin>>N>>C>>F;
	_for(i,C){
		cin>>cow[i].val>>cow[i].cos; 
	}
	if(N==1){
		if(cow[0].cos<=F)
		cout<<cow[0].val<<'\n';
		else
		cout<<"-1";
		return 0;
	}
	sort(cow,cow+C);
	int low=N/2;
	priority_queue<ll> q;
	for(int i=0;i<low;++i){
		q.push(cow[i].cos);
		dp1[low]+=cow[i].cos;
	}
	for(int i=low+1;i<C;i++){
		if(cow[i-1].cos>=q.top())
		dp1[i]=dp1[i-1];
		else
		dp1[i]=dp1[i-1]+cow[i-1].cos-q.top();
		q.pop();
		q.push(cow[i-1].cos);
	}
	while(!q.empty())
	q.pop();
	for(int i=C-1;i>C-low-1;i--){
		q.push(cow[i].cos);
		dp2[C-low-1]+=cow[i].cos;		
	}
	for(int i=C-low-2;i>=0;i--){
		if(cow[i+1].cos>=q.top())
		dp2[i]=dp2[i+1];
		else{
			dp2[i]=dp2[i+1]+cow[i+1].cos-q.top();
			q.pop();
			q.push(cow[i+1].cos);		
		}
	}
	int i,flag=0;
	for(i=N/2;i<C-N/2;i++){
		int ans=cow[i].cos;
		ans+=dp1[i];
		ans+=dp2[i];
		if(ans<=F){
			flag=1;
			break;
		}
	}
	if(flag)
	cout<<cow[i].val<<'\n';
	else
	cout<<"-1";
	return 0;
}

如果预处理的部分太暴力也会T(比如我),这里学习了其他人的方法,采用大根堆,对于前N/2的奶牛,全部加起来存到dp[N/2]中(我喜欢从0开始),从N/2+1头牛开始,如果这头牛的花费比此时的优先队列队首大(队首保存的是前N/2中最大的),则dp[]直接等于前一个dp[](你比队首大肯定就不选你),反之dp[i] =dp[i-1] +cow[i-1].cos-q.top() 意思就是用小的代替堆顶并更新堆顶。另外dp[i]是不包含第i头牛的,后面查询还要记得加上i牛的花费。


poj2236

题意:给出一些坐标和一个距离d,给出一些字符串,要么是修复一个坐标,要么是查询两个已修复的坐标是否能连起来,连起来的意思是要么两个坐标距离小于d,要么可以通过一个或多个已修复的中间坐标连起来.

思路:并查集裸题。

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<iomanip>
using namespace std;
typedef long long ll;
const long long ll_inf=0x3f3f3f3f3f3f3f3f;
const int inf=0x3f3f3f3f;
const double pi=acos(-1.0);
const int Max=1000+5;
const int Mod=1e9;
//cout<<setw(8)<<fixed<<setprecision(3);
//ios::sync_with_stdio(false);
//priority_queue<int,vector<int>,greater<int> >
#define _for(i,n) for(int i=0;i<n;++i)
#define _rep(i,n) for(int i=1;i<=n;++i)
int par[Max];
int Rank[Max];
void init(int n){
	for(int i=0;i<n;i++){
		par[i]=i;
		Rank[i]=0;
	}
}
int find(int x){
	if(par[x]==x)
	return x;
	else
	return par[x]=find(par[x]);//已经进行了压缩 
}
void unite(int x,int y){
	x=find(x);
	y=find(y);
	if(x==y)
	return ;
	if(Rank[x]<Rank[y]){
		par[x]=y;
	}else{
		par[y]=x;
		if(Rank[x]==Rank[y])
		Rank[x]++;	
	}
}
bool same(int x,int y){
	return find(x)==find(y);
}
struct node{
	int x,y;
}p[Max];
int n,d;
bool Can(int a,int b){
	int dis=(p[a].x-p[b].x)*(p[a].x-p[b].x)+(p[a].y-p[b].y)*(p[a].y-p[b].y);
	return dis<=d*d;
}
int main(){
	cin>>n>>d;
	int flag[Max]={0};
	int f=0;
	init(n);
	_for(i,n){
		cin>>p[i].x>>p[i].y;
	}
	char a;	
	int b,c;
	while(cin>>a){
		if(a=='O'){//修复 
			cin>>b;
			b--;	
			_for(i,f){
				if(Can(flag[i],b)){
					unite(flag[i],b);
					
				}			
			}
			flag[f++]=b;
		}else{
			cin>>b>>c;
			b--;
			c--;
			if(same(b,c))
			cout<<"SUCCESS\n";
			else
			cout<<"FAIL\n";
		}
	}
}

这题1w毫秒,不关同步流8000,关了3000,改scanf 1000


poj1703

题意:有两个帮派,给出一些语句,要么问两个人是不是一个帮派的,要么给出两个人不是同一个帮派的。

思路:简直和食物链(poj1182)是一个磨子刻出来的题,其实是它的简化版。那么思路就很清晰了,开2*n的大小,前n个代表帮派A,后n个代表帮派B。询问的时候就查找a,b或者a+n,b+n,给信息的时候就合并a,b+n和b,a+n。同时合并是为了简化操作,即合并所有可能,免得还去找那个是已经属于哪个帮派的。

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<iomanip>
using namespace std;
typedef long long ll;
const long long ll_inf=0x3f3f3f3f3f3f3f3f;
const int inf=0x3f3f3f3f;
const double pi=acos(-1.0);
const int Max=1e5+5;
const int Mod=1e9;
//cout<<setw(8)<<fixed<<setprecision(3);
//ios::sync_with_stdio(false);
//priority_queue<int,vector<int>,greater<int> >
#define _for(i,n) for(int i=0;i<n;++i)
#define _rep(i,n) for(int i=1;i<=n;++i)
int par[Max*2];
int Rank[Max*2];
void init(int n){
	for(int i=0;i<n;i++){
		par[i]=i;
		Rank[i]=0;
	}
}
int find(int x){
	if(par[x]==x)
	return x;
	else
	return par[x]=find(par[x]);//已经进行了压缩 
}
void unite(int x,int y){
	x=find(x);
	y=find(y);
	if(x==y)
	return ;
	if(Rank[x]<Rank[y]){
		par[x]=y;
	}else{
		par[y]=x;
		if(Rank[x]==Rank[y])
		Rank[x]++;	
	}
}
bool same(int x,int y){
	return find(x)==find(y);
}
int main(){
	int t;
	scanf("%d",&t);
	//cin>>t;
	while(t--){
		int n,m;
		scanf("%d %d",&n,&m);
		//cin>>n>>m;
		init(2*n);
		_for(i,m){
			char a;
			int x,y;
			getchar();
			scanf("%c %d %d", &a, &x, &y);
			//cin>>a;
			if(a=='A'){
				if(same(x,y)||same(x+n,y+n)){
					cout<<"In the same gang.\n";
				}else if(same(x+n,y)||same(x,y+n)){
					cout<<"In different gangs.\n";
				}else{
					cout<<"Not sure yet.\n";
				}
			}else{
				unite(x,y+n);
				unite(x+n,y);
			}
		}
	}
}

另外注意并查集用到的两个数组要开2倍,不然等着吃re去吧


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值