本文主要是介绍2024年码蹄杯本科院校赛道初赛(省赛),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
赛时所写题,简单写一下思路,qwq
第一题:
输出严格次小值,
//#pragma GCC optimize(2)#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include<stack>
#include<cmath>
#include <unordered_set>
#include <unordered_map>
#include<set>
#include <map>
#include<ctime>
#include<bitset>using namespace std;typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int>PII;#define fi first
#define se second
#define ls u<<1
#define rs u<<1|1
#define all(ss) ss.begin(),ss.end()
#define pb push_backint const mod=1e9+7;
int const B=507;
//int const mod=998244353;
//int const base=131,mod=2e9+11;
int const N=2e5+7,M=2e6+7;
int const INF=0x3f3f3f3f;
LL const INFF=0x3f3f3f3f3f3f3f3f;int n,m,q,k;
int x,y,z;
int a[N];
string s,t;
vector<int>g[N];
struct Node{int w,x,y;bool operator<(const Node &o)const{return w<o.w;}
};void solve(){cin>>n;for(int i=0;i<n;i++) cin>>a[i];sort(a,a+n);int i=1;while(i<n&&a[i]==a[0]) i++;cout<<a[i];
} void init(){}int main()
{//std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);//init();int T=1;//cin>>T;//scanf("%d",&T);for(int i=1;i<=T;i++){solve();}return 0;
}
第二题:
思路:一段全是1可以直接累加答案,然后记录一个左右两端连续1个数的最大值
//#pragma GCC optimize(2)#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include<stack>
#include<cmath>
#include <unordered_set>
#include <unordered_map>
#include<set>
#include <map>
#include<ctime>
#include<bitset>using namespace std;typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int>PII;#define fi first
#define se second
#define ls u<<1
#define rs u<<1|1
#define all(ss) ss.begin(),ss.end()
#define pb push_backint const mod=1e9+7;
int const B=507;
//int const mod=998244353;
//int const base=131,mod=2e9+11;
int const N=2e5+7,M=2e6+7;
int const INF=0x3f3f3f3f;
LL const INFF=0x3f3f3f3f3f3f3f3f;int n,m,q,k;
int x,y,z;
int a[N];
string s,t;
vector<int>g[N];void solve(){cin>>n;int ans=0;int mx1=0,mx2=0;for(int i=0;i<n;i++){cin>>s; m=s.size();int cnt1=0,cnt2=0;for(int i=0;i<m&&s[i]=='1';i++){cnt1++;}for(int i=m-1;i>=0&&s[i]=='1';i--){cnt2++;}if(cnt1==m) ans+=m;else{mx1=max(mx1,cnt1);mx2=max(mx2,cnt2);}} cout<<ans+max(mx1,mx2)<<endl;
} void init(){}int main()
{//std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);//init();int T=1;//cin>>T;//scanf("%d",&T);for(int i=1;i<=T;i++){solve();}return 0;
}
第三题:
思路:每一步只能往下走,或者往右走,(x1,y2)在(x2,y2)左上角时无解,两点的距离大于n时也是无解的,总距离为n,可以选k1个向左(或者选k2个向右),C(n,k1)/C(n,k2),记得除法用逆元
//#pragma GCC optimize(2)#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include<stack>
#include<cmath>
#include <unordered_set>
#include <unordered_map>
#include<set>
#include <map>
#include<ctime>
#include<bitset>using namespace std;typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int>PII;#define fi first
#define se second
#define ls u<<1
#define rs u<<1|1
#define all(ss) ss.begin(),ss.end()
#define pb push_back//int const mod=1e9+7;
int const B=507;
int const mod=998244353;
//int const base=131,mod=2e9+11;
int const N=3e5+7,M=2e6+7;
int const INF=0x3f3f3f3f;
LL const INFF=0x3f3f3f3f3f3f3f3f;int fact[N],infact[N];
LL inv100;LL qpow(LL a,LL b,int p=mod){LL res=1;a%=p;while(b){if(b&1) res=res*a%p;a=a*a%p;b/=2;}return res;
}
LL C(int a,int b){if(b>a) swap(a,b);return 1LL*fact[a]*infact[a-b]%mod*infact[b]%mod;
}
LL lucas(LL a,LL b){if(a<mod&&b<mod) return C(a,b);return 1LL*lucas(a/mod,b/mod)*lucas(a%mod,b%mod);
}void solve(){int x1,x2,y1,y2,n,p,q;scanf("%d%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&n,&p,&q);LL k1=x2-x1,k2=y2-y1;if(x1>x2||y1>y2||k1+k2!=n) printf("0\n");else{LL t=C(n,k1); LL ans=t%mod*qpow(p*inv100,k1)%mod*qpow(q*inv100,k2)%mod;printf("%lld\n",ans);}
} void init(){inv100=qpow(100,mod-2);fact[0]=infact[0]=1;for(int i=1;i<N;i++){fact[i]=1LL*fact[i-1]*i%mod;infact[i]=1LL*infact[i-1]*qpow(i,mod-2)%mod;}
// for(int i=1;i<=10;i++){
// for(int j=0;j<=i;j++){
// cout<<C(i,j)<<" ";
// }
// cout<<endl;
// }
}int main()
{//std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);init();int T=1;//cin>>T;scanf("%d",&T);for(int i=1;i<=T;i++){solve();}return 0;
}
第四题:
思路:缩点后,跑拓扑排序,记录缩点后每一个团的最小值
//#pragma GCC optimize(2)#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include<stack>
#include<cmath>
#include <unordered_set>
#include <unordered_map>
#include<set>
#include <map>
#include<ctime>
#include<bitset>using namespace std;typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int>PII;#define fi first
#define se second
#define ls u<<1
#define rs u<<1|1
#define all(ss) ss.begin(),ss.end()
#define pb push_backint const mod=1e9+7;
int const B=507;
//int const mod=998244353;
//int const base=131,mod=2e9+11;
int const N=2e5+7,M=2e6+7;
int const INF=0x3f3f3f3f;
LL const INFF=0x3f3f3f3f3f3f3f3f;int n,m,q,k;
int x,y,z;
int a[N];
string s,t;
vector<int>g[N];
int dfn[N],low[N],tot;
int stk[N],top;
bool instk[N];
int scc[N],sz[N],cnt;
int mi[N];
int inD[N];void tarjan(int x){low[x]=dfn[x]=++tot;stk[++top]=x; instk[x]=true;for(int y:g[x]){if(dfn[y]==0){tarjan(y);low[x]=min(low[x],low[y]);}else if(instk[y]){low[x]=min(low[x],dfn[y]);}}if(dfn[x]==low[x]){int y; cnt++;do{y=stk[top--]; instk[y]=false;scc[y]=cnt;sz[cnt]++;mi[cnt]=min(mi[cnt],a[y]);}while(y!=x);}
}
vector<PII>edge;void solve(){scanf("%d%d",&n,&m);for(int i=1;i<=n;i++) scanf("%d",a+i);for(int i=0;i<m;i++){scanf("%d%d",&x,&y);edge.pb({x,y});g[x].pb(y);}memset(mi,0x3f,sizeof mi);for(int i=1;i<=n;i++)if(dfn[i]==0) tarjan(i);for(int i=1;i<=n;i++) g[i].clear();for(auto [x,y]:edge){if(scc[x]!=scc[y]){g[scc[x]].pb(scc[y]);inD[scc[y]]++;}}queue<int>q;for(int i=1;i<=cnt;i++){if(inD[i]==0){q.push(i);}}while(q.size()){int x=q.front(); q.pop();for(int y:g[x]){mi[y]=min(mi[y],mi[x]);if(--inD[y]==0) q.push(y);}}LL ans=0;for(int i=1;i<=n;i++) ans+=mi[scc[i]];cout<<ans<<"\n";for(int i=1;i<=n;i++) cout<<mi[scc[i]]<<" ";cout<<endl;
} void init(){}int main()
{//std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);//init();int T=1;//cin>>T;//scanf("%d",&T);for(int i=1;i<=T;i++){solve();}return 0;
}
第六题:
思路:离线处理,逆向思维,拆边改成建边,用并查集维护每一个小组的内出现的个数,合并时采用启发式合并
//#pragma GCC optimize(2)#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include<stack>
#include<cmath>
#include <unordered_set>
#include <unordered_map>
#include<set>
#include <map>
#include<ctime>
#include<bitset>using namespace std;typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int>PII;#define fi first
#define se second
#define ls u<<1
#define rs u<<1|1
#define all(ss) ss.begin(),ss.end()
#define pb push_backint const mod=1e9+7;
int const B=507;
//int const mod=998244353;
//int const base=131,mod=2e9+11;
int const N=4e5+7,M=4e5+7;
int const INF=0x3f3f3f3f;
LL const INFF=0x3f3f3f3f3f3f3f3f;int n,m,q,k;
int x,y,z;
int a[N];
bool vis[N]; //标记删除的边
struct Node{int opt,x,y;
}Q[M];
int fa[N];
PII edges[N];
int ans[M];int find(int x){return fa[x]==x?fa[x]:fa[x]=find(fa[x]);
}void solve(){scanf("%d%d%d",&n,&m,&q);for(int i=1;i<=n;i++) scanf("%d",a+i);for(int i=1;i<=m;i++) scanf("%d%d",&edges[i].fi,&edges[i].se);for(int i=1;i<=q;i++){int opt,x,y=0; scanf("%d%d",&opt,&x);if(opt==1) vis[x]=true;else{scanf("%d",&y);}Q[i]={opt,x,y};}vector<map<int,int>>mp(n+1);for(int i=1;i<=n;i++) fa[i]=i;for(int i=1;i<=n;i++) mp[i][a[i]]++;for(int i=1;i<=m;i++){if(vis[i]) continue;int x=edges[i].fi,y=edges[i].se;int px=find(x),py=find(y);if(px!=py){if(mp[px].size()<mp[py].size()) swap(mp[px],mp[py]);fa[py]=px;for(auto [val,c]:mp[py]){mp[px][val]+=c;}}}for(int i=q;i>=1;i--){if(Q[i].opt==1){int x=edges[Q[i].x].fi,y=edges[Q[i].x].se;int px=find(x),py=find(y);if(px!=py){if(mp[px].size()<mp[py].size()) swap(mp[px],mp[py]);fa[py]=px;for(auto [val,c]:mp[py]){mp[px][val]+=c;}}}else{int x=Q[i].x,y=Q[i].y;int px=find(x);ans[i]=mp[px][y-a[x]]-(a[x]+a[x]==y);}}for(int i=1;i<=q;i++) if(Q[i].opt==2) printf("%d\n",ans[i]);
} void init(){}int main()
{//std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);//init();int T=1;//cin>>T;//scanf("%d",&T);for(int i=1;i<=T;i++){solve();}return 0;
}
这篇关于2024年码蹄杯本科院校赛道初赛(省赛)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!