洛谷 【数据结构1-1】线性表

P3156 【深基15.例1】询问学号 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
复制代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
int n,m;
map<int,int> mp;
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        int id;
        cin>>id;
        mp[i]=id;
    }
    for(int i=1;i<=m;i++){
        int quiry;
        cin>>quiry;
        cout<<mp[quiry]<<endl;
    }

    return 0;
}
复制代码

 

P3613 【深基15.例2】寄包柜 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

复制代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
int n,q;
map<ll,int> mp;
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    mp.clear();
    cin>>n>>q;
    while(q--){
        int op,i,j,k;
        cin>>op;
        if(op&1){
            cin>>i>>j>>k;
            mp[i*100000+j]=k;
        }else{
            cin>>i>>j;
            cout<<mp[i*100000+j]<<endl;
        }    
    }
    return 0;
}
复制代码

 

P1449 后缀表达式 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

复制代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    stack<int> s;
    char c;
    int left,res=0,right=0;
    cin>>c;
    while(c!='@'){
        left=0;
        if(c>='0' && c<='9'){
            while(c>='0' && c<='9'){
                res=c-'0';
                left=left*10+res;
                cin>>c;
            }
        }
        if(c=='.') s.push(left);
        else{
            right=s.top();
            s.pop();
            left=s.top();
            s.pop();
            if(c=='+') s.push(left+right);
            if(c=='-') s.push(left-right);
            if(c=='*') s.push(left*right);
            if(c=='/') s.push(left/right);
        }
        cin>>c;
    }
    cout<<s.top();
    
    
    return 0;
}
复制代码

 

P1996 约瑟夫问题 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

复制代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
int n,m;
vector<int> a;
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++) a.push_back(i);
    int pos=0;
    for(int i=1;i<=n;i++){
        pos=(pos+m-1)%a.size();
        cout<<a[pos]<<" ";
        a.erase(a.begin()+pos);
    }
    return 0;
}
复制代码

 

约瑟夫问题拓展:Problem - 4841 (hdu.edu.cn)

复制代码
#include<iostream>
#include<vector>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
int n,m;
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    vector<int> a;
    while(cin>>n>>m){
        a.clear();
        for(int i=0;i<2*n;i++) a.push_back(i);
        int pos=0;
        for(int i=0;i<n;i++){
            pos=(pos+m-1)%a.size();
            a.erase(a.begin()+pos);
        }
        int j=0;
        for(int i=0;i<2*n;i++){
            if(!(i%50) && i) cout<<endl;
            if(j<a.size()&&i==a[j]){
                ++j;
                cout<<"G";
            }else cout<<"B";
        }
        cout<<endl<<endl;
    }
    return 0;
}
复制代码

 

P1160 队列安排 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

复制代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int N=1e5+10;
int n,m;
struct node{
    int pre,next,id;
}a[N]={0};
void insert_(int i,int k,int p){
    //i插入k的哪一边 
    if(!p){ //
        a[i].next=k;
        a[a[k].pre].next=i;
        a[i].pre=a[k].pre;
        a[k].pre=i;
    }else{ //
        a[i].pre=k;
        a[a[k].next].pre=i;
        a[i].next=a[k].next;
        a[k].next=i;
    }
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>n;
    a[0].pre=1,a[0].next=1;
    a[1].pre=0;a[1].next=0;
    for(int i=2;i<=n;i++){
        int k,q;
        cin>>k>>q;
        insert_(i,k,q);
    }    
    cin>>m;
    while(m--){
        int x;
        cin>>x;
        a[x].id=1;
    }
    for(int i=a[0].next;i;i=a[i].next){
        if(a[i].id==0) cout<<i<<" ";
    }
    
    return 0;
}
复制代码

 

P1540 [NOIP2010 提高组] 机器翻译 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

复制代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
int M,N,ans=0;
bool vis[1010];
queue<int> q;
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>M>>N;
    for(int i=1;i<=N;i++){
        int x;
        cin>>x;
        if(vis[x]) continue;
        else{
            if(q.size()>=M){
                vis[q.front()]=false;
                q.pop();
            }
            q.push(x);
            vis[x]=true;
            ans++;
        }
    }
    cout<<ans<<endl;
    
    return 0;
}
复制代码

 

P2058 [NOIP2016 普及组] 海港 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

复制代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
int n,t,k,ans,x,c[100010];
struct node{
    int s,t;
};
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>n;
    queue<node> q;
    node now;
    for(int i=1;i<=n;i++){
        cin>>t>>k;
        while(!q.empty()){
            now=q.front();
            if(now.t+86400<=t){
                c[now.s]--;
                if(!c[now.s]) --ans;
                q.pop();
                continue;
            }
            break;
        }
        for(int i=1;i<=k;i++){
            cin>>x;
            now.s=x,now.t=t;
            c[x]++;
            q.push(now);
            if(c[x]==1) ++ans;
        }
        cout<<ans<<endl;
    }
    return 0;
}
复制代码

 

P1241 括号序列 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

复制代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
int top=0,w[110];
char s[110],c[110];
string a; 
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>a;
    int n=a.size();
    for(int i=0;i<n;i++){
        if(a[i]=='(' || a[i]=='['){
            s[++top]=a[i];
            w[top]=i;
            if(a[i]=='(') c[i]=')';
            else c[i]=']';
        }
        if(a[i]==')'){
            if(top && s[top]=='('){
                c[w[top]]=' ';
                top--;
            }else c[i]='(';
        }
        if(a[i]==']'){
            if(top && s[top]=='['){
                c[w[top]]=' ';
                top--;
            }else c[i]='[';
        }
    }
    for(int i=0;i<n;i++){
        if(c[i]=='(' || c[i]=='[') cout<<c[i]<<a[i];
        else if(c[i]==')' || c[i]==']') cout<<a[i]<<c[i];
        else cout<<a[i];
    }
    
    return 0;
}
复制代码

 

赠送:小苯的IDE括号问题(easy) (nowcoder.com) & 小苯的IDE括号问题(hard) (nowcoder.com)

复制代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int N=2e5+10;
int n,m,a[N],b[N];
char str[N];
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>n>>m;
    cin>>str+1;
    int top1=0,top2=0;
    for(int i=1;i<=n;i++){
        if(str[i]=='(') a[++top1]=0;
        if(str[i]==')') a[++top1]=1;
        if(str[i]=='I') break;
    }
    for(int i=n;i>=1;i--){
        if(str[i]=='(') b[++top2]=0;
        if(str[i]==')') b[++top2]=1;
        if(str[i]=='I') break;
    }
    while(m--){
        cin>>str+1;
        if(str[1]=='<'){
            if(top1) b[++top2]=a[top1--];
        }
        if(str[1]=='-'){
            if(top2) a[++top1]=b[top2--];
        }
        if(str[1]=='b'){
            if(top1){
                if(a[top1]==0 && top2 && b[top2]==1) top2--;
                top1--;
            }
        }
        if(str[1]=='d'){
            if(top2) top2--;
        }
    }
    for(int i=1;i<=top1;i++){
        if(a[i]==0) cout<<"(";
        else cout<<")";
    } 
    cout<<"I";
    for(int i=top2;i>=1;i--){
        if(b[i]==0)cout<<"(";
        else cout<<")";
    }
    
    return 0;
}
复制代码

 

P4387 【深基15.习9】验证栈序列 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

复制代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int N=100010;
int q,n,a[N],b[N];
stack<int> s;
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>q;
    while(q--){
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        int pos=1;
        cin>>n;
        for(int i=1;i<=n;i++) cin>>a[i];
        for(int j=1;j<=n;j++) cin>>b[j];
        for(int i=1;i<=n;i++){
            s.push(a[i]);
            while(s.top()==b[pos]){
                s.pop();
                pos++;
                if(s.empty()) break;
            }
        }
        if(s.empty()) cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
        while(!s.empty()) s.pop();
    }
    
    return 0;
}
复制代码

 

P2234 [HNOI2002] 营业额统计 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

复制代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
int n,x;
ll ans=0;
set<int> s;
set<int>::iterator l,r;
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    s.insert(INF);s.insert(-INF);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>x;
        if(s.size()==2){
            ans+=x;
            s.insert(x);
        }else{
            l=s.lower_bound(x);
            if(*l!=x){
                r=l;
                --r;
                ans+=min(abs(*l-x),abs(*r-x));    
                s.insert(x);
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}
复制代码

 

posted @   ACCbulb  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示