2.23训练赛

本文介绍了多个编程题目,涉及路径搜索(A-GridIceFloor),功能判断(B-StrictlySuperior),字符串反转计数(C-Reversible),最长连续序列查找(D-VacationTogether),时间签到模拟(E-When?),字符串旋转标记(F-Rotation),以及优化问题(G-ManyOranges和K-Alcoholic)。
摘要由CSDN通过智能技术生成

A - Grid Ice Floor

bfs,遍历四个方向,出发前检测一次,沿着一个方向走,每走一步又检测一次,若下一步会撞墙,则结束行走。

细节在于撞墙时点的入队和最后数量的统计。

#include<bits/stdc++.h>
 
#define int long long
 
int n, m, ans, vis[207][207];
char mp[207][207];
 
const int dx[5] = {0, 1, 0, -1, 0};
 
bool check(int x, int y) {
	if (mp[x][y] != '#') return true;
	else return false;
}
 
void bfs() {
	queue <std::pair <int, int>> q;
	q.push({2, 2});
	vis[2][2] = 1; // 这个别漏了
 
	while (!q.empty()) {
		auto top = q.front(); q.pop();
 
		int tx = top.first, ty = top.second;
		
		for (int i = 0; i <= 3; ++ i) {
			int x = tx, y = ty;
			if (check(x + dx[i], y + dx[i + 1])) {
				while (true) {
					int nx = x + dx[i], ny = y + dx[i + 1];
					if(check(nx, ny)) {
						x = nx, y = ny;
						vis[x][y] ++;
					} else {
						if (vis[x][y] <= 1) q.push({x, y});
						break;
					}
				}
			}
		}
	}
}
 
int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; ++ i) {
		for (int j = 1; j <= m; ++ j) {
			cin >> mp[i][j];
		}
	}
 
	bfs();
 
	for (int i = 1; i <= n; ++ i) {
		for (int j = 1; j <= m; ++ j) {
			if (vis[i][j] >= 1) ans ++;
		}
	}
 
	cout << ans;
}

B - Strictly Superior

判断是否有商品满足下列条件

1.Pi等于Pj

2.j商品拥有i商品所有功能

3.Pi价格大于Pj,或者j商品拥有i商品所没有的功能

满足其一即可输出Yes

#include<bits/stdc++.h>
using namespace std;
bool b[105][105];
int a[105];
//void solve(int n,string s){
	
//}

int main() {
    ios::sync_with_stdio(false); 
	cin.tie(0); cout.tie(0);
    int n,m,k,w=0;
    cin>>n>>m;
    for(int i=0;i<n;i++){
    	cin>>a[i]>>k;
    	for(int j=0;j<k;j++){
    		int sh;
    		cin>>sh;
    		b[i][sh]=1;
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			w=0;
			if(i==j){
				continue;
			}
			if(a[i]>a[j]){
				continue;
			}
			if(a[i]<a[j]){
				for(int l=0;l<m;l++){
					if(b[j][l]==1&&b[i][l]==0){
						w=1;
					}
				}
				if(!w){
					cout<<"Yes";
					return 0;
				}
			}
			if(a[i]==a[j]){
				for(int l=0;l<m;l++){
					if(b[j][l]==1&&b[i][l]==0){
						w=1;
						break;
					}
					if(b[i][l]==1&&b[j][l]==0){
						w=2;
					}
				}
				if(w==2){
					cout<<"Yes";
					return 0;
				}
			}
		}
	}
	cout<<"No";
}

C - Reversible

判断有多少个不同的字符串,反转的也算同一个

思路:用一个map存字符串是否出现过,若没出现过则sum++,同时存其及其反转后的字符串到map

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

//void solve(int n,string s){
	
//}

int main() {
    ios::sync_with_stdio(false); 
	cin.tie(0); cout.tie(0);
    int n,sum=0;
    string s;
    unordered_map<string,bool>v;
    cin>>n;
    while(n--){
    	cin>>s;
    	if(!v[s]){
    		v[s]=1;
    		reverse(s.begin(),s.end());
    		v[s]=1;
    		sum++;
		}
	}
	cout<<sum;
}

E - Vacation Together

寻找最长的连续列的o

思路:sum表示当前最长连续列o,num表示上一个状态最长连续列o,每次连续停止时max两者最大即可,同时令sum=0

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

bool solve(int j,int n,string a[]){
	for(int i=0;i<n;i++){
		if(a[i][j]=='x'){
			return false;
		}
	}
	return true;
}

int main() {
    ios::sync_with_stdio(false); 
	cin.tie(0); cout.tie(0);
    int n,m,sum=0,num=0;
    cin>>n>>m;
    //solve();
    string a[105];
    for(int i=0;i<n;i++){
    	cin>>a[i];
	}
	for(int i=0;i<m;i++){
		if(solve(i,n,a)){
			sum++;
		}
		else{
			num=max(sum,num);
			sum=0;
		}
	}
	num=max(sum,num);
	cout<<num;
}

H - When?

签到题,打表

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

//void solve(){
	
//}

int main() {
    ios::sync_with_stdio(false); 
	cin.tie(0); cout.tie(0);
    int n;
    cin>>n;
   	if(n==0){
   		cout<<"21:00";
	}
	else if(n<10){
		cout<<"21:0"<<n;
	}
	else if(n<60){
		cout<<"21:"<<n;
	}
	else if(n>=60&&n<70){
		cout<<"22:0"<<n-60;
	}
	else if(n>=70){
		cout<<"22:"<<n-60;
	}
}

I - Rotation

标记每次1操作之后的字符串的开头

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

//void solve(int n,string s){
	
//}

int main() {
    ios::sync_with_stdio(false); 
	cin.tie(0); cout.tie(0);
    int n,m,k=0,num;
    string s;
    cin>>n>>m;
   	cin>>s;
   	while(m--){
   		int q,w;
	char ch;
	cin>>q>>w;
	if(q==2){
		num=(k+w-1+n)%n;
		cout<<s[num]<<endl;
	}
	else if(q==1&&w%n==0){
		
	}
	else {
		k=k-w+n;
		k%=n;
	}
	}
}

K - Many Oranges

可以看出,最大值由最小重量的橘子取得,因为余数总可以均摊到取的橘子上,向下取整。最小值由最大重量的橘子取得,因为余数总可以均摊到其他橘子上,向上取整

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

//void solve(int n,string s){
	
//}

int main() {
    ios::sync_with_stdio(false); 
	cin.tie(0); cout.tie(0);
    int n,m,max,min;
    double k;
    cin>>n>>m>>k;
    k*=1000;
    max=floor(k/n);
    min=ceil(k/m);
    if(min>max){
    	cout<<"UNSATISFIABLE"<<endl;
	}
	else{
		cout<<min<<' '<<max<<endl;
	}
}

L - Alcoholic

判断酒精含量之和是否大于X,是则输出第几次喝酒

思路:考虑到精度问题,让X*100即可

#include<bits/stdc++.h>
using namespace std;
long long n,m,q,sum,x=1,w;
int solve(){
	cin>>q>>w;
	sum+=q*w;
	if(sum>m){
		cout<<x<<endl;
		return 1;
	}
	x++;
	return 0;
}

int main() {
    ios::sync_with_stdio(false); 
	cin.tie(0); cout.tie(0);
    cin>>n>>m;
    m*=100;
   	while(n--){
   		if(solve()){
   			return 0;
		}
	}
	cout<<"-1";
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值