3.赢球票
题目链接:https://www.lanqiao.cn/problems/94/learning/?page=1&first_category_id=1&second_category_id=3
题意:
给定长度为n的卡片排列,顺时针从1开始数,若卡片上的数字与数的数字相同则将此卡片收入囊中,下一个卡片从头数
求最大的卡片数字和(可以从任意一张卡片开始)
set
求set最大值maxn=(*st.rbegin()),注意循环需要集合非空且数的数字要小于集合最大值!
#include<bits/stdc++.h> #define rep(i,a,n) for(int i=a;i<=n;i++) #define pb push_back #define endl "\n" #pragma GCC optimize(3) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int inf=0x3f3f3f3f; const ll llmax=LLONG_MAX; const int maxn=1e5+5; const int mod=1e9+7; int n; int a[105]; set<int>st; bool vis[105]; signed main() { ios::sync_with_stdio(false),cin.tie(0); cin>>n; rep(i,1,n){ cin>>a[i]; } ll ans=0; rep(i,1,n){ rep(j,1,n)st.insert(a[j]); rep(j,1,n)vis[j]=false; ll res=0; int t=1; int pos=i; while(!st.empty()&&t<=(*st.rbegin())){ if(a[pos]==t&&!vis[pos]){ res+=a[pos]; st.erase(a[pos]); vis[pos]=true; t=1; } if(!vis[pos])t++; pos++; if(pos>n)pos=1; } ans=max(res,ans); } cout<<ans; return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现