# 训练记录6：数据结构

#### 优先队列

poj3614

#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

#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;
}

poj2236

#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";
}
}
}

poj1703

#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);
}
}
}
}

• 0
点赞
• 0
收藏
觉得还不错? 一键收藏
• 打赏
• 0
评论
02-25 332
12-10 436
08-08 428
04-28 4255
09-01 600
08-21 2406
02-25 3759
09-28 386
10-02 151

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

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

¥1 ¥2 ¥4 ¥6 ¥10 ¥20

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