算法训练,项目

一.木材加工

题解:

二分答案,左边0,右边可以为最长的木头,但我直接赋值了一个很大的值,进行二分,随后写个check;内部遍历木头截取为mid木块的个数,要是>=k,满足要求,还可以继续往后面找,因为它是要求最大每段木头的长度,直至左边加一小于右边,最后输出左边;

代码:

复制代码
#include <iostream>
#include <cstring>
#include <cmath>
#include <iomanip> 
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>
#include<set>
#include <string>
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 MaxN = 200005;
constexpr int MaxM = 10005;
constexpr int mod = 1e9 + 7;
constexpr int inf = 0x7fffffff;

ll n, k;
ll a[MaxN];

bool check(ll x) {   //  判断是否能切割出长度为x的k段木头

	ll sum = 0;
	up(i, 1, n) {
		sum += a[i] / x;
	}
	return sum >= k;
}
int main() {

	Ios;
	cin >> n >> k;
	up(i, 1, n) {
		cin >> a[i];
	}
	ll l = 0, r = inf;
	while (l + 1 < r) { // l+1 为了避免死循环
		ll mid = (l + r) / 2;
		if (check(mid)) l = mid;
		else r = mid;
	}
	cout << l << endl;
	return 0;
}

二.并查集

题解:

这是并查集的模版,并查集就是利用一个数组来标记他们,看他们是否是一起;

代码:

复制代码
#include <iostream>
#include <cstring>
#include <cmath>
#include <iomanip> 
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>
#include<set>
#include <string>
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 MaxN = 10005;
constexpr int MaxM = 100005;
constexpr int mod = 1e9 + 7;
constexpr int inf = 0x7fffffff;



void slove() {

	
}

int f[MaxN];

int find(int x) { //  寻找;
	return f[x] == x ? x : f[x] = find(f[x]); 
}
int main() {

	Ios;
	/*int t;
	cin >> t;
	while (t--) {
		slove();
	}*/
	int n, m;
	cin >> n >> m;
	up(i, 1, n) f[i] = i;  //初始化
	up(i, 1, m) {
		int z, x, y;
		cin >> z >> x >> y;
		if (z == 1) {
			f[find(y)] = find(x);  // 合并
		}
		else if (find(x) == find(y)) {
			cout << "Y" << endl;
		}
		else {
			cout << "N" << endl;
		}
	}
	return 0;
}

三、单源最短路径(弱化版)

题解:

模版题,用Dijkstra,但是我还是有点不理解Dijkstra;

代码:

复制代码
#include <iostream>
#include <cstring>
#include <cmath>
#include <iomanip> 
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>
#include<set>
#include <string>
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 MaxN = 500005;
constexpr int MaxM = 10005;
constexpr int mod = 1e9 + 7;
constexpr int inf = 0x7fffffff;


node{
	int to,w,next;
}e[MaxN];
int head[MaxM], dis[MaxM], book[MaxM];
int n, m, s;
int main() {

	Ios;
	cin >> n >> m >> s;
	book[s] = 1;
	memset(head, -1, sizeof(head));
	int num = 0;
	up(i, 1, m) {
		int u, v, w;
		cin >> u >> v >> w;
		e[num].to = v;
		e[num].w = w;
		e[num].next = head[u];
		head[u] = num++;
	}
	up(i, 1, n) {
		dis[i] = pow(2, 31) - 1;
	}
	for (int i = head[s]; i != -1; i = e[i].next) {
		dis[e[i].to] = min(e[i].w, dis[e[i].to]);
	}
	dis[s] = 0;
	up(i, 1, n) {
		int min1 = pow(2, 31) - 1;
		int k = s;
		up(j, 1, n) {
			if (!book[j] && dis[j] < min1) {
				min1 = dis[j];
				k = j;
			}
		}
		book[k] = 1;
		for (int j = head[k]; j != -1; j = e[j].next) {
			if (dis[e[j].to] > dis[k] + e[j].w) {
				dis[e[j].to] = dis[k] + e[j].w;
			}
		}
	}
	up(i, 1, n) {
		cout << dis[i] << ' ';
	}
	return 0;
}

将发送的消息显示至list中

需要创建一个ChatBubble,来显示头像之类的操作,调用类中方法,实际操作:

聊天气泡的设置;

复制代码
Rectangle bubble = new Rectangle(40, 20, message.getContent().length()*15, 30); // 设置气泡大小
            bubble.setArcWidth(10);
            bubble.setArcHeight(10);
            bubble.setFill(isSentByMe ? Color.LIGHTGREEN : Color.LIGHTGRAY); // 根据消息发送者设置颜色

            TextFlow messageFlow = new TextFlow();
            messageFlow.setLayoutX(40);
            messageFlow.setLayoutY(28);
            messageFlow.setPrefWidth(100); // 调整宽度以获得更好的布局
            messageFlow.setLineSpacing(5); // 根据需要调整行间距

            processMessage(message.getContent(), messageFlow,bubble); // 处理消息以包含文本和图片

            getChildren().addAll(head,label,label1,bubble, messageFlow);
相关推荐
LDG_AGI3 分钟前
【推荐系统】深度学习训练框架(九):推荐系统与LLM在Dataset、Tokenizer阶段的异同
人工智能·深度学习·算法·机器学习·推荐算法
爪哇部落算法小助手7 分钟前
每日两题day61
数据结构·c++·算法
Swift社区7 分钟前
LeetCode 439 - 三元表达式解析器
算法·leetcode·ssh
小殊小殊8 分钟前
重磅!DeepSeek发布V3.2系列模型!
论文阅读·人工智能·算法
裤裤兔9 分钟前
利用matlab进行FDR校正的实现方式
数据结构·算法·matlab·多重比较矫正·校正·fdr
野蛮人6号9 分钟前
力扣热题100道之31下一个排列
算法·leetcode·职场和发展
子一!!10 分钟前
哈希桶,元素插入逻辑实现
算法·哈希算法
敲代码的嘎仔10 分钟前
LeetCode面试HOT100——160. 相交链表
java·学习·算法·leetcode·链表·面试·职场和发展
吃着火锅x唱着歌11 分钟前
LeetCode 454.四数相加II
算法·leetcode·职场和发展
敲代码的嘎仔12 分钟前
LeetCode面试HOT100—— 206. 反转链表
java·数据结构·学习·算法·leetcode·链表·面试