2022CUST程序设计天梯赛校赛(正式赛)题解代码参考

官方题解地址:https://oj.acm.cust.team/discussion-detail/22

题目链接:CUSTOJ题目地址

以下为个人代码

L1-1:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
	cout<<"CUST fighting!"<<endl;
	return 0;
}

L1-2:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
map<int,int>mp;
struct person{
	string name;
	double s;
	double t;
	double dep;
	double v;
	double r;
	int p;
};
bool comp1(person v1,person v2){
	if(v1.s==v2.s){
		return v1.p<v2.p;
	}
	return v1.s>v2.s;
}
bool comp2(person v1,person v2){
	if(v1.dep==v2.dep){
		return v1.p<v2.p;
	}
	return v1.dep<v2.dep;
}
bool comp3(person v1,person v2){
	if(v1.v==v2.v){
		return v1.p<v2.p;
	}
	return v1.v>v2.v;
}
bool comp4(person v1,person v2){
	if(v1.r==v2.r){
		return v1.p<v2.p;
	}
	return v1.r>v2.r;
}
bool comp5(person v1,person v2){
	if(v1.t==v2.t){
		return v1.p<v2.p;
	}
	return v1.t<v2.t;
}
person a[1000];
int main(){
	int t;
	cin>>t;
	for(int i=1;i<=t;i++){
		int n;
		cin>>n;
		cout<<"Alice"<<endl;
	}
	
	return 0;
}

L1-3:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
map<int,int>mp;
struct line{
	int l,r;
};
line a[1000010];
bool comp(line v1,line v2){
	return v1.l<v2.l;
}
int main(){
	int n,l;
	cin>>n>>l;
	for(int i=1;i<=n;i++){
		cin>>a[i].l>>a[i].r;
	}
	sort(a+1,a+n+1,comp);
	int ans=a[1].r-a[1].l+1;
	int r=a[1].r;
	for(int i=2;i<=n;i++){
		if(a[i].r>r){
			if(a[i].l<r){
				ans+=a[i].r-r;
			}
			else{
				ans+=a[i].r-a[i].l+1;
			}
			r=a[i].r;
		}
	}
	cout<<ans<<endl;
	return 0;
}

L1-4:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
string key="small bottle";
int main(){
	int n;
	cin>>n;
	int f;
	string s;
	ll sum=0;
	getline(cin,s);
	for(int i=1;i<=n;i++){
		f=0;
		getline(cin,s);
		for(int j=0;j<s.length()-key.length();j++){
			string ss="";
			for(int k=j;k<j+key.length();k++){
				ss+=s[k];
			}
			if(ss==key){
				f=1;
				break;
			}
		}
		if(f){
			continue;
		}
		else{
			ll res=0;
			int r=s.length()-1;
			while(r>=0&&s[r]!=':'){
				r--;
			}
			for(int j=r+1;j<s.length();j++){
				res=res*10+(s[j]-'0');
			}
			sum+=res;
		}
	}
	cout<<sum<<endl;
	return 0;
}

L1-5:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
map<int,int>mp;
int getp(int k){
	for(int i=2;i*i<=k;i++){
		if(k%i==0){
			if(mp.count(i)){
				return 0;
			}
			mp[i]=1;
		}
		while(k%i==0){
			k/=i;
		}
	}
	if(k>1){
		if(mp.count(k)){
			return 0;
		}
		mp[k]=1;
	}
	return 1;
}
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		int d;
		cin>>d;
		if(!getp(d)){
			if(i%2){
				cout<<"B"<<endl;
				cout<<i<<endl;
			}
			else{
				cout<<"A"<<endl;
				cout<<i<<endl;
			}
			return 0;
		}
	}
	cout<<"cai ji hu zhuo!"<<endl;
	return 0;
}

L1-6:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[500][500],b[50][50];
int main(){
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			cin>>a[i][j];
		}
	}
	for(int i=1;i<=m;i++){
		for(int j=1;j<=m;j++){
			cin>>b[i][j];
		}
	}
	cout<<n-m+1<<endl;	
	for(int i=1;i<=n-m+1;i++){
		for(int j=1;j<=n-m+1;j++){
			ll sum=0;
			for(int x=i;x<i+m;x++){
				for(int y=j;y<j+m;y++){
					sum+=a[x][y]*b[x-i+1][y-j+1];
				}
			}
			cout<<sum;
			if(j!=n-m+1){
				cout<<" ";
			}
		}
		if(i!=n-m+1){
			cout<<endl;	
		}
	}
	return 0;
}

L1-7:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

struct elemt{
	int id;
	string name;
	double ac;
};
bool comp(elemt v1,elemt v2){
	if(v1.ac==v2.ac){
		return v1.id>v2.id;
	}
	return v1.ac>v2.ac;
}
elemt a[100010];
int main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n,m,x;
cin>>n>>m>>x;
int k1,k2,k3;
for(int i=1;i<=m;i++){
	string s;
	cin>>s;
	if(s=="id"){
		k1=i;
	}
	if(s=="name"){
		k2=i;
	}
	if(s=="accuracy"){
		k3=i;
	}
}
for(int i=1;i<=n;i++){
	for(int j=1;j<=m;j++){
		if(j==k1){
			cin>>a[i].id;
		}
		else if(j==k2){
			cin>>a[i].name;
		}
		else if(j==k3){
			cin>>a[i].ac;
		}
		else{
			int s;
			cin>>s;
		}
	}
}
sort(a+1,a+n+1,comp);
for(int i=1;i<=x;i++){
	cout<<a[i].id<<" "<<a[i].name<<" "<<setiosflags(ios::fixed)<<setprecision(2)<<a[i].ac<<endl;
}
	return 0;
}

L1-8:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
map<int,int>mp;
struct person{
	string name;
	double s;
	double t;
	double dep;
	double v;
	double r;
	int p;
};
bool comp1(person v1,person v2){
	if(v1.s==v2.s){
		return v1.p<v2.p;
	}
	return v1.s>v2.s;
}
bool comp2(person v1,person v2){
	if(v1.dep==v2.dep){
		return v1.p<v2.p;
	}
	return v1.dep<v2.dep;
}
bool comp3(person v1,person v2){
	if(v1.v==v2.v){
		return v1.p<v2.p;
	}
	return v1.v>v2.v;
}
bool comp4(person v1,person v2){
	if(v1.r==v2.r){
		return v1.p<v2.p;
	}
	return v1.r>v2.r;
}
bool comp5(person v1,person v2){
	if(v1.t==v2.t){
		return v1.p<v2.p;
	}
	return v1.t<v2.t;
}
person a[1000];
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		a[i].p=i;
		cin>>a[i].name;
		cin>>a[i].s;
		string ss;
		cin>>ss;
		double tmp=0;
		a[i].t=0;
		for(int j=0;j<ss.length();j++){
			if(ss[j]==':'){
				a[i].t=a[i].t*60.0+tmp;
				tmp=0;
			}	
			else{
				tmp=tmp*10.0+(ss[j]-'0');
			}
		}
		a[i].t=a[i].t*60+tmp;
		cin>>a[i].dep>>a[i].v>>a[i].r;
		a[i].t=a[i].t/a[i].r;
	}
	sort(a+1,a+n+1,comp1);
	cout<<a[1].name<<endl;
	sort(a+1,a+n+1,comp2);
	cout<<a[1].name<<endl;
	sort(a+1,a+n+1,comp3);
	cout<<a[1].name<<endl;
	sort(a+1,a+n+1,comp4);
	cout<<a[1].name<<endl;
	sort(a+1,a+n+1,comp5);
	cout<<a[1].name<<endl;
	return 0;
}

L2-1:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[10010][20];
int x[10010],c[10010];
vector<int>b;
int main(){
	int n,m;
	cin>>m>>n;
	for(int i=1;i<=n;i++){
		cin>>x[i]>>c[i];
		for(int j=1;j<=x[i];j++){
			cin>>a[i][j];
		}
		sort(a[i]+1,a[i]+x[i]+1);
	}
	int p;
	cin>>p;
	for(int i=1;i<=p;i++){
		int d;
		cin>>d;
		b.push_back(d);
	}
	sort(b.begin(),b.end());
	int minn=1e9;
	for(int i=1;i<=n;i++){
		if(c[i]>=minn){
			continue;
		}
		int l=0,r=1;
		while(r<=x[i]&&l<b.size()){
			if(a[i][r]==b[l]){
				l++;
			}
			r++;
		}
		if(l>=b.size()){
			minn=c[i];
		}
	}
	if(minn==1e9){
		cout<<-1<<endl;
	}
	else{
		cout<<minn<<endl;
	}
	return 0;
}

L2-2:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int dp[100][3100];
int pre[100][3100];
int u[100],w[100];
int main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++){
	cin>>u[i]>>w[i];
}
int ans=0;
for(int i=1;i<=n;i++){
	for(int j=u[i];j<=m;j++){
		for(int k=0;k<=i;k++){
			if(u[i]<u[k]){
				continue;
			}
			dp[i][j]=max(dp[i][j],dp[k][j-u[i]]+w[i]);
			ans=max(ans,dp[i][j]);
		}
	}
}
cout<<ans<<endl;
	return 0;
}

L2-3:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,m;
int ans[600][600];
int c[600][600];
struct elemt{
	int x,y,d;
};
bool check(int x,int y){
	if(x>n|y>m||x<1||y<1||c[x][y]||ans[x][y]!=-1){
		return 0;
	}
	return 1;
}
int tx[4]={0,1,0,-1};
int ty[4]={1,0,-1,0};
void bfs(int sx,int sy){
	ans[sx][sy]=0;
	queue<elemt>q;
	q.push({sx,sy,0});
	while(q.size()){
		elemt f=q.front();
		q.pop();
		int x=f.x,y=f.y;
		for(int i=0;i<4;i++){
			int fx=f.x+tx[i],fy=f.y+ty[i];
			if(check(fx,fy)){
				ans[fx][fy]=f.d+1;
				q.push({fx,fy,f.d+1});
			}
		}
			int fx=f.x-2,fy=f.y+1;
			if(check(fx,fy)&&!c[x-1][y]){
				ans[fx][fy]=f.d+1;
				q.push({fx,fy,f.d+1});
			}
			fx=f.x-2,fy=f.y-1;
			if(check(fx,fy)&&!c[x-1][y]){
				ans[fx][fy]=f.d+1;
				q.push({fx,fy,f.d+1});
			}
			fx=f.x+2,fy=f.y-1;
			if(check(fx,fy)&&!c[x+1][y]){
				ans[fx][fy]=f.d+1;
				q.push({fx,fy,f.d+1});
			}
			fx=f.x+2,fy=f.y+1;
			if(check(fx,fy)&&!c[x+1][y]){
				ans[fx][fy]=f.d+1;
				q.push({fx,fy,f.d+1});
			}
			fx=f.x+1,fy=f.y+2;
			if(check(fx,fy)&&!c[x][y+1]){
				ans[fx][fy]=f.d+1;
				q.push({fx,fy,f.d+1});
			}
			fx=f.x+1,fy=f.y-2;
			if(check(fx,fy)&&!c[x][y-1]){
				ans[fx][fy]=f.d+1;
				q.push({fx,fy,f.d+1});
			}
			fx=f.x-1,fy=f.y+2;
			if(check(fx,fy)&&!c[x][y+1]){
				ans[fx][fy]=f.d+1;
				q.push({fx,fy,f.d+1});
			}
			fx=f.x-1,fy=f.y-2;
			if(check(fx,fy)&&!c[x][y-1]){
				ans[fx][fy]=f.d+1;
				q.push({fx,fy,f.d+1});
			}
	}
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
memset(ans,-1,sizeof(ans));
cin>>n>>m;
int sx,sy;
cin>>sx>>sy;
int t;
cin>>t;
for(int i=1;i<=t;i++){
	int x,y;
	cin>>x>>y;
	c[x][y]=1;
}
bfs(sx,sy);
for(int i=1;i<=n;i++){
	for(int j=1;j<=m;j++){
		cout<<ans[i][j];
		if(j!=m){
			cout<<" ";
		}
	}
	cout<<endl;
}
	return 0;
}

L2-4:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[100010];
int num[100];
ll check(ll sum){
	for(ll i=0;i<=60;i++){
		if(sum&(1ll<<i)){
			if(!num[i]){
				return 0;
			}
		}
	}
	return 1;
}
void pushin(ll k){
	for(ll i=0;i<=60;i++){
		if(k&(1ll<<i)){
			num[i]++;
		}
	}
}
void del(ll k){
	for(ll i=0;i<=60;i++){
		if(k&(1ll<<i)){
			num[i]--;
		}
	}
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int n;
	cin>>n;
	ll sum=0;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		sum|=a[i];
	} 
	int ans=1e9;
	int l=1,r=2;
	pushin(a[l]);
	while(r<=n){
		while(!check(sum)&&r<=n){
			pushin(a[r]);
			r++;
		}
		if(r>n&&!check(sum)){
			break;
		}
		while(l<=n&&check(sum)){
			del(a[l]);
			l++;
		}
		l--;
		pushin(a[l]);
		ans=min(ans,r-l);
		del(a[l]);
		l++;
	}
	ans=max(ans,1);
	cout<<ans<<endl;
	return 0;
}

L3待补

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值