跳到正文

个人记录

4月18日总结

Gold_Coin约 7 分钟
原文
文章信息与版本

文章信息

发布
2025年4月24日
收录
2025年12月12日
更新
2026年11月10日
篇幅
约 7 分钟

版本

  1. v1当前

T3 P4826

1.时间复杂度:O(mlogm)

2.错误原因:没写

3.正确思路:因为题目要求最大化所有比赛的总得分,而每场比赛的得分是由两支队伍的 ID 的按位异或值决定的。我们需要通过合理安排比赛顺序,使得所有比赛的得分之和最大,所以是最大生成树,最大生成树又有两种算法,kruskal和prim,分别是以边和点作为研究对象,再看题目,把每一条边都与其他边连上来建图,故会很稠密,用prim算法会跟节省空间,接下来只需要打一个相似模板的代码即可

4.正确代码:

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

int n, a[2005], key[2005];
long long ans;
bool vis[2005];
void Prim() {
    for (int i = 1, x = 0; i <= n; i++, x = 0) {
        for (int j = 1; j <= n; j++) {
        	if (!vis[j]) {
        		if (key[j] >= key[x]) {
        			x = j;
				}
			}
		}
        vis[x] = true;
        for (int y = 1; y <= n; y++) {
        	if (!vis[y]) {
        		key[y] = max(key[y], a[x] ^ a[y]);
			}
		}
    }
}
int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
    	cin >> a[i];
	}
    Prim();
    for (int i = 1; i <= n; i++) {
    	ans += (long long)key[i];
	}
    cout << ans << endl;
}

T4 P2058

1.时间复杂度:O(t * k * k)

2.错误原因:空间开小了

3.正确思路:用一个结构体数组存储船上国家的个数和到达的时间,这里直接存储,每一次存储完后再找到数组内所有超出时间限制的人,并弹出来,ans—,执行完后输出ans

4.正确代码:

C++
#include <bits/stdc++.h>
#define ll long long
using namespace std;

struct node {
	int timee, cty;
}q[300005];

int a[100005], ans, tmp, l;
int main() {
	int T;
	cin >> T;
	while (T--) {
		int t, k;
		cin >> t >> k;
		for (int i = 1; i <= k; i++) {
			int x;
			cin >> x;
			q[++l] = {t, x};
			a[x]++;
			if (a[x] == 1) {
				ans++;	
			}
		}
		while (tmp < l && t - q[tmp].timee >= 86400) {
			a[q[tmp].cty]--;
			if (a[q[tmp].cty] == 0) {
				ans--;	
			}
			tmp++;
		}
		cout << ans << '\n';
	}
    return 0;
}

T6 P10577

1.时间复杂度:O(nlogn)

2.错误原因:没写

3.正确思路:开两个结构体数组来记录,不然到后面有影响,看到题目,情况一共有四种,如果是双向奔赴,就会有集合点,分离则说明前后两个数是不同集合的,这样就只用从左往右遍历,合并等于前一个数的就好了,向右需要开一个 cnt 记录向右的数量,遇到双向奔赴就直接清零,并且都让他们等于中间值

4.正确代码:

C++
#include<bits/stdc++.h>
using namespace std;
#define ll long long

const int N = 1e5 + 5;
int n, pre, now, flag, cnt;

struct s {
    int x, y;
}a[N], b[N];

bool cmp1(s p, s q) {
    if (p.x < q.x) {
    	return true;
	} else {
    	return false;
	}
}

bool cmp2(s p, s q) {
    if (p.y < q.y) {
    	return true;
	} else {
		return false;
	}
}

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        int p;
        cin >> p;
        a[i].x = p; a[i].y = i;
    }
    sort(a + 1, a + 1 + n, cmp1);
    for (int i = 1; i <= n; i++) {
    	b[i].x = a[i].x, b[i].y = a[i].y;
	}
    for (int i = 2; i < n; i++) {
        if (i == 2) {
        	pre = 1, cnt = 1;
		}
        if (a[i].x - a[i - 1].x <= a[i + 1].x - a[i].x) {
            now = -1;
            if (pre == -now) {
                int mid = (a[i].x + a[i - 1].x) >> 1;
                b[i].x = mid;
                for (int j = i - cnt; j <= i - 1; j++) {
                	b[j].x = mid;
				}
                cnt = 0;
            } else if (pre == now) {
                b[i].x = b[i - 1].x;
            }
            pre = -1;
        } else {
            pre = 1;
            if (i == 2) {
            	flag = 1;
			}
            ++cnt;
        }
        if (i == n - 1) {
            if (pre == -1) {
            	b[n].x = b[n - 1].x;
			} else if (pre == 1) {
                int mid = (a[n - 1].x + a[n].x) >> 1;
                b[n].x = mid;
                for (int j = n - cnt; j <= n - 1; j++) {
                	b[j].x = mid;
				}
            }
        }
    }
    if (flag == 1) {
    	b[1].x = b[2].x;
	}
    sort(b + 1, b + n + 1, cmp2);
    for (int i = 1; i <= n; i++) {
    	cout << b[i].x << ' ';
	}
    return 0;
}
原文