河南萌新联赛2024第(二)场:南阳理工学院

目录

A-国际旅行Ⅰ_河南萌新联赛2024第(二)场:南阳理工学院 (nowcoder.com)

​思路:

代码:

D-A*BBBB_河南萌新联赛2024第(二)场:南阳理工学院 (nowcoder.com)

思路:

代码:

 F-水灵灵的小学弟_河南萌新联赛2024第(二)场:南阳理工学院 (nowcoder.com)

思路:

代码:

G-lxy的通风报信_河南萌新联赛2024第(二)场:南阳理工学院 (nowcoder.com)

思路:

代码:

H-狼狼的备忘录_河南萌新联赛2024第(二)场:南阳理工学院 (nowcoder.com)

思路:

代码:

I-重生之zbk要拿回属于他的一切_河南萌新联赛2024第(二)场:南阳理工学院 (nowcoder.com)

思路:

代码:

J-这是签到_河南萌新联赛2024第(二)场:南阳理工学院 (nowcoder.com)​

思路:

代码:


A-国际旅行Ⅰ_河南萌新联赛2024第(二)场:南阳理工学院 (nowcoder.com)

思路:

题目表示保证各个国家可以相互抵达,即从小到大排序,按所查序号输出。

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#define int long long
using namespace std;
const int N = 1e4;
int n,t,m,q;
int a[N];
string s;
bool cmp(int a, int b)
{
	return a < b;
}
void slove()
{
	cin >> n >> m >> q;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	sort(a + 1, a + 1 + n, cmp);
	for (int i = 1; i <= m; i++)
	{
		int x, y;
		cin >> x >> y;
	}
	while (q--)
	{
		int k;
		cin >> k;
		cout << a[k] << "\n";
	}
}
signed main()
{
	t=1;
	while (t--)
	{
		slove();
	}
	return 0;
}

D-A*BBBB_河南萌新联赛2024第(二)场:南阳理工学院 (nowcoder.com)

思路:

b的每一位都相同,即可以发现a的某一位乘b得到的每一位结果都相同,用字符串模拟乘法计算。

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#define int long long
using namespace std;
string multiply(string a, string b) //用a的每一位数乘b相同的数
{
    int t = 0, cur = 0;
    string ans;
    reverse(a.begin(), a.end());//从低位开始
    for (int i = 0; i < a.size() + b.size(); i++) 
    {
        if (i < a.size()) 
            cur += a[i] - '0';
        if (i >= b.size()) 
            cur -= a[i - b.size()] - '0';
        t += cur * (b[0] - '0');
        ans += (t % 10 + '0');
        t /= 10;//是否进位
    }
    while (ans.back() == '0' && ans.size() > 1) 
        ans.pop_back();
    reverse(ans.begin(), ans.end());
    return ans;
}
signed main() 
{
    int t;
    cin >> t;
    while (t--) 
    {
        string a, b;
        cin >> a >> b;
        cout << multiply(a, b) << endl;
    }
    return 0;
}

 F-水灵灵的小学弟_河南萌新联赛2024第(二)场:南阳理工学院 (nowcoder.com)

思路:

签到题,可以用博弈论做,但是其实两个人缩写都是DHY,直接输出即可

代码:

#include<iostream>
#include<cstring>
#define int long long
using namespace std;
int n,t;
string s;
void slove()
{
	int a, b;
	cin >> a >> b;
	cout << "DHY" << "\n";
}
signed main()
{
	cin >> t;
	while (t--)
	{
		slove();
	}
	return 0;
}

G-lxy的通风报信_河南萌新联赛2024第(二)场:南阳理工学院 (nowcoder.com)

 

思路:

先用bfs跑一遍,记录每个队伍之间的最短距离,再跑一遍最小生成树,即可得到答案

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#define int long long
using namespace std;
const int N = 1e3+10;
int n, m, k;
struct node {
	int x, y, w;
};
node g[N];
int f[N][N],dis[N][N],fa[N];
char a[N][N];
int vis[N][N];
node b[N];
int dx[4] = { 1, -1, 0, 0 };
int dy[4] = { 0, 0, 1, -1 };
bool cmp(node a,node b) 
{
	return a.w < b.w;
}
void bfs(int x) 
{
	memset(f, -1, sizeof(f));
	f[b[x].x][b[x].y] = 0;
	queue<node>q;
	q.push({ b[x].x, b[x].y });
	vis[b[x].x][b[x].y] = 1;
	while (!q.empty()) 
	{
		node temp = q.front(); q.pop();
		for (int i = 0; i < 4; i++) 
		{
			int tx = dx[i] + temp.x;
			int ty = dy[i] + temp.y;
			if (tx < 1 || tx > n || ty < 1 || ty > m || vis[tx][ty] || a[tx][ty] == '#')
				continue;
			vis[tx][ty] = true;
			f[tx][ty] = f[temp.x][temp.y] + 1;
			q.push({ tx, ty });
		}
	}
	for (int i = 1; i <= k; i++) 
	{
		if (f[b[i].x][b[i].y] == -1) 
		{
			cout << "No" << '\n';
			exit(0);
		}
		dis[x][i] = f[b[i].x][b[i].y];
	}
}
int find(int u)
{
	if (fa[u] == u)
		return u;
	return fa[u]=find(fa[u]);
}
signed main() 
{

	cin >> n >> m;
	for (int i = 1; i <= n; i++) 
	{
		for (int j = 1; j <= m; j++) 
		{
			cin >> a[i][j];
			if (a[i][j] == '*') 
			{
				b[++k].x = i;
				b[k].y = j;
			}
		}
	}
	for (int i = 1; i <= k; i++) 
	{
		memset(vis, 0, sizeof(vis));
		bfs(i);
	}
	for (int i = 1; i <= k; i++) 
		fa[i] = i;
	int cnt = 0;
	for (int i = 1; i <= k; i++) 
	{
		for (int j = 1; j <= k; j++) 
		{
			if (i == j)
				continue;
			cnt++;
			g[cnt].x = i; g[cnt].y = j;
			g[cnt].w = dis[i][j];
		}
	}
	sort(g + 1, g + cnt + 1, cmp);
	int ans = 0;
	for (int i = 1; i <= cnt; i++) 
	{
		int fx = find(g[i].x);
		int fy = find(g[i].y);
		if (fx == fy)continue;
		fa[fx] = fy;
		ans += g[i].w;
	}
	cout << ans << '\n';
	return 0;
}

H-狼狼的备忘录_河南萌新联赛2024第(二)场:南阳理工学院 (nowcoder.com)

 

思路:

直接用map加set,去除相同的再舍去后缀

代码:

#include <iostream>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#define int long long
using namespace std;
signed main() 
{
	int n;
	cin >> n;
	map<string, set<string>> mp;
	for (int i = 0; i < n; i++)
	{
		string s;
		cin >> s;
		int m;
		cin >> m;
		for (int i = 0; i < m; i++)
		{
			string a;
			cin >> a;
			mp[s].insert(a);
		}
	}
	for (auto& it : mp)
	{
		for (auto x : it.second)
		{
			string temp;
			for (int i = x.size() - 1; i > 0; i--)
			{
				temp = x.substr(i, x.size() - i);
				if (it.second.count(temp))//it.second.count()是用来查找x的子集是否存在
					it.second.erase(temp);//如果it.second里存在x的某个子集,则删除
			}
		}
	}
	cout << mp.size() << endl;
	for (auto it : mp)
	{
		cout << it.first << " ";
		cout << it.second.size() << " ";
		for (auto x : it.second)
			cout << x << " ";
		cout << "\n";
	}
	return 0;
}

I-重生之zbk要拿回属于他的一切_河南萌新联赛2024第(二)场:南阳理工学院 (nowcoder.com)

思路:

直接用string类型的find函数,找一个用erase删一个

代码:

#include<iostream>
#include<cstring>
#define int long long
using namespace std;
int n;
string s;
signed main()
{
	cin >> n >> s;
	int flag = 1, cnt = 0;
	while (flag != -1)
	{
		flag = s.find("chuan");
		if (flag != -1)
		{
			cnt++;
			s.erase(flag, flag + 4);
		}
	}
	cout << cnt;
	return 0;
}

J-这是签到_河南萌新联赛2024第(二)场:南阳理工学院 (nowcoder.com)

思路:

直接硬列出来了

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#define int long long
using namespace std;
const int N = 1e3;
int n,t,m,q;
int a[N][N];
string s;
void slove()
{
	int n, m;
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			cin >> a[i][j];
	if (m < n)
	{
		for (int j = m + 1; j <= n; j++)
			for (int i = 1; i <= n; i++)
				a[i][j] = 0;
	}
	if (n < m)
	{
		for (int i = n + 1; i <= m; i++)
			for (int j = 1; j <= m; j++)
				a[i][j] = 0;
	}
    int x = min(n, m);
    int mi = 1e9;
    int p = 0;
    if (x >= 1)
    {
        p = a[1][1];
        mi = min(mi, p);
    }
    if (x >= 2)
    {
        p = (a[1][1] * a[2][2] - a[2][1] * a[1][2]);
        mi = min(mi, p);
    }
    if (x >= 3)
    {
        p = ((a[1][1] * a[2][2] - a[2][1] * a[1][2]) * a[3][3]) + ((a[1][2] * a[2][3] - a[1][3] * a[2][2]) * a[3][1]) + ((a[1][3] * a[2][1] - a[1][1] * a[2][3]) * a[3][2]);
        mi = min(mi, p);
    }
    if (x >= 4)
    {
        p = (a[1][1] * a[2][2] * a[3][3] - a[1][3] * a[2][2] * a[3][1]) * a[4][4] + (a[1][2] * a[2][3] * a[3][4] - a[1][4] * a[2][3] * a[3][2]) * a[4][1] + (a[1][3] * a[2][4] * a[3][1] - a[1][1] * a[2][4] * a[3][3]) * a[4][2] + (a[1][4] * a[2][1] * a[3][2] - a[1][2] * a[2][1] * a[3][4]) * a[4][3];
        mi = min(mi, p);
    }
    if (x >= 5)
    {
        p = (a[1][1] * a[2][2] * a[3][3] * a[4][4] - a[1][4] * a[2][3] * a[3][2] * a[4][1]) * a[5][5] + (a[1][2] * a[2][3] * a[3][4] * a[4][5] - a[1][5] * a[2][4] * a[3][3] * a[4][2]) * a[5][1] + (a[1][3] * a[2][4] * a[3][5] * a[4][1] - a[1][1] * a[2][5] * a[3][4] * a[4][3]) * a[5][2] + (a[1][4] * a[2][5] * a[3][1] * a[4][2] - a[1][2] * a[2][1] * a[3][5] * a[4][4]) * a[5][3] + (a[1][5] * a[2][1] * a[3][2] * a[4][3] - a[1][3] * a[2][2] * a[3][1] * a[4][5]) * a[5][4];
        mi = min(mi, p);
    }
    int w = 0;
    if (m != n)
        mi = min(mi, w);
    cout << mi << "\n";
}
signed main()
{
	t=1;
	while (t--)
	{
		slove();
	}
	return 0;
}

 

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值