CCPC-Wannafly 夏季欢乐赛 题解

博主又复活了(因为文化课作业太多写不完了所以继续自爆自弃


A.完全k叉树

签到题,考虑最底层的点的距离即可,注意细节(成功拉低了平均通过率)

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define Mp make_pair
#define Pi pair<int, int>
#define Pb push_back
#define Fi first
#define Se second
#define Gc getchar
#define LL long long
#define Res register int
#define For(i, l, r) for(int i = (int)(l); i <= (int)(r); ++i)
#define Rep(i, r, l) for(int i = (int)(r); i >= (int)(l); --i)

using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;

inline LL read(){
	LL x = 0;
	char ch = Gc();
	bool positive = 1;
	for (; !isdigit(ch); ch = Gc())	if (ch == '-')	positive = 0;
	for (; isdigit(ch); ch = Gc()) x = (x << 1) + (x << 3) + (ch ^ 48);
	return positive ? x : -x;
}

int main() {
	int T = read();
	while (T--) {
	LL k = read(), n = read(), x = 1, now = 1, cnt = 0;
	if (k == 1) {
		printf("%d\n", n - 1);
		continue;
	}
	while (x < n) {
		cnt++;
		now *= k;
		x += now;
	}
	x -= now;
	cnt--;
	LL ans = 0, lft = n - x;
	if (lft <= now / k) ans = max(cnt * 2 + 1, lft == 1 ? 1LL : 2LL);
	else ans = cnt * 2 + 2;
	printf("%d\n", ans);
	}
	return 0;
}

B.距离产生美

签到题,每次当且仅当 ∣ a i − a i − 1 ∣ &lt; k |a_i-a_{i-1}|&lt;k aiai1<k时,把 a i a_i ai改成 i n f inf inf

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define Mp make_pair
#define Pi pair<int, int>
#define Pb push_back
#define Fi first
#define Se second
#define Gc getchar
#define LL long long
#define Res register int
#define For(i, l, r) for(int i = (int)(l); i <= (int)(r); ++i)
#define Rep(i, r, l) for(int i = (int)(r); i >= (int)(l); --i)

using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
const int N = 100010;
	
int a[N], used[N];
inline LL read(){
	LL x = 0;
	char ch = Gc();
	bool positive = 1;
	for (; !isdigit(ch); ch = Gc())	if (ch == '-')	positive = 0;
	for (; isdigit(ch); ch = Gc()) x = (x << 1) + (x << 3) + (ch ^ 48);
	return positive ? x : -x;
}

int main() {
	int n = read(), k = read(), ans = 0;
	For(i, 1, n) a[i] = read();
	For(i, 2, n) {
		if (abs(a[i] - a[i - 1]) < k && !used[i - 1]) {
			ans++;
			used[i] = 1;
		}
	}
	printf("%d\n", ans);
	return 0;
}

C.烤面包片

签到题, m o d ≤ 1 e 9 , 4 ! ! &gt; 1 e 9 mod\leq 1e9,4!!&gt;1e9 mod1e9,4!!>1e9所以只需考虑 n ≤ 3 n\leq 3 n3的情况,其他都输出 0 0 0,注意 0 ! ! ! = 1 0!!!=1 0!!!=1

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define Mp make_pair
#define Pi pair<int, int>
#define Pb push_back
#define Fi first
#define Se second
#define Gc getchar
#define LL long long
#define Res register int
#define For(i, l, r) for(int i = (int)(l); i <= (int)(r); ++i)
#define Rep(i, r, l) for(int i = (int)(r); i >= (int)(l); --i)

using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;

inline int mul(int x, int y, int M) {return (int)(1LL * x * y % M);}
inline LL read(){
	LL x = 0;
	char ch = Gc();
	bool positive = 1;
	for (; !isdigit(ch); ch = Gc())	if (ch == '-')	positive = 0;
	for (; isdigit(ch); ch = Gc()) x = (x << 1) + (x << 3) + (ch ^ 48);
	return positive ? x : -x;
}

int main() {
	int n = read(), M = read();
	if (n == 0 || n == 1) printf("%d\n", 1 % M);
	else if (n == 2) printf("%d\n", 2 % M);
	else if (n == 3) {
		int now = 1;
		For(i, 1, 720) now = mul(now, i, M);
		printf("%d\n", now);
	}
	else puts("0");
		
	return 0;
}

D.茶颜悦色

普通题,将y坐标离散对x这一维线性枚举长度为k的所有点,对y这一维用扫描线维护只对[y-k,y]有贡献,时间复杂度 O ( n log ⁡ n ) \mathcal{O(n\log n)} O(nlogn)给茶颜悦色打广告可还行

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define Mp make_pair
#define Pi pair<int, int>
#define Pb push_back
#define Fi first
#define Se second
#define Gc getchar
#define LL long long
#define Res register int
#define For(i, l, r) for(int i = (int)(l); i <= (int)(r); ++i)
#define Rep(i, r, l) for(int i = (int)(r); i >= (int)(l); --i)

using namespace std;
const int N = 100010;
struct Point {
	int x, y;
}p[N];
struct Node {
	Node *ls, *rs;
	int tag, v;
	Node() {
		ls = rs = 0;
		tag = v = 0;
	}
}*root;
int py[N << 1], cnty;

inline LL read(){
	LL x = 0;
	char ch = Gc();
	bool positive = 1;
	for (; !isdigit(ch); ch = Gc())	if (ch == '-')	positive = 0;
	for (; isdigit(ch); ch = Gc()) x = (x << 1) + (x << 3) + (ch ^ 48);
	return positive ? x : -x;
}

inline bool cmp(Point a, Point b) {
	return a.x < b.x || a.x == b.x && a.y < b.y;
}

inline void pushdown(Node *&rt) {
	if (!rt -> ls) rt -> ls = new Node();
	if (!rt -> rs) rt -> rs = new Node();
	rt -> ls -> v += rt -> tag;
	rt -> rs -> v += rt -> tag;
	rt -> ls -> tag += rt -> tag;
	rt -> rs -> tag += rt -> tag;
	rt -> tag = 0;
}

inline void pushup(Node *&rt) {
	rt -> v = max(rt -> ls -> v, rt -> rs -> v);
}

inline void modify(int l, int r, int L, int R, int val, Node *&rt) {
	if (!rt) rt = new Node();
	if (r < L || R < l) return;
	if (L <= l && r <= R) {
		rt -> tag += val;
		rt -> v += val;
		return;
	}
	int mid = l + r >> 1;
	pushdown(rt);
	modify(l, mid, L, R, val, rt -> ls); 
	modify(mid + 1, r, L, R, val, rt -> rs);
	pushup(rt);
}

int main() {
	int n = read(), k = read();
	py[++cnty] = 1;
	For(i, 1, n) {
		p[i].x = read(), p[i].y = read();
		py[++cnty] = p[i].y;
		py[++cnty] = p[i].y - k;
	}
	sort(py + 1, py + cnty + 1);
	int ny = unique(py + 1, py + cnty + 1) - py - 1;
	sort(p + 1, p + n + 1, cmp);
	int l = 1, r = 1;
	for(; r <= n; ++r) {
		if (p[r].x - p[l].x > k) break;
		modify(1, ny, lower_bound(py + 1, py + ny + 1, p[r].y - k) - py, lower_bound(py + 1, py + ny + 1, p[r].y) - py, 1, root);
	}
	int ans = 0;
	for(; r <= n; ++r) {
		ans = max(ans, root -> v);
		for(; p[r].x - p[l].x > k; ++l) modify(1, ny, lower_bound(py + 1, py + ny + 1, p[l].y - k) - py, lower_bound(py + 1, py + ny + 1, p[l].y) - py, -1, root);
		modify(1, ny, lower_bound(py + 1, py + ny + 1, p[r].y - k) - py, lower_bound(py + 1, py + ny + 1, p[r].y) - py, 1, root);
	}
	ans = max(ans, root -> v);
	printf("%d\n", ans);
	return 0;
}

E.飞行棋

普通题,考虑在 [ d − k , d − 1 ] [d-k,d-1] [dk,d1]中的每一个位置都有 1 k \frac{1}{k} k1的概率到达终点,还有 k − 1 k \frac{k-1}{k} kk1的概率到达另一个 [ d − k , d − 1 ] [d-k,d-1] [dk,d1]的点,所以期望步数是 k k k。从终点出发逆向dp, d p [ x ] = k ( x ∈ [ 1 , k ] ) , d p [ x ] = 1 + 1 k ∑ i = x − k x − 1 d p [ i ] dp[x]=k(x\in [1,k]),dp[x]=1+\frac{1}{k}\sum_{i=x-k}^{x-1}dp[i] dp[x]=k(x[1,k]),dp[x]=1+k1i=xkx1dp[i],由于d较大,使用矩阵加速即可。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define Mp make_pair
#define Pi pair<int, int>
#define Pb push_back
#define Fi first
#define Se second
#define Gc getchar
#define LL long long
#define Res register int
#define For(i, l, r) for(int i = (int)(l); i <= (int)(r); ++i)
#define Rep(i, r, l) for(int i = (int)(r); i >= (int)(l); --i)

using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
const int N = 25;

int K, inv;
inline LL read(){
	LL x = 0;
	char ch = Gc();
	bool positive = 1;
	for (; !isdigit(ch); ch = Gc())	if (ch == '-')	positive = 0;
	for (; isdigit(ch); ch = Gc()) x = (x << 1) + (x << 3) + (ch ^ 48);
	return positive ? x : -x;
}

inline void upd(int &x, int y) {x += y; x -= x >= MOD ? MOD : 0;}
inline int dec(int x, int y) {x -= y; x += x < 0 ? MOD : 0; return x;}
inline int add(int x, int y) {x += y; x -= x >= MOD ? MOD : 0; return x;}
inline int mul(int x, int y) {return 1LL * x * y % MOD;};

inline int qui_pow(int x, int y) {
	int ret = 1;
	for(; y; y >>= 1) {
		if (y & 1) ret = mul(ret, x);
		x = mul(x, x);
	}
	return ret;
}

struct Matrix {
	int num[N][N];
	Matrix operator * (const Matrix &A) const {
		Matrix ret;
		memset(ret.num, 0, sizeof ret.num);
		For(i, 1, K) For(j, 1, K) For(k, 1, K) upd(ret.num[i][j], mul(num[i][k], A.num[k][j]));
		return ret;
	}
}init, cha;

inline Matrix mat_pow(Matrix x, LL y) {
	Matrix ret;
	memset(ret.num, 0, sizeof ret.num);
	For(i, 1, K) ret.num[i][i] = 1;
	for(; y; y >>= 1) {
		if (y & 1) ret = ret * x;
		x = x * x;
	}
	return ret;
}

int main() {
	LL d = read();
	K = read(), inv = qui_pow(K, MOD - 2);
	For(i, 1, K) init.num[1][i] = K, cha.num[i][1] = inv;
	init.num[1][K + 1] = cha.num[K + 1][1] = cha.num[K + 1][K + 1] = 1;
	For(i, 2, K) cha.num[i - 1][i] = 1;
	K++;
	init = init * mat_pow(cha, d - K + 1);
	printf("%d\n", init.num[1][1]);
	return 0;
}

F.三元组

普通题,假设 a i + a j ≤ b i + b j a_i+a_j\leq b_i+b_j ai+ajbi+bj,所以 2 ( a i + a j ) ≤ b i + b j , 2 a i − b i + 2 a j − b j ≤ 0 2(a_i+a_j)\leq b_i +b_j,2a_i-b_i+2a_j-b_j\leq 0 2(ai+aj)bi+bj,2aibi+2ajbj0,只需按 2 a i − b i 2a_i-b_i 2aibi排序即可, a i + a j ≥ b i + b j a_i+a_j\geq b_i+b_j ai+ajbi+bj亦然

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define Mp make_pair
#define Pi pair<int, int>
#define Pb push_back
#define Fi first
#define Se second
#define Gc getchar
#define LL long long
#define Res register int
#define For(i, l, r) for(int i = (int)(l); i <= (int)(r); ++i)
#define Rep(i, r, l) for(int i = (int)(r); i >= (int)(l); --i)

using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
const int N = 100010;

inline void upd(int &x, int y) {x += y; x -= x >= MOD ? MOD : 0;}
inline int dec(int x, int y) {x -= y; x += x < 0 ? MOD : 0; return x;}
inline int add(int x, int y) {x += y; x -= x >= MOD ? MOD : 0; return x;}
inline int mul(int x, int y) {return 1LL * x * y % MOD;};

inline LL read(){
	LL x = 0;
	char ch = Gc();
	bool positive = 1;
	for (; !isdigit(ch); ch = Gc())	if (ch == '-')	positive = 0;
	for (; isdigit(ch); ch = Gc()) x = (x << 1) + (x << 3) + (ch ^ 48);
	return positive ? x : -x;
}

struct Node {
	int a, b, c, v;
}p[N];
int v[N], sum[N], n;

inline bool cmp(Node x, Node y) {return x.v < y.v;}
 
inline int solve() {
	int ret = 0;
	For(i, 1, n) p[i].v = 2 * p[i].a - p[i].b;
	sort(p + 1, p + n + 1, cmp);
	sum[0] = v[0] = 0;
	For(i, 1, n) {
		v[i] = p[i].v;
		sum[i] = add(sum[i - 1], p[i].c);
		upd(ret, mul(sum[upper_bound(v + 1, v + i + 1, -v[i]) - v - 1], p[i].c));
	}
	return ret;
}
		
int main() {
	n = read();
	For(i, 1, n) p[i].a = read(), p[i].b = read(), p[i].c = read();
	int ans = solve();
	For(i, 1, n) swap(p[i].a, p[i].b);
	upd(ans, solve());
	printf("%d\n", ans);
	return 0;
}


G.篮球校赛

签到题,直接状压dp,时间复杂度 O ( 5 ∗ 2 5 n ) \mathcal{O(5* 2^5 n)} O(525n)

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define Mp make_pair
#define Pi pair<int, int>
#define Pb push_back
#define Fi first
#define Se second
#define Gc getchar
#define LL long long
#define Res register int
#define For(i, l, r) for(int i = (int)(l); i <= (int)(r); ++i)
#define Rep(i, r, l) for(int i = (int)(r); i >= (int)(l); --i)

using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
const int N = 100010;

inline LL read(){
	LL x = 0;
	char ch = Gc();
	bool positive = 1;
	for (; !isdigit(ch); ch = Gc())	if (ch == '-')	positive = 0;
	for (; isdigit(ch); ch = Gc()) x = (x << 1) + (x << 3) + (ch ^ 48);
	return positive ? x : -x;
}

LL dp[N][32], a[N][5];

int main() {
	int n = read();
	For(i, 1, n) For(j, 0, 4) a[i][j] = read();
	For(i, 1, n) {
		For(j, 0, 31) {
			For(k, 0, 4) {
				if ((j >> k) & 1) dp[i][j] = max(dp[i - 1][j ^ (1 << k)] + a[i][k], dp[i][j]);
				dp[i][j] = max(dp[i - 1][j], dp[i][j]);
			}
		}
	}
	printf("%lld\n", dp[n][31]);
	return 0;
}

H.分配学号

签到题,先排序确定最终的学号集合,易证明集合是唯一的,从大到小枚举可能,运用乘法原理和加法原理求得答案

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define Mp make_pair
#define Pi pair<int, int>
#define Pb push_back
#define Fi first
#define Se second
#define Gc getchar
#define LL long long
#define Res register int
#define For(i, l, r) for(int i = (int)(l); i <= (int)(r); ++i)
#define Rep(i, r, l) for(int i = (int)(r); i >= (int)(l); --i)

using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
const int N = 100010;
inline void upd(int &x, int y) {x += y; x -= x >= MOD ? MOD : 0;}
inline int dec(int x, int y) {x -= y; x += x < 0 ? MOD : 0; return x;}
inline int add(int x, int y) {x += y; x -= x >= MOD ? MOD : 0; return x;}
inline int mul(int x, int y) {return 1LL * x * y % MOD;}

LL a[N], b[N];
int ans = 1, cnt[N];

inline LL read(){
	LL x = 0;
	char ch = Gc();
	bool positive = 1;
	for (; !isdigit(ch); ch = Gc())	if (ch == '-')	positive = 0;
	for (; isdigit(ch); ch = Gc()) x = (x << 1) + (x << 3) + (ch ^ 48);
	return positive ? x : -x;
}

int main() {
	int n = read();
	For(i, 1, n) a[i] = read();
	sort(a + 1, a + n + 1);
	memcpy(b, a, sizeof b);
	For(i, 2, n) {
		if (a[i] <= a[i - 1]) a[i] = a[i - 1] + 1, cnt[i] = cnt[i - 1] + 1;
	}
	int now = 0, maxi = 0, ret = 1;
	Rep(i, n, 1) {
		if (!cnt[i]) {
			ans = mul(ans, ret);
			now = maxi = 0;
			continue;
		}
		if (!now) now = cnt[i], maxi = a[i], ret = 1;
		ret = mul(ret, maxi - b[i] + 1 - now + cnt[i]);
	}
	printf("%d\n", ans);
	return 0;
}

I.Gree的心房

签到题,留出最短的一条路径,可见障碍物最多填充 ( n − 1 ) × ( m − 1 ) (n-1)\times(m-1) (n1)×(m1)个位置,超过这个值就输出-1,否则输出n+m-2

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define Mp make_pair
#define Pi pair<int, int>
#define Pb push_back
#define Fi first
#define Se second
#define Gc getchar
#define LL long long
#define Res register int
#define For(i, l, r) for(int i = (int)(l); i <= (int)(r); ++i)
#define Rep(i, r, l) for(int i = (int)(r); i >= (int)(l); --i)

using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;

inline LL read(){
	LL x = 0;
	char ch = Gc();
	bool positive = 1;
	for (; !isdigit(ch); ch = Gc())	if (ch == '-')	positive = 0;
	for (; isdigit(ch); ch = Gc()) x = (x << 1) + (x << 3) + (ch ^ 48);
	return positive ? x : -x;
}

int main() {
	int n = read(), m = read(), k = read();
	LL all = 1LL * (n - 1) * (m - 1);
	if (k <= all) printf("%d\n", n + m - 2);
	else puts("-1");
	return 0;
}

比赛总结:jwj是有gf的人生赢家!我也要!

【使用教程】 一、环境配置 1、建议下载anaconda和pycharm 在anaconda中配置好环境,然后直接导入到pycharm中,在pycharm中运行项目 anaconda和pycharm安装及环境配置参考网上博客,有很多博主介绍 2、在anacodna中安装requirements.txt中的软件包 命令为:pip install -r requirements.txt 或者改成清华源后再执行以上命令,这样安装要快一些 软件包都安装成功后才算成功 3、安装好软件包后,把anaconda中对应的python导入到pycharm中即可(不难,参考网上博客) 二、环境配置好后,开始训练(也可以训练自己数据集) 1、数据集准备 需要准备yolo格式的目标检测数据集,如果不清楚yolo数据集格式,或者有其他数据训练需求,请看博主yolo格式各种数据集集合链接:https://blog.csdn.net/DeepLearning_/article/details/127276492 里面涵盖了上百种yolo数据集,且在不断更新,基本都是实际项目使用。来自于网上收集、实际场景采集制作等,自己使用labelimg标注工具标注的。数据集质量绝对有保证! 本项目所使用的数据集,见csdn该资源下载页面中的介绍栏,里面有对应的下载链接,下载后可直接使用。 2、数据准备好,开始修改配置文件 参考代码中data文件夹下的banana_ripe.yaml,可以自己新建一个不同名称的yaml文件 train:训练集的图片路径 val:验证集的图片路径 names: 0: very-ripe 类别1 1: immature 类别2 2: mid-ripe 类别3 格式按照banana_ripe.yaml照葫芦画瓢就行,不需要过多参考网上的 3、修改train_dual.py中的配置参数,开始训练模型 方式一: 修改点: a.--weights参数,填入'yolov9-s.pt',博主训练的是yolov9-s,根据自己需求可自定义 b.--cfg参数,填入 models/detect/yolov9-c.yaml c.--data参数,填入data/banana_ripe.yaml,可自定义自己的yaml路径 d.--hyp参数,填入hyp.scratch-high.yaml e.--epochs参数,填入100或者200都行,根据自己的数据集可改 f.--batch-size参数,根据自己的电脑性能(显存大小)自定义修改 g.--device参数,一张显卡的话,就填0。没显卡,使用cpu训练,就填cpu h.--close-mosaic参数,填入15 以上修改好,直接pycharm中运行train_dual.py开始训练 方式二: 命令行方式,在pycharm中的终端窗口输入如下命令,可根据自己情况修改参数 官方示例:python train_dual.py --workers 8 --device 0 --batch 16 --data data/coco.yaml --img 640 --cfg models/detect/yolov9-c.yaml --weights '' --name yolov9-c --hyp hyp.scratch-high.yaml --min-items 0 --epochs 500 --close-mosaic 15 训练完会在runs/train文件下生成对应的训练文件及模型,后续测试可以拿来用。 三、测试 1、训练完,测试 修改detect_dual.py中的参数 --weights,改成上面训练得到的best.pt对应的路径 --source,需要测试的数据图片存放的位置,代码中的test_imgs --conf-thres,置信度阈值,自定义修改 --iou-thres,iou阈值,自定义修改 其他默认即可 pycharm中运行detect_dual.py 在runs/detect文件夹下存放检测结果图片或者视频 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值