牛客周赛R168

D 小月的储物格

并查集

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;

#define int long long
const int N=2e5+5;
const int INF=0x3f3f3f3f3f3f3f3f;
int n,m;
int a[N],tree[N];

int get(int z){
    if(tree[z]!=z) tree[z]=get(tree[z]);
    return tree[z];
}

int nxt(int pos){
    if(pos==n) return 1;
    return pos+1;
}

void solve(){
    cin>>n>>m;
    int ans=0;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        tree[i]=i;
    }
    int cnt=1;
    while(m--){
        int tp;
        cin>>tp;
        cnt+=tp;
        while(cnt>n)cnt=cnt-n;
        cnt=get(cnt);
        ans+=a[cnt];
        tree[cnt]=get(nxt(cnt));
    }
    cout<<ans<<' '<<cnt;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t=1;
    while(t--){
        solve();
    }
    return 0;
}

set

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;

#define int long long
const int N=2e5+5;
const int INF=0x3f3f3f3f3f3f3f3f;
int n,m;
int a[N];

void solve(){
    cin>>n>>m;
    int ans=0;set<int>s;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        s.insert(i);
    }
    int cnt=1;
    while(m--){
        int tp;
        cin>>tp;
        cnt+=tp;
        while(cnt>n)cnt-=n;
        auto it=s.lower_bound(cnt);
        if(it==s.end())it=s.begin();
        cnt=*it;
        ans+=a[cnt];
        s.erase(cnt);
    }
    cout<<ans<<' '<<cnt;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t=1;
    while(t--){
        solve();
    }
    return 0;
}

E 小月的栈(Easy)

二维状压DP

G 小月的栈(Hard)

贪心可行性判断

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;

#define int long long
const int N=200005;
const int INF=0x3f3f3f3f3f3f3f3f;
int n,m,k;
int a[N],b[N],c[N],d[N];
int suma[N],sumb[N],pa[N],pb[N];


void solve(){
    int ans=-INF;
    cin>>n>>m>>k;
    c[0]=0;suma[0]=0;pa[0]=0;
    for(int i=1;i<=n;i++){
        cin>>a[i]>>c[i],suma[i]=suma[i-1]+a[i];
        pa[i]=pa[i-1]+c[i-1];
    }
    d[0]=0;sumb[0]=0;pb[0]=0;
    for(int i=1;i<=m;i++){
        cin>>b[i]>>d[i],sumb[i]=sumb[i-1]+b[i];
        pb[i]=pb[i-1]+d[i-1];
    }
    int lo=max(0LL,k-m),hi=min(k,n);
    for(int i=lo;i<=hi;i++){
        if(pa[i]<=k-i&&i>=pb[k-i])ans=max(ans,suma[i]+sumb[k-i]);
    }
    if(ans==-INF)cout<<"IMPOSSIBLE"<<endl;
    else cout<<ans<<endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;cin>>t;
    while(t--) solve();
    return 0;
}
相关推荐
青少儿编程课堂12 小时前
第十八届蓝桥杯青少组图形化真题:校园分形雪花绘制解析(全国青少年信息素养大赛·电子学会图形化考级冲刺)
c++·python·算法·bfs·信息学竞赛
geovindu14 小时前
python: KNeighborsClassifier K
开发语言·后端·python·算法
让我们一起加油好吗15 小时前
【数据结构】线段树详解
数据结构·c++·算法·线段树·洛谷
凌冰_16 小时前
数据结构 - 队列(五)
数据结构·算法·软件工程
让我们一起加油好吗17 小时前
【数据结构】ST 表
数据结构·c++·算法·动态规划·洛谷·st表
让我们一起加油好吗18 小时前
【算法刷题】LeetCode 力扣 Hot 100 刷题记录
数据结构·c++·算法·leetcode·面试·笔试
lisw051 天前
稀疏算法概述!
算法·机器人·无人机
YLCHUP1 天前
【括号匹配+dp优化】题解:双端括号(brackets)_动态规划_状态优化_数学建模_贪心_性质挖掘_STLmap_C++算法竞赛
数据结构·c++·笔记·算法·数学建模·贪心算法·动态规划
郝学胜-神的一滴1 天前
Effective STL 条款09:慎重选择删除元素的方法
开发语言·网络·数据结构·c++·算法·stl