跳到正文

算法·理论

7.5来做梦比赛晚上河西教室1 -- 队列进阶总结

sbl_xh约 13 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年8月7日
篇幅
约 13 分钟
获赞
1

版本

  1. v1当前

比赛传送门:

来 做 梦 信 息 学 比 赛

笔记:

h t t p s : / / i m g s e . c o m / i / p V K I X n S

图片显示不了

A 迷宫出口

题目传送门
评测成绩:AC/100pts\color{green}{AC/100pts}
代码:

C++
#include<bits/stdc++.h>
using namespace std;
int n,bx,by,ex,ey;
int a[300][300];
int dx[4]={0,0,-1,1};
int dy[4]={1,-1,0,0};
bool vis[300][300],ans;
void dfs(int x,int y){
    if(vis[x][y]==1) return ;
    if(x<1||x>n||y<1||y>n) return ;
    if(a[x][y]==1) return ;
    if(x==ex&&y==ey) {
        ans=1;
        return ;
    }
    vis[x][y]=1;
    for(int i = 0;i<4;i++) dfs(x+dx[i],y+dy[i]);
}
int main(){
    cin >> n;
    for(int i = 1;i<=n;i++){
        for(int j = 1;j<=n;j++){
            cin >> a[i][j];
        }
    }
    cin >> bx >> by >> ex >> ey;
    dfs(bx,by);
    if(ans) cout << "YES";
    else cout << "NO";
    return 0;
}

B 周末舞会

题目传送门
评测成绩:AC/100pts\color{green}{AC/100pts}
代码:

C++
#include<bits/stdc++.h>
using namespace std;
int n,m,k,p[3000],q[3000],t1,t2,h1,h2,a=1,b=1;
int main(){
    cin >> n >> m >> k;
    t1=n;
    t2=m;
    h1=h2=1;
    for(int i = 1;i<=n;i++) p[i]=i;
    for(int i = 1;i<=m;i++) q[i]=i;
    for(int i = 1;i<=k;i++){
        if(a==n+1) a=1;
        if(b==m+1) b=1;
        cout << p[a++] << ' ' << q[b++] << endl;
    }
    return 0;
}

C 围圈报数

题目传送门
评测成绩:AC/100pts\color{green}{AC/100pts}
代码:

C++
#include<bits/stdc++.h>
using namespace std;
int n,m,h,t;
int a[200000];
int main(){
    cin >> n >> m;
    for(int i = 1;i<=n;i++) {
        a[t++] = i;
    }
    for(int i = 1;i<=n;i++){
        for(int j = 1;j<m;j++){
            a[t++]=a[h++];
        }
        cout << a[h] << ' ';
        h++;
    }
    return 0;
}

D [NOIP 2010 提高组] 机器翻译

题目传送门
评测成绩:未交/0tps\color{gray}{未交/0tps}
正解:

C++
#include<bits/stdc++.h>
using namespace std;
int n,m,x,ans,l,r,a[1005],b[1005];
int main(){
    cin>>m>>n;
    l=0;r=0;
    for (int i=1;i<=n;i++){
        scanf("%d",&x);
        if (a[x]==0) {
            ans++;
            r++;b[r]=x;a[x]=1;
            if (r>m) {l++;a[b[l]]=0;}
        }
    }
    cout<<ans;
    return 0;
}

E [NOIP 2004 提高组] 合并果子

题目传送门
评测成绩:AC/100pts\color{green}{AC/100pts}
代码:

C++
#include<bits/stdc++.h>
using namespace std;
int n;
long long ans,x;
priority_queue<long long,vector<long long>,greater<long long> > q;
int main(){
    cin >> n;
    for(int i = 1;i<=n;i++){
        cin >> x;
        q.push(x);
    }
    while(q.size() > 1){
        long long a;
        ans+=q.top();
        a=q.top();
        q.pop();
        ans+=q.top();
        a+=q.top();
        q.pop();
        q.push(a);
    }
    cout << ans;
    return 0;
}

F 【深基4.习5】求极差 / 最大跨度值 / 最大值和最小值的差

题目传送门
评测成绩:AC/100pts\color{green}{AC/100pts}
代码:

C++
#include<bits/stdc++.h>
using namespace std;
priority_queue<int> q1;
priority_queue<int,vector<int>,greater<int> > q2;
int n;
int x;
int main(){
    cin >> n;
    for(int i = 1;i<=n;i++) {
        cin >> x;
        q1.push(x);
        q2.push(x);
    }
    cout << q1.top()-q2.top();
    return 0;
}

G [USACO09OPEN] Cow Line S

题目传送门
评测成绩:AC/100pts\color{green}{AC/100pts}
代码:

C++
#include<bits/stdc++.h>
using namespace std;
deque <int> q;
int n;
int main(){
    cin >> n;
    int p=1;
    for(int i = 1;i<=n;i++){
        char a,b;
        cin >> a >> b;
        if(a=='D') {
            int k;
            cin >> k;
            if(b=='L'){
                for(int j = 1;j<=k;j++) q.pop_front();
            }else {
                for(int j = 1;j<=k;j++) q.pop_back();
            }
        }else{
            if(b=='L') q.push_front(p);
            else q.push_back(p);
            p++;
            
        }
        
    }
    while(q.size()){
        cout << q.front() << endl;
        q.pop_front();
    }
    return 0;
}

作业:

B3616:

代码:

C++
#include<bits/stdc++.h>
using namespace std;
queue<int>q;
int main(){
    int n;
    cin >> n;
    for(int i = 1;i<=n;i++){
        int ob,x;
        cin >> ob;
        if(ob==1){
            cin >> x;
            q.push(x);
        }else if(ob==2){
            if(q.size()==0) cout << "ERR_CANNOT_POP" << endl;
            else q.pop();
        }else if(ob==3){
            if(q.size()==0) cout << "ERR_CANNOT_QUERY" << endl;
            else cout << q.front() << endl;
        }else cout << q.size() << endl;
    }
    return 0;
}

P10457

代码:

C++
#include<bits/stdc++.h>
#define ll long long
#define u unsigned
#define ull u ll
using namespace std;
char x;
deque<int> a[25];
int b[25];
long long ans;
int main(){
	for(int i = 1;i<=13;i++){
		for(int j = 1;j<=4;j++){
			cin >> x;
			if(x=='0') a[i].push_back(10);
			else if(x=='A') a[i].push_back(1);
			else if(x=='J') a[i].push_back(11);
			else if(x=='Q') a[i].push_back(12);
			else if(x=='K') a[i].push_back(13);
			else a[i].push_back(x-'0');
		}
	}
	while(a[13].size()){
		int y =a[13].front(),n=y;
		a[13].pop_front();
		while(y!=13){
			b[y]++;
			y=a[y].back();
			a[n].pop_back();
			n=y;
		}
	}
	for(int i=  1;i<=12;i++) {if(b[i]==4)ans++;}
	cout << ans;
	return 0;
}

P3887

代码:

C++
#include<bits/stdc++.h>
using namespace std;
priority_queue<int,vector<int>,less<int> >k;
priority_queue<int,vector<int>,less<int> >d;
priority_queue<int,vector<int>,less<int> >m;
priority_queue<int,vector<int>,less<int> >f;
int q,a,b,c,e;
int main(){
	int i,tmp;
	cin  >> a >> b >> c >> e;
	for(i = 1;i<=a;i++)cin >> tmp,f.push(tmp);
	for(i = 1;i<=b;i++)cin >> tmp,m.push(tmp);
	for(i = 1;i<=c;i++)cin >> tmp,d.push(tmp);
	for(i = 1;i<=e;i++)cin >> tmp,k.push(tmp);
    int n,x,y,z;
    cin >> n;
    for(i = 1;i<=n;i++){
    	int ans = 0;
    	cin >> x >> y >> z;
    	while(z--){
    		int tmp =k.top();k.pop();
    		ans+=tmp;
		}
		while(y--){
    		int tmp = d.top();d.pop();
    		ans+=tmp;
		}
		while(x--){
    		int tmp = m.top();m.pop();
    		ans+=tmp;
    	
		}
		ans+=f.top();f.pop();
		printf("%.2lf\n",ans*1.0/11);
	}
	return 0;
}
原文