跳到正文

个人记录

2.27改错+题解

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

文章信息

收录
2026年2月27日
更新
2026年2月27日
篇幅
约 7 分钟

版本

  1. v2当前
  2. v1

3.最少油耗(gas.cpp)

题目大意:

A国有n座编号为1-n的城市与n-1条公路,每条公路连接两座城市,汽车行驶一段公路油耗为1,现在第2-n座城市的代表要去第1座城市,他们可以共坐一辆车,但人数不超过座位数m,现在求最少油耗。

解题思路

每个节点都要到达根节点(tr[1]),所以可以从叶子节点考虑。以样例2为例

7 2

1 2

2 4

4 3

6 1

1 5

5 7

其中的叶子节点有3,6,7.从7开始遍历,往上找到5,此时7->5消耗一单位油,而5节点人数变为2,以此类推,遍历所有叶子节点,那么所有叶子节点油耗都为1,且与叶子节点相连的节点:5和1和4的人数都变为2.再遍历5,4,1;得到1的人数为3,2的人数为3.因为1的人不用移动,所以可以不统计1,故2不用再遍历。

此时我们获得了所有非根节点的节点的人数,这些人向上移动需要的车辆就为(人数/车数)再向上取整,而向上移动至另一个城市每辆车要用1单位油,所有总耗油量为以每个节点为根节点的子树的节点个数/座位数再向上取整的和

遍历思路

先初始化每个节点的人数为1,再将1作为根节点,使用深度优先遍历(DFS)遍历根节点所有子节点,因为有n-1条路,所以当一个节点中与它相连的节点数量为1且该节点不是1,那么它就是叶子节点,遇到直接返回,每返回1次,就使根节点人数+1(DFS遍历时会把所有节点作为根节点遍历子树,这里的根节点含义就是遍历时产生的根节点,而非1),这样就得到了每个节点的子树的节点个数。

AC代码

C++
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int read(){
	int cnt = 0,f = 1;
	char ch = getchar();
	while(ch > '9' || ch < '0'){
		if(ch == '-'){
			f = -1;
		}
		ch = getchar();
	}
	while(ch >= '0' && ch <= '9' ){
		cnt = cnt*10+ch-'0';
		ch = getchar();
	}
	return f*cnt;
}
const int N = 1e4+5;
int n,m,a,b,vis[N];
struct node{
	vector<int> nxt;
	int man,oil;
}C[N];
void dfs(int root){
	if(C[root].nxt.size() == 1 && root != 1){
		return ;
	}
	for(auto it : C[root].nxt){
		if(vis[it] == 0){
			//cout << it << endl;
			vis[it] = 1;
			dfs(it);
			C[root].man += C[it].man;
			//vis[it] = 0;
		}
	}
	return ;
}
int main(){
	//freopen("gas.in","r",stdin);
	//freopen("gas.out","w",stdout);
	n = read(),m = read();
	for(int i=1;i<n;i++){
		cin >> a >> b;
		C[a].nxt.push_back(b);
		C[b].nxt.push_back(a);
		C[a].man = 1;
		C[b].man = 1;
	}
	vis[1] = 1;
	dfs(1);
	ll ans = 0;
	for(int i=2;i<=n;i++){
		//cout << C[i].man << " " << i << endl;
		ans+=ceil(C[i].man*1.0/m);
	}
	cout << ans;
	return 0;
}

4.商船守护(guard.cpp)

题目描述

在一个二维平面中,左上角坐标为(1,1),右下角坐标为(HH,WW),在坐标系中有n个灯塔,坐标为(CiC_i,RiR_i),小酷从(1,1)出发前往(HH,WW),途中只能向下或向右移动,问他可以经过的灯塔数量最大为多少。

解题思路

我们很容易想到用DP来做,但DPOO(n2n^2)的时间复杂度一定会炸。我们可以将所有灯塔按照x从小到大排序,若x相同则按照y从小到大排序,这样只要求灯塔数组的最长上升子序列(LST)即可。

求LST的方法有DP和贪心+二分,DP的时间复杂度为OO(n2n^2),而贪心+二分的时间复杂度为OO(nn lognlog_n),所以用贪心+二分即可满分。

贪心+二分思路:

贪心策略:

维护一个数组 tails,其中 tails[i] 表示长度为 i+1 的上升子序列的最小末尾元素。 例如:tails = [2,3,7,18] 表示:

长度 1 的 LIS 最小末尾是 2;

长度 2 的 LIS 最小末尾是 3;

长度 3 的 LIS 最小末尾是 7;

长度 4 的 LIS 最小末尾是 18。

更新规则:

遍历每个元素 num:

若 num > tails.back():直接加入 tails 末尾(LIS 长度 + 1);

否则:用二分查找找到 tails 中第一个 ≥ num 的位置,替换该位置的元素(保证 tails 始终是 “最小末尾”,为后续更长的子序列留空间)

最终结果

tails 的长度即为 LIS 的长度。

AC代码

C++
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int read(){
	int cnt = 0,f = 1;
	char ch = getchar();
	while(ch > '9' || ch < '0'){
		if(ch == '-'){
			f = -1;
		}
		ch = getchar();
	}
	while(ch >= '0' && ch <= '9' ){
		cnt = cnt*10+ch-'0';
		ch = getchar();
	}
	return f*cnt;
}
bool cmp(pair<int,int> a,pair<int,int> b){
	if(a.first != b.first){
		return a.first < b.first;
	}else{
		return a.second < b.second;
	}

}
const int N = 2e5+5;
int h,w,n,x,y,zx[2] = {-1,0},zy[2] = {0,-1},sx[N];
vector<pair<int,int> > vc;
vector<int> tails;
struct node{
	int x,y,s;
};
int main(){
	vector<int> l;
	//freopen("guard.in","r",stdin);
	//freopen("guard.out","w",stdout);
	h = read(),w = read(),n = read();
	for(int i=1;i<=n;i++){
		cin >> x >> y;
		vc.push_back({x,y});
	}
	sort(vc.begin(),vc.end(),cmp);
	//cout << 1 << endl;
	for(auto it : vc){
		if(!tails.size()){
			tails.push_back(it.second);
			continue;
		}
		if(it.second >= tails.back()){
			tails.push_back(it.second);
		}else{
			int len = upper_bound(tails.begin(),tails.end(),it.second) - tails.begin();
			tails[len] = it.second;
		}
	}
	cout << tails.size();
	return 0;
}

这么强?!

?!强强!?

有点弱:)

?!弱弱!?

原文