2-4 每日总结

题目链接:朋友

题解:运用并查集将小明和小红认识的组成;两个集合,最多能配成的情侣就是两个集合少的集合的人数。

AC代码:

#include <iostream>
#include <cstring>
#include <cmath>
#include <iomanip> 
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>
using namespace std;

using ll = long long;
#define up(h,n) for(int i=h;i<=n;i++)
#define down(h,n) for(int i=h;i>=n;i--)
#define wh(x) while(x--)
#define node struct node
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
constexpr int N = 20005;
constexpr int mod = 1e9 + 7;
typedef int SElemType;

int n, m, p, q;
int a[N], b[N];
int find1(int x) {
	return a[x] == x ? x : a[x] = find1(a[x]);
}
int find2(int x) {
	return b[x] == x ? x : b[x] = find2(b[x]);
}
int main() {
	cin >> n >> m >> p >> q;
	up(1, n) {
		a[i] = i;
	}
	up(1, m) {
		b[i] = i;
	}
	up(1, p) {
		int x, y;
		cin >> x >> y;
		a[find1(x)] = find1(y);
	}
	up(1, q) {
		int x, y;
		cin >> x >> y;
		b[find2(-x)] = find2(-y);
	}
	int num1 = 0, num2 = 0;
	up(1, n) {
		if (find1(i) == find1(1)) num1++;
	}
	up(1, m) {
		if (find2(i) == find2(1)) num2++;
	}
	cout << min(num1, num2);
	return 0;
}

题目链接:选数

题解:这就是个简单的dfs,不过有个要注意的就是会出现重读的排列

AC代码:

#include <iostream>
#include <cstring>
#include <cmath>
#include <iomanip> 
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>

using namespace std;

using ll = long long;
using ull = unsigned long long;
#define up(i, h, n) for (int  i = h; i <= n; i++) 
#define down(i, h, n) for(int  i = h; i >= n; i--)
#define wh(x) while(x--)
#define node struct node
#define it ::iterator
#define Ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
constexpr int N = 205;
constexpr int mod = 1e9 + 7;

int a[25], n, k,sum=0;
int book[25];//用来标记数是否已经被使用
int isprime(int  x) { // 判断素数
	up(i,2, x / 2) {
		if (x % i == 0) return 0;
	}
	return 1;
}
void dfs(int x, int m,int s) { //x位置 m为和 s为个数

	if (s >= k) {
		if (isprime(m)) sum++;
		return;
	}
	up(i,x+1, n) { // 从x+1开始防止重复排列 列如 123 312
		if (book[i] == 0) {
			book[i] = 1;
			dfs(i, m + a[i],s+1);
			book[i] = 0;
		}
	}
	return;
}

int main() {

	cin >> n >> k;
	up(i,1, n) {
		cin >> a[i];
	}
	dfs(0, 0, 0);
	cout << sum;
	return 0;
}

题目链接:奇怪的电梯

题解:这道题运用结构体队列写,根据当前的最少步数尝试更新下一个能到达的楼层的最少步数,一步一步地更新,下一个楼层的最少步数一定是这一层的最少步数 +1。每一层第一次在队列里出现时的步数就是它的最少步数。

AC代码:

#include <iostream>
#include <cstring>
#include <cmath>
#include <iomanip> 
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>

using namespace std;

using ll = long long;
using ull = unsigned long long;
#define up(i, h, n) for (int  i = h; i <= n; i++) 
#define down(i, h, n) for(int  i = h; i >= n; i--)
#define wh(x) while(x--)
#define node struct node
#define it ::iterator
#define Ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
constexpr int N = 205;
constexpr int mod = 1e9 + 7;

node{
	int x; // 楼层
    int step;// 步数
};
typedef struct  top {
	int n;// 楼层
	int start;// 起始楼层
	int end; // 终点楼层
	int nums[N]; // 每层楼的动作
	bool book[N]; // 对楼层的标记
}tops;
tops m;
int bfs(tops m) {
	node cur = { m.start,0 }; // 当前楼层
	node next; // 下一楼层
	m.book[cur.x] = true; 
	queue<node>q;
	q.push(cur); // 起点入列
	while (false==q.empty()) {
		cur = q.front();  
		q.pop();
		if (cur.x == m.end) {
			return cur.step;
		}
         // 下楼
		next.x = cur.x - m.nums[cur.x];
		next.step = cur.step + 1;
		if (next.x > 0 && false == m.book[next.x]) {
			q.push(next);
			m.book[next.x] = true;
		}
		//上楼
		next.x = cur.x + m.nums[cur.x];
		next.step = cur.step + 1;
		if (next.x <= m.n && false == m.book[next.x]) {
			q.push(next);
			m.book[next.x] = true;
		}
	}
	return -1;
}
int main() {
	cin >> m.n >> m.start >> m.end ;
	up(i, 1, m.n) {
		cin >> m.nums[i];
	}
	cout << bfs(m) << '\n';
	return 0;
}

题目链接:

题解:这道题的话还是比较容易但是还是有一点坑,首先你要是按着他这个每一步操作就会超时,在转大小写只需要执行最后一个转的即可,还有就是转换之后后面可能还会有改变的字符,所以每一次改变要用数组给存起来,记住最后转换大小写的位置,在这个位置上加1对后面的进行改变操作即可;

AC代码:

#include <iostream>
#include <cstring>
#include <cmath>
#include <iomanip> 
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>

using namespace std;

using ll = long long;
using ull = unsigned long long;
#define up(i, h, n) for (int  i = h; i <= n; i++) 
#define down(i, h, n) for(int  i = h; i >= n; i--)
#define wh(x) while(x--)
#define node struct node
#define it ::iterator
#define Ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
constexpr int N = 500005;
constexpr int mod = 1e9 + 7;

string s;
int n, q;
int t[N], x[N];
char c[N];
int main() {
	Ios;
	cin >> n;
	cin >> s;
	cin >> q;
	int sum = 0, flag = 0;
	int i = 0;
	int p = q;
	wh(q) {
		cin >> t[i] >> x[i] >> c[i];
		if (t[i] == 1) {
			s[x[i] - 1] = c[i];
		}
		else if (t[i] == 2) {
			sum = i;
			flag = 1;
		}
		else if(t[i]==3) {
			sum = i;
			flag = 2;
		}
		i++;
	}
	if (flag == 1) {
		transform(s.begin(), s.end(), s.begin(), ::tolower);
	}
	else if(flag==2){
		transform(s.begin(), s.end(), s.begin(), ::toupper);
	}
	up(i, sum + 1, p-1) {
		s[x[i] - 1] = c[i];
	}
	cout << s;
	return 0;
}

题目链接:修复公路

题解:这道题的话也是并查集的思路,不过首先按时间排个序,然后对村庄进行进行修路合并,并进行村庄的集合减1,直到只剩下一个村庄就表示所有村庄都连通。

AC代码:

#include <iostream>
#include <cstring>
#include <cmath>
#include <iomanip> 
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>
using namespace std;

using ll = long long;
#define up(h,n) for(int i=h;i<=n;i++)
#define down(h,n) for(int i=h;i>=n;i--)
#define wh(x) while(x--)
#define node struct node
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
constexpr int N = 100005;
constexpr int mod = 1e9 + 7;
typedef int SElemType;

node{
	int x;
	int y;
	int t;// x,y表示两村庄,t代表时间
}s[N];
int a[N];
int n, m;
int find(int x) {
	return a[x] == x ? x : a[x] = find(a[x]); 
}
int cmp(node a, node b) {
	return a.t < b.t;

}
int main() {
	ios;
	cin >> n >> m;
	up(1, n) {
		a[i] = i;
	}
	up(1, m) {
		cin >> s[i].x >> s[i].y >> s[i].t;
	}
	sort(s + 1, s + 1 + m, cmp);// 按时间排序
	up(1, m) {
		if (find(s[i].x) != find(s[i].y)) { //祖先不同,合并祖先
			a[find(s[i].x)] = find(s[i].y);
			n--;  // 两家族合并 家族减一
			if (n == 1) {  // 如果只剩下一个家族,即表示所以村庄都连通
				cout << s[i].t << '\n';
				return 0;
			}
		}
	}
	cout << -1 << '\n';
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值